mcMMO.java 20 KB

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