mcMMO.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package com.gmail.nossr50;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import net.shatteredlands.shatt.backup.ZipLibrary;
  10. import org.bukkit.OfflinePlayer;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.plugin.PluginDescriptionFile;
  14. import org.bukkit.plugin.PluginManager;
  15. import org.bukkit.plugin.java.JavaPlugin;
  16. import org.bukkit.scheduler.BukkitScheduler;
  17. import org.mcstats.Metrics;
  18. import org.mcstats.Metrics.Graph;
  19. import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManager;
  20. import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManagerFactory;
  21. import com.gmail.nossr50.chat.commands.ACommand;
  22. import com.gmail.nossr50.chat.commands.PCommand;
  23. import com.gmail.nossr50.commands.CommandRegistrationHelper;
  24. import com.gmail.nossr50.commands.player.MccCommand;
  25. import com.gmail.nossr50.commands.player.McmmoCommand;
  26. import com.gmail.nossr50.commands.player.McrankCommand;
  27. import com.gmail.nossr50.commands.player.McstatsCommand;
  28. import com.gmail.nossr50.commands.player.MctopCommand;
  29. import com.gmail.nossr50.config.AdvancedConfig;
  30. import com.gmail.nossr50.config.Config;
  31. import com.gmail.nossr50.config.HiddenConfig;
  32. import com.gmail.nossr50.config.TreasuresConfig;
  33. import com.gmail.nossr50.database.Database;
  34. import com.gmail.nossr50.database.commands.McpurgeCommand;
  35. import com.gmail.nossr50.database.commands.McremoveCommand;
  36. import com.gmail.nossr50.database.commands.MmoupdateCommand;
  37. import com.gmail.nossr50.database.runnables.UserPurgeTask;
  38. import com.gmail.nossr50.datatypes.PlayerProfile;
  39. import com.gmail.nossr50.listeners.BlockListener;
  40. import com.gmail.nossr50.listeners.EntityListener;
  41. import com.gmail.nossr50.listeners.HardcoreListener;
  42. import com.gmail.nossr50.listeners.InventoryListener;
  43. import com.gmail.nossr50.listeners.PlayerListener;
  44. import com.gmail.nossr50.listeners.WorldListener;
  45. import com.gmail.nossr50.mods.config.CustomArmorConfig;
  46. import com.gmail.nossr50.mods.config.CustomBlocksConfig;
  47. import com.gmail.nossr50.mods.config.CustomToolsConfig;
  48. import com.gmail.nossr50.party.PartyManager;
  49. import com.gmail.nossr50.party.commands.PartyCommand;
  50. import com.gmail.nossr50.party.commands.PtpCommand;
  51. import com.gmail.nossr50.runnables.MobStoreCleaner;
  52. import com.gmail.nossr50.runnables.SaveTimer;
  53. import com.gmail.nossr50.skills.repair.RepairManager;
  54. import com.gmail.nossr50.skills.repair.RepairManagerFactory;
  55. import com.gmail.nossr50.skills.repair.Repairable;
  56. import com.gmail.nossr50.skills.repair.config.RepairConfigManager;
  57. import com.gmail.nossr50.skills.runnables.BleedTimer;
  58. import com.gmail.nossr50.skills.runnables.PartyAutoKick;
  59. import com.gmail.nossr50.skills.runnables.SkillMonitor;
  60. import com.gmail.nossr50.spout.SpoutConfig;
  61. import com.gmail.nossr50.spout.SpoutTools;
  62. import com.gmail.nossr50.spout.commands.MchudCommand;
  63. import com.gmail.nossr50.spout.commands.XplockCommand;
  64. import com.gmail.nossr50.util.Anniversary;
  65. import com.gmail.nossr50.util.Leaderboard;
  66. import com.gmail.nossr50.util.Users;
  67. public class mcMMO extends JavaPlugin {
  68. private final PlayerListener playerListener = new PlayerListener(this);
  69. private final BlockListener blockListener = new BlockListener(this);
  70. private final EntityListener entityListener = new EntityListener(this);
  71. private final InventoryListener inventoryListener = new InventoryListener(this);
  72. private final WorldListener worldListener = new WorldListener();
  73. private final HardcoreListener hardcoreListener = new HardcoreListener();
  74. private HashMap<Integer, String> tntTracker = new HashMap<Integer, String>();
  75. private HashMap<Block, String> furnaceTracker = new HashMap<Block, String>();
  76. public static mcMMO p;
  77. public static ChunkManager placeStore;
  78. public static RepairManager repairManager;
  79. // Jar Stuff
  80. public static File mcmmo;
  81. // File Paths
  82. private static String mainDirectory;
  83. private static String flatFileDirectory;
  84. private static String usersFile;
  85. private static String modDirectory;
  86. // Spout Check
  87. public static boolean spoutEnabled = false;
  88. // XP Event Check
  89. private boolean xpEventEnabled = false;
  90. /**
  91. * Things to be run when the plugin is enabled.
  92. */
  93. @Override
  94. public void onEnable() {
  95. p = this;
  96. setupFilePaths();
  97. // Check for Spout
  98. if (getServer().getPluginManager().isPluginEnabled("Spout")) {
  99. spoutEnabled = true;
  100. SpoutConfig.getInstance();
  101. SpoutTools.setupSpoutConfigs();
  102. SpoutTools.registerCustomEvent();
  103. SpoutTools.preCacheFiles();
  104. SpoutTools.reloadSpoutPlayers(); // Handle spout players after a /reload
  105. }
  106. // Force the loading of config files
  107. Config configInstance = Config.getInstance();
  108. TreasuresConfig.getInstance();
  109. HiddenConfig.getInstance();
  110. AdvancedConfig.getInstance();
  111. PartyManager.loadParties();
  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. // Check if Repair Anvil and Salvage Anvil have different itemID's
  128. if (configInstance.getSalvageAnvilId() == configInstance.getRepairAnvilId()) {
  129. getLogger().warning("Can't use the same itemID for Repair/Salvage Anvils!");
  130. }
  131. if (!configInstance.getUseMySQL()) {
  132. Users.loadUsers();
  133. }
  134. PluginManager pluginManager = getServer().getPluginManager();
  135. // Register events
  136. pluginManager.registerEvents(playerListener, this);
  137. pluginManager.registerEvents(blockListener, this);
  138. pluginManager.registerEvents(entityListener, this);
  139. pluginManager.registerEvents(inventoryListener, this);
  140. pluginManager.registerEvents(worldListener, this);
  141. if (configInstance.getHardcoreEnabled()) {
  142. pluginManager.registerEvents(hardcoreListener, this);
  143. }
  144. PluginDescriptionFile pdfFile = getDescription();
  145. // Setup the leader boards
  146. if (configInstance.getUseMySQL()) {
  147. // TODO: Why do we have to check for a connection that hasn't be made yet?
  148. Database.checkConnected();
  149. Database.createStructure();
  150. }
  151. else {
  152. Leaderboard.updateLeaderboards();
  153. }
  154. for (Player player : getServer().getOnlinePlayers()) {
  155. Users.addUser(player); // In case of reload add all users back into PlayerProfile
  156. }
  157. getLogger().info("Version " + pdfFile.getVersion() + " is enabled!");
  158. BukkitScheduler scheduler = getServer().getScheduler();
  159. // Periodic save timer (Saves every 10 minutes by default)
  160. scheduler.scheduleSyncRepeatingTask(this, new SaveTimer(), 0, configInstance.getSaveInterval() * 1200);
  161. // Regen & Cooldown timer (Runs every second)
  162. scheduler.scheduleSyncRepeatingTask(this, new SkillMonitor(), 0, 20);
  163. // Bleed timer (Runs every two seconds)
  164. scheduler.scheduleSyncRepeatingTask(this, new BleedTimer(), 0, 40);
  165. // Old & Powerless User remover
  166. int purgeInterval = Config.getInstance().getPurgeInterval();
  167. if (purgeInterval == 0) {
  168. scheduler.scheduleSyncDelayedTask(this, new UserPurgeTask(), 40); // Start 2 seconds after startup.
  169. }
  170. else if (purgeInterval > 0) {
  171. scheduler.scheduleSyncRepeatingTask(this, new UserPurgeTask(), 0, purgeInterval * 60L * 60L * 20L);
  172. }
  173. // Automatically remove old members from parties
  174. long kickInterval = Config.getInstance().getAutoPartyKickInterval();
  175. if (kickInterval == 0) {
  176. scheduler.scheduleSyncDelayedTask(this, new PartyAutoKick(), 40); // Start 2 seconds after startup.
  177. }
  178. else if (kickInterval > 0) {
  179. scheduler.scheduleSyncRepeatingTask(this, new PartyAutoKick(), 0, kickInterval * 60L * 60L * 20L);
  180. }
  181. registerCommands();
  182. if (configInstance.getStatsTrackingEnabled()) {
  183. try {
  184. Metrics metrics = new Metrics(this);
  185. // Timings Graph
  186. Graph timingsGraph = metrics.createGraph("Percentage of servers using timings");
  187. if (pluginManager.useTimings()) {
  188. timingsGraph.addPlotter(new Metrics.Plotter("Enabled") {
  189. @Override
  190. public int getValue() {
  191. return 1;
  192. }
  193. });
  194. }
  195. else {
  196. timingsGraph.addPlotter(new Metrics.Plotter("Disabled") {
  197. @Override
  198. public int getValue() {
  199. return 1;
  200. }
  201. });
  202. }
  203. // Donut Version Graph
  204. Graph versionDonutGraph = metrics.createGraph("Donut Version");
  205. boolean haveVersionInformation = false;
  206. boolean isOfficialBuild = false;
  207. String officialKey = "e14cfacdd442a953343ebd8529138680";
  208. String version = getDescription().getVersion();
  209. InputStreamReader isr = new InputStreamReader(getResource(".jenkins"));
  210. BufferedReader br = new BufferedReader(isr);
  211. char[] key = new char[32];
  212. br.read(key);
  213. if (officialKey.equals(String.valueOf(key)))
  214. {
  215. isOfficialBuild = true;
  216. }
  217. if (version.contains("-")) {
  218. String majorVersion = version.substring(0, version.indexOf("-"));
  219. String subVersion = "";
  220. if (isOfficialBuild) {
  221. int startIndex = version.indexOf("-");
  222. if (version.substring(startIndex + 1).contains("-")) {
  223. subVersion = version.substring(startIndex, version.indexOf("-", startIndex + 1));
  224. } else {
  225. subVersion = "-release";
  226. }
  227. } else {
  228. subVersion = "-custom";
  229. }
  230. version = majorVersion + "~=~" + subVersion;
  231. haveVersionInformation = true;
  232. } else {
  233. haveVersionInformation = false;
  234. }
  235. if (haveVersionInformation) {
  236. versionDonutGraph.addPlotter(new Metrics.Plotter(version) {
  237. @Override
  238. public int getValue() {
  239. return 1;
  240. }
  241. });
  242. }
  243. // Official v Custom build Graph
  244. Graph officialGraph = metrics.createGraph("Built by official ci");
  245. if (isOfficialBuild) {
  246. officialGraph.addPlotter(new Metrics.Plotter("Yes") {
  247. @Override
  248. public int getValue() {
  249. return 1;
  250. }
  251. });
  252. }
  253. else {
  254. officialGraph.addPlotter(new Metrics.Plotter("No") {
  255. @Override
  256. public int getValue() {
  257. return 1;
  258. }
  259. });
  260. }
  261. // Chunkmeta enabled Graph
  262. Graph chunkmetaGraph = metrics.createGraph("Uses Chunkmeta");
  263. if (HiddenConfig.getInstance().getChunkletsEnabled()) {
  264. chunkmetaGraph.addPlotter(new Metrics.Plotter("Yes") {
  265. @Override
  266. public int getValue() {
  267. return 1;
  268. }
  269. });
  270. }
  271. else {
  272. chunkmetaGraph.addPlotter(new Metrics.Plotter("No") {
  273. @Override
  274. public int getValue() {
  275. return 1;
  276. }
  277. });
  278. }
  279. // Storage method Graph
  280. Graph storageGraph = metrics.createGraph("Storage method");
  281. if (configInstance.getUseMySQL()) {
  282. storageGraph.addPlotter(new Metrics.Plotter("SQL") {
  283. @Override
  284. public int getValue() {
  285. return 1;
  286. }
  287. });
  288. }
  289. else {
  290. storageGraph.addPlotter(new Metrics.Plotter("Flatfile") {
  291. @Override
  292. public int getValue() {
  293. return 1;
  294. }
  295. });
  296. }
  297. metrics.start();
  298. }
  299. catch (IOException e) {
  300. System.out.println("Failed to submit stats.");
  301. }
  302. }
  303. placeStore = ChunkManagerFactory.getChunkManager(); // Get our ChunkletManager
  304. new MobStoreCleaner(); // Automatically starts and stores itself
  305. Anniversary.createAnniversaryFile(); // Create Anniversary files
  306. }
  307. /**
  308. * Setup the various storage file paths
  309. */
  310. public void setupFilePaths() {
  311. mcmmo = getFile();
  312. mainDirectory = getDataFolder().getPath() + File.separator;
  313. flatFileDirectory = mainDirectory + "FlatFileStuff" + File.separator;
  314. usersFile = flatFileDirectory + "mcmmo.users";
  315. modDirectory = mainDirectory + "ModConfigs" + File.separator;
  316. }
  317. /**
  318. * Get profile of the player by name.
  319. * </br>
  320. * This function is designed for API usage.
  321. *
  322. * @param playerName Name of player whose profile to get
  323. * @return the PlayerProfile object
  324. */
  325. public PlayerProfile getPlayerProfile(String playerName) {
  326. return Users.getPlayer(playerName).getProfile();
  327. }
  328. /**
  329. * Get profile of the player.
  330. * </br>
  331. * This function is designed for API usage.
  332. *
  333. * @param player player whose profile to get
  334. * @return the PlayerProfile object
  335. */
  336. public PlayerProfile getPlayerProfile(OfflinePlayer player) {
  337. return Users.getPlayer(player.getName()).getProfile();
  338. }
  339. /**
  340. * Get profile of the player.
  341. * </br>
  342. * This function is designed for API usage.
  343. *
  344. * @param player player whose profile to get
  345. * @return the PlayerProfile object
  346. */
  347. @Deprecated
  348. public PlayerProfile getPlayerProfile(Player player) {
  349. return Users.getProfile(player);
  350. }
  351. /**
  352. * Things to be run when the plugin is disabled.
  353. */
  354. @Override
  355. public void onDisable() {
  356. Users.saveAll(); // Make sure to save player information if the server shuts down
  357. PartyManager.saveParties();
  358. getServer().getScheduler().cancelTasks(this); // This removes our tasks
  359. placeStore.saveAll(); // Save our metadata
  360. placeStore.cleanUp(); // Cleanup empty metadata stores
  361. // Remove other tasks BEFORE starting the Backup, or we just cancel it straight away.
  362. try {
  363. ZipLibrary.mcMMObackup();
  364. }
  365. catch (IOException e) {
  366. getLogger().severe(e.toString());
  367. }
  368. Anniversary.saveAnniversaryFiles();
  369. getLogger().info("Was disabled."); //How informative!
  370. }
  371. /**
  372. * Register the commands.
  373. */
  374. private void registerCommands() {
  375. CommandRegistrationHelper.registerSkillCommands();
  376. // mc* commands
  377. getCommand("mcpurge").setExecutor(new McpurgeCommand());
  378. getCommand("mcremove").setExecutor(new McremoveCommand());
  379. CommandRegistrationHelper.registerMcabilityCommand();
  380. getCommand("mcc").setExecutor(new MccCommand());
  381. CommandRegistrationHelper.registerMcgodCommand();
  382. getCommand("mcmmo").setExecutor(new McmmoCommand());
  383. CommandRegistrationHelper.registerMcrefreshCommand();
  384. getCommand("mctop").setExecutor(new MctopCommand());
  385. getCommand("mcrank").setExecutor(new McrankCommand());
  386. getCommand("mcstats").setExecutor(new McstatsCommand());
  387. // Party commands
  388. getCommand("a").setExecutor(new ACommand());
  389. getCommand("party").setExecutor(new PartyCommand());
  390. getCommand("p").setExecutor(new PCommand(this));
  391. getCommand("ptp").setExecutor(new PtpCommand(this));
  392. // Other commands
  393. CommandRegistrationHelper.registerAddxpCommand();
  394. CommandRegistrationHelper.registerAddlevelsCommand();
  395. CommandRegistrationHelper.registerMmoeditCommand();
  396. CommandRegistrationHelper.registerInspectCommand();
  397. CommandRegistrationHelper.registerXprateCommand();
  398. getCommand("mmoupdate").setExecutor(new MmoupdateCommand());
  399. CommandRegistrationHelper.registerSkillresetCommand();
  400. // Spout commands
  401. getCommand("xplock").setExecutor(new XplockCommand());
  402. getCommand("mchud").setExecutor(new MchudCommand());
  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 void addToOpenFurnaceTracker(Block furnace, String playerName) {
  440. furnaceTracker.put(furnace, playerName);
  441. }
  442. public boolean furnaceIsTracked(Block furnace) {
  443. return furnaceTracker.containsKey(furnace);
  444. }
  445. public void removeFromFurnaceTracker(Block furnace) {
  446. furnaceTracker.remove(furnace);
  447. }
  448. public Player getFurnacePlayer(Block furnace) {
  449. return getServer().getPlayer(furnaceTracker.get(furnace));
  450. }
  451. public static String getMainDirectory() {
  452. return mainDirectory;
  453. }
  454. public static String getFlatFileDirectory() {
  455. return flatFileDirectory;
  456. }
  457. public static String getUsersFilePath() {
  458. return usersFile;
  459. }
  460. public static String getModDirectory() {
  461. return modDirectory;
  462. }
  463. public boolean isXPEventEnabled() {
  464. return xpEventEnabled;
  465. }
  466. public void setXPEventEnabled(boolean enabled) {
  467. this.xpEventEnabled = enabled;
  468. }
  469. public void toggleXpEventEnabled() {
  470. xpEventEnabled = !xpEventEnabled;
  471. }
  472. }