mcMMO.java 21 KB

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