MissileWars.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * This file is part of MissileWars (https://github.com/Butzlabben/missilewars).
  3. * Copyright (c) 2018-2021 Daniel Nägele.
  4. *
  5. * MissileWars is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * MissileWars is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with MissileWars. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. package de.butzlabben.missilewars;
  19. import co.aikar.commands.PaperCommandManager;
  20. import de.butzlabben.missilewars.commands.*;
  21. import de.butzlabben.missilewars.configuration.Config;
  22. import de.butzlabben.missilewars.configuration.Messages;
  23. import de.butzlabben.missilewars.game.Arenas;
  24. import de.butzlabben.missilewars.game.GameManager;
  25. import de.butzlabben.missilewars.game.misc.MissileWarsPlaceholder;
  26. import de.butzlabben.missilewars.game.signs.CheckRunnable;
  27. import de.butzlabben.missilewars.game.signs.SignRepository;
  28. import de.butzlabben.missilewars.game.stats.StatsFetcher;
  29. import de.butzlabben.missilewars.listener.PlayerListener;
  30. import de.butzlabben.missilewars.listener.SignListener;
  31. import de.butzlabben.missilewars.player.PlayerData;
  32. import de.butzlabben.missilewars.util.ConnectionHolder;
  33. import de.butzlabben.missilewars.util.MoneyUtil;
  34. import de.butzlabben.missilewars.util.SetupUtil;
  35. import de.butzlabben.missilewars.util.stats.PreFetcher;
  36. import de.butzlabben.missilewars.util.version.VersionUtil;
  37. import lombok.Getter;
  38. import org.apache.commons.io.FileUtils;
  39. import org.bstats.bukkit.Metrics;
  40. import org.bukkit.Bukkit;
  41. import org.bukkit.configuration.serialization.ConfigurationSerialization;
  42. import org.bukkit.entity.Player;
  43. import org.bukkit.plugin.java.JavaPlugin;
  44. import java.io.File;
  45. import java.util.Date;
  46. /**
  47. * @author Butzlabben
  48. * @since 01.01.2018
  49. */
  50. @Getter
  51. public class MissileWars extends JavaPlugin {
  52. @Getter
  53. private static MissileWars instance;
  54. public final String version = getDescription().getVersion();
  55. private SignRepository signRepository;
  56. public PaperCommandManager commandManager;
  57. private boolean foundFAWE;
  58. @Getter
  59. private PlayerListener playerListener;
  60. @Getter
  61. private SignListener signListener;
  62. public MissileWars() {
  63. instance = this;
  64. }
  65. @Override
  66. public void onEnable() {
  67. long startTime;
  68. long endTime;
  69. startTime = System.currentTimeMillis();
  70. sendPluginInfo();
  71. Logger.BOOT.log("Loading properties...");
  72. // delete old missile wars temp-worlds from the last server session
  73. deleteTempWorlds();
  74. Config.load();
  75. Messages.load();
  76. SetupUtil.saveDefaultSchematics(new File(Config.getMissilesFolder()), "missiles.zip");
  77. SetupUtil.saveDefaultSchematics(new File(Config.getShieldsFolder()), "shields.zip");
  78. new File(Config.getLobbiesFolder()).mkdirs();
  79. this.signRepository = SignRepository.load();
  80. registerEvents();
  81. registerCommands();
  82. Arenas.load();
  83. GameManager.getInstance().loadGamesOnStartup();
  84. new Metrics(this, 3749);
  85. // Check if FAWE is installed
  86. foundFAWE = Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit") != null;
  87. GameManager.getInstance().getGames().values().forEach(game -> {
  88. for (Player player : Bukkit.getOnlinePlayers()) {
  89. if (!game.isIn(player.getLocation())) continue;
  90. game.teleportToLobbySpawn(player);
  91. }
  92. });
  93. MoneyUtil.giveMoney(null, -1);
  94. Bukkit.getScheduler().runTaskTimerAsynchronously(this, new CheckRunnable(), 20, 20 * 10);
  95. if (Config.isPrefetchPlayers()) {
  96. PreFetcher.preFetchPlayers(new StatsFetcher(new Date(0L), ""));
  97. }
  98. checkPlaceholderAPI();
  99. ConfigurationSerialization.registerClass(PlayerData.class);
  100. endTime = System.currentTimeMillis();
  101. Logger.SUCCESS.log("MissileWars was enabled in " + (endTime - startTime) + "ms");
  102. }
  103. @Override
  104. public void onDisable() {
  105. GameManager.getInstance().disableAll();
  106. deleteTempWorlds();
  107. ConnectionHolder.close();
  108. }
  109. /**
  110. * This method checks if the PlaceholderAPI is installed. When it is
  111. * installed, a message is sent to the log.
  112. */
  113. private void checkPlaceholderAPI() {
  114. if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
  115. new MissileWarsPlaceholder(this).register();
  116. Logger.NORMAL.log("The PlaceholderAPI is installed. New placeholders are provided by MissileWars.");
  117. }
  118. }
  119. /**
  120. * This method registers all events of the MissileWars event listener.
  121. */
  122. private void registerEvents() {
  123. playerListener = new PlayerListener();
  124. signListener = new SignListener();
  125. Bukkit.getPluginManager().registerEvents(playerListener, this);
  126. Bukkit.getPluginManager().registerEvents(signListener, this);
  127. }
  128. /**
  129. * This method loads the command manager and registers the MissileWars commands.
  130. */
  131. private void registerCommands() {
  132. Logger.BOOT.log("Registering commands");
  133. // Using the Paper Command Manager does not mean the plugin requires Paper.
  134. // It simply lets it take advantage of Paper specific features if available,
  135. // such as Asynchronous Tab Completions.
  136. commandManager = new PaperCommandManager(this);
  137. new MWCommandCompletions(commandManager);
  138. commandManager.registerCommand(new MWCommands());
  139. commandManager.registerCommand(new StatsCommands());
  140. commandManager.registerCommand(new UserCommands());
  141. commandManager.registerCommand(new SetupCommands());
  142. }
  143. /**
  144. * This method checks if FAWE (FastAsyncWorldEdit) is installed.
  145. *
  146. * @return true, if it's installed
  147. */
  148. public boolean foundFAWE() {
  149. return foundFAWE;
  150. }
  151. /**
  152. * This methode deletes the temp arena worlds of the MW game.
  153. */
  154. private void deleteTempWorlds() {
  155. File[] dirs = Bukkit.getWorldContainer().listFiles();
  156. if (dirs == null) return;
  157. for (File dir : dirs) {
  158. if (dir.getName().startsWith("mw-")) {
  159. try {
  160. FileUtils.deleteDirectory(dir);
  161. } catch (Exception ignored) {
  162. }
  163. }
  164. }
  165. }
  166. /**
  167. * This method sends information about the version, version
  168. * warnings (if necessary) and authors in the console.
  169. */
  170. private void sendPluginInfo() {
  171. Logger.BOOT.log("This server is running MissileWars v" + version + " by Butzlabben");
  172. if (VersionUtil.getVersion() < 20) {
  173. Logger.WARN.log("====================================================");
  174. Logger.WARN.log("It seems that you are using version older than 1.20");
  175. Logger.WARN.log("There is no guarantee for this to work");
  176. Logger.WARN.log("Proceed with extreme caution");
  177. Logger.WARN.log("====================================================");
  178. }
  179. if (version.contains("beta")) {
  180. Logger.WARN.log("NOTE: This is a beta version which means, that it may not be fully stable");
  181. }
  182. if (getDescription().getAuthors().size() > 1) {
  183. StringBuilder sb = new StringBuilder();
  184. for (String author : getDescription().getAuthors()) {
  185. if (author.equals("Butzlabben"))
  186. continue;
  187. sb.append(author);
  188. sb.append(" ");
  189. }
  190. Logger.BOOT.log("Other authors: " + sb);
  191. }
  192. }
  193. }