mcMMO.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. package com.gmail.nossr50;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import net.shatteredlands.shatt.backup.ZipLibrary;
  8. import org.bukkit.OfflinePlayer;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.plugin.PluginDescriptionFile;
  11. import org.bukkit.plugin.PluginManager;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13. import org.bukkit.scheduler.BukkitScheduler;
  14. import com.gmail.nossr50.commands.general.AddlevelsCommand;
  15. import com.gmail.nossr50.commands.general.AddxpCommand;
  16. import com.gmail.nossr50.commands.general.InspectCommand;
  17. import com.gmail.nossr50.commands.general.McstatsCommand;
  18. import com.gmail.nossr50.commands.general.MmoeditCommand;
  19. import com.gmail.nossr50.commands.general.MmoupdateCommand;
  20. import com.gmail.nossr50.commands.general.XprateCommand;
  21. import com.gmail.nossr50.commands.mc.McabilityCommand;
  22. import com.gmail.nossr50.commands.mc.MccCommand;
  23. import com.gmail.nossr50.commands.mc.McgodCommand;
  24. import com.gmail.nossr50.commands.mc.McmmoCommand;
  25. import com.gmail.nossr50.commands.mc.McrefreshCommand;
  26. import com.gmail.nossr50.commands.mc.McremoveCommand;
  27. import com.gmail.nossr50.commands.mc.MctopCommand;
  28. import com.gmail.nossr50.commands.party.ACommand;
  29. import com.gmail.nossr50.commands.party.AcceptCommand;
  30. import com.gmail.nossr50.commands.party.InviteCommand;
  31. import com.gmail.nossr50.commands.party.PCommand;
  32. import com.gmail.nossr50.commands.party.PartyCommand;
  33. import com.gmail.nossr50.commands.party.PtpCommand;
  34. import com.gmail.nossr50.commands.skills.AcrobaticsCommand;
  35. import com.gmail.nossr50.commands.skills.ArcheryCommand;
  36. import com.gmail.nossr50.commands.skills.AxesCommand;
  37. import com.gmail.nossr50.commands.skills.ExcavationCommand;
  38. import com.gmail.nossr50.commands.skills.FishingCommand;
  39. import com.gmail.nossr50.commands.skills.HerbalismCommand;
  40. import com.gmail.nossr50.commands.skills.MiningCommand;
  41. import com.gmail.nossr50.commands.skills.RepairCommand;
  42. import com.gmail.nossr50.commands.skills.SwordsCommand;
  43. import com.gmail.nossr50.commands.skills.TamingCommand;
  44. import com.gmail.nossr50.commands.skills.UnarmedCommand;
  45. import com.gmail.nossr50.commands.skills.WoodcuttingCommand;
  46. import com.gmail.nossr50.commands.spout.MchudCommand;
  47. import com.gmail.nossr50.commands.spout.XplockCommand;
  48. import com.gmail.nossr50.config.Config;
  49. import com.gmail.nossr50.config.HiddenConfig;
  50. import com.gmail.nossr50.config.TreasuresConfig;
  51. import com.gmail.nossr50.config.mods.CustomArmorConfig;
  52. import com.gmail.nossr50.config.mods.CustomBlocksConfig;
  53. import com.gmail.nossr50.config.mods.CustomToolsConfig;
  54. import com.gmail.nossr50.config.repair.RepairConfigManager;
  55. import com.gmail.nossr50.datatypes.PlayerProfile;
  56. import com.gmail.nossr50.listeners.BlockListener;
  57. import com.gmail.nossr50.listeners.EntityListener;
  58. import com.gmail.nossr50.listeners.HardcoreListener;
  59. import com.gmail.nossr50.listeners.PlayerListener;
  60. import com.gmail.nossr50.listeners.WorldListener;
  61. import com.gmail.nossr50.locale.LocaleLoader;
  62. import com.gmail.nossr50.party.PartyManager;
  63. import com.gmail.nossr50.runnables.BleedTimer;
  64. import com.gmail.nossr50.runnables.ChunkletUnloader;
  65. import com.gmail.nossr50.runnables.SaveTimer;
  66. import com.gmail.nossr50.runnables.SkillMonitor;
  67. import com.gmail.nossr50.runnables.SpoutStart;
  68. import com.gmail.nossr50.skills.repair.RepairManager;
  69. import com.gmail.nossr50.skills.repair.RepairManagerFactory;
  70. import com.gmail.nossr50.skills.repair.Repairable;
  71. import com.gmail.nossr50.util.Database;
  72. import com.gmail.nossr50.util.Leaderboard;
  73. import com.gmail.nossr50.util.Metrics;
  74. import com.gmail.nossr50.util.Metrics.Graph;
  75. import com.gmail.nossr50.util.Users;
  76. import com.gmail.nossr50.util.blockmeta.ChunkletManager;
  77. import com.gmail.nossr50.util.blockmeta.ChunkletManagerFactory;
  78. public class mcMMO extends JavaPlugin {
  79. private final PlayerListener playerListener = new PlayerListener(this);
  80. private final BlockListener blockListener = new BlockListener(this);
  81. private final EntityListener entityListener = new EntityListener(this);
  82. private final WorldListener worldListener = new WorldListener();
  83. private final HardcoreListener hardcoreListener = new HardcoreListener();
  84. private HashMap<String, String> aliasMap = new HashMap<String, String>(); //Alias - Command
  85. private HashMap<Integer, String> tntTracker = new HashMap<Integer, String>();
  86. public static File versionFile;
  87. public static Database database;
  88. public static mcMMO p;
  89. public static ChunkletManager placeStore;
  90. public static RepairManager repairManager;
  91. /* Jar Stuff */
  92. public static File mcmmo;
  93. //File Paths
  94. public static String mainDirectory;
  95. public static String flatFileDirectory;
  96. public static String usersFile;
  97. public static String leaderboardDirectory;
  98. public static String modDirectory;
  99. //Spout Check
  100. public static boolean spoutEnabled;
  101. /**
  102. * Things to be run when the plugin is enabled.
  103. */
  104. @Override
  105. public void onEnable() {
  106. p = this;
  107. setupFilePaths();
  108. //Force the loading of config files
  109. Config configInstance = Config.getInstance();
  110. TreasuresConfig.getInstance();
  111. HiddenConfig.getInstance();
  112. List<Repairable> repairables = new ArrayList<Repairable>();
  113. if (configInstance.getToolModsEnabled()) {
  114. repairables.addAll(CustomToolsConfig.getInstance().getLoadedRepairables());
  115. }
  116. if (configInstance.getArmorModsEnabled()) {
  117. repairables.addAll(CustomArmorConfig.getInstance().getLoadedRepairables());
  118. }
  119. if (configInstance.getBlockModsEnabled()) {
  120. CustomBlocksConfig.getInstance();
  121. }
  122. //Load repair configs, make manager, and register them at this time
  123. RepairConfigManager rManager = new RepairConfigManager(this);
  124. repairables.addAll(rManager.getLoadedRepairables());
  125. repairManager = RepairManagerFactory.getRepairManager(repairables.size());
  126. repairManager.registerRepairables(repairables);
  127. if (!configInstance.getUseMySQL()) {
  128. Users.loadUsers();
  129. }
  130. PluginManager pluginManager = getServer().getPluginManager();
  131. //Register events
  132. pluginManager.registerEvents(playerListener, this);
  133. pluginManager.registerEvents(blockListener, this);
  134. pluginManager.registerEvents(entityListener, this);
  135. pluginManager.registerEvents(worldListener, this);
  136. if (configInstance.getHardcoreEnabled()) {
  137. pluginManager.registerEvents(hardcoreListener, this);
  138. }
  139. PluginDescriptionFile pdfFile = getDescription();
  140. //Setup the leaderboards
  141. if (configInstance.getUseMySQL()) {
  142. database = new Database(this);
  143. database.createStructure();
  144. }
  145. else {
  146. Leaderboard.makeLeaderboards();
  147. }
  148. for (Player player : getServer().getOnlinePlayers()) {
  149. Users.addUser(player); //In case of reload add all users back into PlayerProfile
  150. }
  151. System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
  152. BukkitScheduler scheduler = getServer().getScheduler();
  153. //Schedule Spout Activation 1 second after start-up
  154. scheduler.scheduleSyncDelayedTask(this, new SpoutStart(this), 20);
  155. //Periodic save timer (Saves every 10 minutes by default)
  156. scheduler.scheduleSyncRepeatingTask(this, new SaveTimer(this), 0, configInstance.getSaveInterval() * 1200);
  157. //Regen & Cooldown timer (Runs every second)
  158. scheduler.scheduleSyncRepeatingTask(this, new SkillMonitor(this), 0, 20);
  159. //Bleed timer (Runs every two seconds)
  160. scheduler.scheduleSyncRepeatingTask(this, new BleedTimer(), 0, 40);
  161. //Chunklet unloader (Runs every 20 seconds by default)
  162. scheduler.scheduleSyncRepeatingTask(this, new ChunkletUnloader(), 0, ChunkletUnloader.RUN_INTERVAL * 20);
  163. registerCommands();
  164. if (configInstance.getStatsTrackingEnabled()) {
  165. try {
  166. Metrics metrics = new Metrics(this);
  167. Graph graph = metrics.createGraph("Percentage of servers using timings");
  168. if (pluginManager.useTimings()) {
  169. graph.addPlotter(new Metrics.Plotter("Enabled") {
  170. @Override
  171. public int getValue() {
  172. return 1;
  173. }
  174. });
  175. }
  176. else {
  177. graph.addPlotter(new Metrics.Plotter("Disabled") {
  178. @Override
  179. public int getValue() {
  180. return 1;
  181. }
  182. });
  183. }
  184. metrics.start();
  185. }
  186. catch (IOException e) {
  187. System.out.println("Failed to submit stats.");
  188. }
  189. }
  190. // Get our ChunkletManager
  191. placeStore = ChunkletManagerFactory.getChunkletManager();
  192. }
  193. /**
  194. * Setup the various storage file paths
  195. */
  196. public void setupFilePaths() {
  197. mcmmo = getFile();
  198. mainDirectory = getDataFolder().getPath() + File.separator;
  199. flatFileDirectory = mainDirectory + "FlatFileStuff" + File.separator;
  200. usersFile = flatFileDirectory + "mcmmo.users";
  201. leaderboardDirectory = flatFileDirectory + "Leaderboards" + File.separator;
  202. modDirectory = mainDirectory + "ModConfigs" + File.separator;
  203. }
  204. /**
  205. * Get profile of the player by name.
  206. * </br>
  207. * This function is designed for API usage.
  208. *
  209. * @param playerName Name of player whose profile to get
  210. * @return the PlayerProfile object
  211. */
  212. public PlayerProfile getPlayerProfile(String playerName) {
  213. return Users.getProfile(playerName);
  214. }
  215. /**
  216. * Get profile of the player.
  217. * </br>
  218. * This function is designed for API usage.
  219. *
  220. * @param player player whose profile to get
  221. * @return the PlayerProfile object
  222. */
  223. public PlayerProfile getPlayerProfile(OfflinePlayer player) {
  224. return Users.getProfile(player);
  225. }
  226. /**
  227. * Get profile of the player.
  228. * </br>
  229. * This function is designed for API usage.
  230. *
  231. * @param player player whose profile to get
  232. * @return the PlayerProfile object
  233. */
  234. @Deprecated
  235. public PlayerProfile getPlayerProfile(Player player) {
  236. return Users.getProfile(player);
  237. }
  238. /**
  239. * Things to be run when the plugin is disabled.
  240. */
  241. @Override
  242. public void onDisable() {
  243. Users.saveAll(); //Make sure to save player information if the server shuts down
  244. PartyManager.getInstance().saveParties();
  245. getServer().getScheduler().cancelTasks(this); //This removes our tasks
  246. placeStore.saveAll(); //Save our metadata
  247. placeStore.cleanUp(); //Cleanup empty metadata stores
  248. //Remove other tasks BEFORE starting the Backup, or we just cancel it straight away.
  249. try {
  250. ZipLibrary.mcMMObackup();
  251. }
  252. catch (IOException e) {
  253. getLogger().severe(e.toString());
  254. }
  255. System.out.println("mcMMO was disabled."); //How informative!
  256. }
  257. /**
  258. * Register the commands.
  259. */
  260. private void registerCommands() {
  261. //Register aliases with the aliasmap (used in the playercommandpreprocessevent to ugly alias them to actual commands)
  262. //Skills commands
  263. aliasMap.put(LocaleLoader.getString("Acrobatics.SkillName").toLowerCase(), "acrobatics");
  264. aliasMap.put(LocaleLoader.getString("Archery.SkillName").toLowerCase(), "archery");
  265. aliasMap.put(LocaleLoader.getString("Axes.SkillName").toLowerCase(), "axes");
  266. aliasMap.put(LocaleLoader.getString("Excavation.SkillName").toLowerCase(), "excavation");
  267. aliasMap.put(LocaleLoader.getString("Fishing.SkillName").toLowerCase(), "fishing");
  268. aliasMap.put(LocaleLoader.getString("Herbalism.SkillName").toLowerCase(), "herbalism");
  269. aliasMap.put(LocaleLoader.getString("Mining.SkillName").toLowerCase(), "mining");
  270. aliasMap.put(LocaleLoader.getString("Repair.SkillName").toLowerCase(), "repair");
  271. aliasMap.put(LocaleLoader.getString("Swords.SkillName").toLowerCase(), "swords");
  272. aliasMap.put(LocaleLoader.getString("Taming.SkillName").toLowerCase(), "taming");
  273. aliasMap.put(LocaleLoader.getString("Unarmed.SkillName").toLowerCase(), "unarmed");
  274. aliasMap.put(LocaleLoader.getString("Woodcutting.SkillName").toLowerCase(), "woodcutting");
  275. //Register commands
  276. //Skills commands
  277. getCommand("acrobatics").setExecutor(new AcrobaticsCommand());
  278. getCommand("archery").setExecutor(new ArcheryCommand());
  279. getCommand("axes").setExecutor(new AxesCommand());
  280. getCommand("excavation").setExecutor(new ExcavationCommand());
  281. getCommand("fishing").setExecutor(new FishingCommand());
  282. getCommand("herbalism").setExecutor(new HerbalismCommand());
  283. getCommand("mining").setExecutor(new MiningCommand());
  284. getCommand("repair").setExecutor(new RepairCommand());
  285. getCommand("swords").setExecutor(new SwordsCommand());
  286. getCommand("taming").setExecutor(new TamingCommand());
  287. getCommand("unarmed").setExecutor(new UnarmedCommand());
  288. getCommand("woodcutting").setExecutor(new WoodcuttingCommand());
  289. Config configInstance = Config.getInstance();
  290. //mc* commands
  291. if (configInstance.getCommandMCRemoveEnabled()) {
  292. getCommand("mcremove").setExecutor(new McremoveCommand(this));
  293. }
  294. if (configInstance.getCommandMCAbilityEnabled()) {
  295. getCommand("mcability").setExecutor(new McabilityCommand());
  296. }
  297. if (configInstance.getCommandMCCEnabled()) {
  298. getCommand("mcc").setExecutor(new MccCommand());
  299. }
  300. if (configInstance.getCommandMCGodEnabled()) {
  301. getCommand("mcgod").setExecutor(new McgodCommand());
  302. }
  303. if (configInstance.getCommandmcMMOEnabled()) {
  304. getCommand("mcmmo").setExecutor(new McmmoCommand());
  305. }
  306. if (configInstance.getCommandMCRefreshEnabled()) {
  307. getCommand("mcrefresh").setExecutor(new McrefreshCommand(this));
  308. }
  309. if (configInstance.getCommandMCTopEnabled()) {
  310. getCommand("mctop").setExecutor(new MctopCommand());
  311. }
  312. if (configInstance.getCommandMCStatsEnabled()) {
  313. getCommand("mcstats").setExecutor(new McstatsCommand());
  314. }
  315. //Party commands
  316. if (configInstance.getCommandAcceptEnabled()) {
  317. getCommand("accept").setExecutor(new AcceptCommand(this));
  318. }
  319. if (configInstance.getCommandAdminChatAEnabled()) {
  320. getCommand("a").setExecutor(new ACommand(this));
  321. }
  322. if (configInstance.getCommandInviteEnabled()) {
  323. getCommand("invite").setExecutor(new InviteCommand(this));
  324. }
  325. if (configInstance.getCommandPartyEnabled()) {
  326. getCommand("party").setExecutor(new PartyCommand(this));
  327. }
  328. if (configInstance.getCommandPartyChatPEnabled()) {
  329. getCommand("p").setExecutor(new PCommand(this));
  330. }
  331. if (configInstance.getCommandPTPEnabled()) {
  332. getCommand("ptp").setExecutor(new PtpCommand(this));
  333. }
  334. //Other commands
  335. if (configInstance.getCommandAddXPEnabled()) {
  336. getCommand("addxp").setExecutor(new AddxpCommand(this));
  337. }
  338. if (configInstance.getCommandAddLevelsEnabled()) {
  339. getCommand("addlevels").setExecutor(new AddlevelsCommand(this));
  340. }
  341. if (configInstance.getCommandMmoeditEnabled()) {
  342. getCommand("mmoedit").setExecutor(new MmoeditCommand());
  343. }
  344. if (configInstance.getCommandInspectEnabled()) {
  345. getCommand("inspect").setExecutor(new InspectCommand(this));
  346. }
  347. if (configInstance.getCommandXPRateEnabled()) {
  348. getCommand("xprate").setExecutor(new XprateCommand(this));
  349. }
  350. getCommand("mmoupdate").setExecutor(new MmoupdateCommand(this));
  351. //Spout commands
  352. if (configInstance.getCommandXPLockEnabled()) {
  353. getCommand("xplock").setExecutor(new XplockCommand());
  354. }
  355. getCommand("mchud").setExecutor(new MchudCommand());
  356. }
  357. /**
  358. * Checks to see if the alias map contains the given key.
  359. *
  360. * @param command The command to check
  361. * @return true if the command is in the map, false otherwise
  362. */
  363. public boolean commandIsAliased(String command) {
  364. return aliasMap.containsKey(command);
  365. }
  366. /**
  367. * Get the alias of a given command.
  368. *
  369. * @param command The command to retrieve the alias of
  370. * @return the alias of the command
  371. */
  372. public String getCommandAlias(String command) {
  373. return aliasMap.get(command);
  374. }
  375. /**
  376. * Add a set of values to the TNT tracker.
  377. *
  378. * @param tntID The EntityID of the TNT
  379. * @param playerName The name of the detonating player
  380. */
  381. public void addToTNTTracker(int tntID, String playerName) {
  382. tntTracker.put(tntID, playerName);
  383. }
  384. /**
  385. * Check to see if a given TNT Entity is tracked.
  386. *
  387. * @param tntID The EntityID of the TNT
  388. * @return true if the TNT is being tracked, false otherwise
  389. */
  390. public boolean tntIsTracked(int tntID) {
  391. return tntTracker.containsKey(tntID);
  392. }
  393. /**
  394. * Get the player who detonated the TNT.
  395. *
  396. * @param tntID The EntityID of the TNT
  397. * @return the Player who detonated it
  398. */
  399. public Player getTNTPlayer(int tntID) {
  400. return getServer().getPlayer(tntTracker.get(tntID));
  401. }
  402. /**
  403. * Remove TNT from the tracker after it explodes.
  404. *
  405. * @param tntID The EntityID of the TNT
  406. */
  407. public void removeFromTNTTracker(int tntID) {
  408. tntTracker.remove(tntID);
  409. }
  410. }