mcMMO.java 20 KB

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