mcMMO.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. package com.gmail.nossr50;
  2. import com.gmail.nossr50.datatypes.PlayerProfile;
  3. import com.gmail.nossr50.datatypes.SkillType;
  4. import com.gmail.nossr50.commands.skills.*;
  5. import com.gmail.nossr50.commands.spout.*;
  6. import com.gmail.nossr50.commands.mc.*;
  7. import com.gmail.nossr50.commands.party.*;
  8. import com.gmail.nossr50.commands.general.*;
  9. import com.gmail.nossr50.config.*;
  10. import com.gmail.nossr50.runnables.*;
  11. import com.gmail.nossr50.skills.Skills;
  12. import com.gmail.nossr50.spout.SpoutStuff;
  13. import com.gmail.nossr50.listeners.mcBlockListener;
  14. import com.gmail.nossr50.listeners.mcEntityListener;
  15. import com.gmail.nossr50.listeners.mcPlayerListener;
  16. import com.gmail.nossr50.locale.mcLocale;
  17. import com.gmail.nossr50.party.Party;
  18. import java.io.BufferedInputStream;
  19. import java.io.BufferedReader;
  20. import java.io.BufferedWriter;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileNotFoundException;
  24. import java.io.FileReader;
  25. import java.io.FileWriter;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.util.ArrayDeque;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import org.bukkit.Bukkit;
  32. import org.bukkit.plugin.Plugin;
  33. import org.bukkit.plugin.PluginDescriptionFile;
  34. import org.bukkit.plugin.java.JavaPlugin;
  35. import org.bukkit.plugin.PluginManager;
  36. import org.bukkit.block.Block;
  37. import org.bukkit.configuration.file.FileConfiguration;
  38. import org.bukkit.configuration.file.YamlConfiguration;
  39. import org.bukkit.entity.Player;
  40. import org.getspout.spoutapi.SpoutManager;
  41. import org.getspout.spoutapi.player.FileManager;
  42. public class mcMMO extends JavaPlugin {
  43. public static String maindirectory = "plugins" + File.separator + "mcMMO";
  44. public static File file = new File(maindirectory + File.separator + "config.yml");
  45. public static File versionFile = new File(maindirectory + File.separator + "VERSION");
  46. private final mcPlayerListener playerListener = new mcPlayerListener(this);
  47. private final mcBlockListener blockListener = new mcBlockListener(this);
  48. private final mcEntityListener entityListener = new mcEntityListener(this);
  49. //Queue for block data change for R2+ fix
  50. public ArrayDeque<Block> changeQueue = new ArrayDeque<Block>();
  51. public ArrayDeque<Block> fastChangeQueue = new ArrayDeque<Block>();
  52. private Runnable mcMMO_Timer = new mcTimer(this); //BLEED AND REGENERATION
  53. private Runnable mcMMO_SaveTimer = new mcSaveTimer(this); //Periodic saving of Player Data
  54. private Runnable ChangeDataValueTimer = new ChangeDataValueTimer(changeQueue); //R2 block place workaround
  55. private Runnable FastChangeDataValueTimer = new ChangeDataValueTimer(fastChangeQueue); //R2 block place workaround for instant-break stuff
  56. //Alias - Command
  57. public HashMap<String, String> aliasMap = new HashMap<String, String>();
  58. public static Database database = null;
  59. public Misc misc = new Misc(this);
  60. //Config file stuff
  61. LoadProperties config;
  62. LoadTreasures config2;
  63. //Jar stuff
  64. public static File mcmmo;
  65. /**
  66. * Things to be run when the plugin is enabled.
  67. */
  68. public void onEnable() {
  69. final Plugin thisPlugin = this;
  70. mcmmo = this.getFile();
  71. new File(maindirectory).mkdir();
  72. if (!versionFile.exists()) {
  73. updateVersion();
  74. }
  75. else {
  76. String vnum = readVersion();
  77. //This will be changed to whatever version preceded when we actually need updater code.
  78. //Version 1.0.48 is the first to implement this, no checking before that version can be done.
  79. if (vnum.equalsIgnoreCase("1.0.48")) {
  80. updateFrom(1);
  81. }
  82. //Just add in more else if blocks for versions that need updater code. Increment the updateFrom age int as we do so.
  83. //Catch all for versions not matching and no specific code being needed
  84. else if (!vnum.equalsIgnoreCase(this.getDescription().getVersion())) {
  85. updateFrom(-1);
  86. }
  87. }
  88. this.config = new LoadProperties(this);
  89. this.config.load();
  90. this.config2 = new LoadTreasures(this);
  91. this.config2.load();
  92. Party.getInstance().loadParties();
  93. new Party(this);
  94. if (!LoadProperties.useMySQL) {
  95. Users.getInstance().loadUsers();
  96. }
  97. PluginManager pm = getServer().getPluginManager();
  98. if (pm.getPlugin("Spout") != null) {
  99. LoadProperties.spoutEnabled = true;
  100. }
  101. else {
  102. LoadProperties.spoutEnabled = false;
  103. }
  104. //Register events
  105. pm.registerEvents(playerListener, this);
  106. pm.registerEvents(blockListener, this);
  107. pm.registerEvents(entityListener, this);
  108. PluginDescriptionFile pdfFile = this.getDescription();
  109. //Setup the leaderboards
  110. if (LoadProperties.useMySQL) {
  111. database = new Database(this);
  112. database.createStructure();
  113. }
  114. else {
  115. Leaderboard.makeLeaderboards();
  116. }
  117. for (Player player : getServer().getOnlinePlayers()) {
  118. Users.addUser(player); //In case of reload add all users back into PlayerProfile
  119. }
  120. System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
  121. //Periodic save timer (Saves every 10 minutes)
  122. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, mcMMO_SaveTimer, 0, LoadProperties.saveInterval * 1200);
  123. //Bleed & Regen timer (Runs every 20 seconds)
  124. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, mcMMO_Timer, 0, 20);
  125. //R2+ block place fix
  126. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, ChangeDataValueTimer, 0, 10);
  127. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, FastChangeDataValueTimer, 0, 1);
  128. registerCommands();
  129. //Spout Stuff
  130. if (LoadProperties.spoutEnabled) {
  131. SpoutStuff.setupSpoutConfigs();
  132. SpoutStuff.registerCustomEvent();
  133. SpoutStuff.extractFiles(); //Extract source materials
  134. FileManager FM = SpoutManager.getFileManager();
  135. FM.addToPreLoginCache(this, SpoutStuff.getFiles());
  136. }
  137. if (LoadProperties.statsTracking) {
  138. //Plugin Metrics running in a new thread
  139. new Thread(new Runnable() {
  140. public void run() {
  141. try {
  142. // create a new metrics object
  143. Metrics metrics = new Metrics();
  144. // 'this' in this context is the Plugin object
  145. metrics.beginMeasuringPlugin(thisPlugin);
  146. }
  147. catch (IOException e) {
  148. System.out.println("Failed to submit stats.");
  149. }
  150. }
  151. }).start();
  152. }
  153. }
  154. /**
  155. * Get profile of the player.
  156. * </br>
  157. * This function is designed for API usage.
  158. *
  159. * @param player Player whose profile to get
  160. * @return the PlayerProfile object
  161. */
  162. public PlayerProfile getPlayerProfile(Player player) {
  163. return Users.getProfile(player);
  164. }
  165. /**
  166. * Check the XP of a player.
  167. * </br>
  168. * This function is designed for API usage.
  169. *
  170. * @param player
  171. * @param skillType
  172. */
  173. public void checkXp(Player player, SkillType skillType) {
  174. if (skillType == SkillType.ALL) {
  175. Skills.XpCheckAll(player);
  176. }
  177. else {
  178. Skills.XpCheckSkill(skillType, player);
  179. }
  180. }
  181. /**
  182. * Check if two players are in the same party.
  183. * </br>
  184. * This function is designed for API usage.
  185. *
  186. * @param playera The first player to check
  187. * @param playerb The second player to check
  188. * @return true if the two players are in the same party, false otherwise
  189. */
  190. public boolean inSameParty(Player playera, Player playerb) {
  191. if (Users.getProfile(playera).inParty() && Users.getProfile(playerb).inParty()) {
  192. if (Users.getProfile(playera).getParty().equals(Users.getProfile(playerb).getParty())) {
  193. return true;
  194. }
  195. else {
  196. return false;
  197. }
  198. }
  199. else {
  200. return false;
  201. }
  202. }
  203. /**
  204. * Get a list of all current party names.
  205. * </br>
  206. * This function is designed for API usage.
  207. *
  208. * @return the list of parties.
  209. */
  210. public ArrayList<String> getParties() {
  211. String location = "plugins/mcMMO/mcmmo.users";
  212. ArrayList<String> parties = new ArrayList<String>();
  213. try {
  214. //Open the users file
  215. FileReader file = new FileReader(location);
  216. BufferedReader in = new BufferedReader(file);
  217. String line = "";
  218. while((line = in.readLine()) != null) {
  219. String[] character = line.split(":");
  220. String theparty = null;
  221. //Party
  222. if (character.length > 3) {
  223. theparty = character[3];
  224. }
  225. if (!parties.contains(theparty)) {
  226. parties.add(theparty);
  227. }
  228. }
  229. in.close();
  230. }
  231. catch (Exception e) {
  232. Bukkit.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString());
  233. }
  234. return parties;
  235. }
  236. /**
  237. * Get the name of the party a player is in.
  238. * </br>
  239. * This function is designed for API usage.
  240. *
  241. * @param player The player to check the party name of
  242. * @return the name of the player's party
  243. */
  244. public static String getPartyName(Player player) {
  245. PlayerProfile PP = Users.getProfile(player);
  246. return PP.getParty();
  247. }
  248. /**
  249. * Checks if a player is in a party.
  250. * </br>
  251. * This function is designed for API usage.
  252. *
  253. * @param player The player to check
  254. * @return true if the player is in a party, false otherwise
  255. */
  256. public static boolean inParty(Player player) {
  257. PlayerProfile PP = Users.getProfile(player);
  258. return PP.inParty();
  259. }
  260. /**
  261. * Things to be run when the plugin is disabled.
  262. */
  263. public void onDisable() {
  264. //Make sure to save player information if the server shuts down
  265. for (Player x : Bukkit.getOnlinePlayers()) {
  266. Users.getProfile(x).save();
  267. }
  268. Bukkit.getServer().getScheduler().cancelTasks(this); //This removes our tasks
  269. System.out.println("mcMMO was disabled."); //How informative!
  270. }
  271. /**
  272. * Register the commands.
  273. */
  274. private void registerCommands() {
  275. //Register aliases with the aliasmap (used in the playercommandpreprocessevent to ugly alias them to actual commands)
  276. //Skills commands
  277. aliasMap.put(mcLocale.getString("m.SkillAcrobatics").toLowerCase(), "acrobatics");
  278. aliasMap.put(mcLocale.getString("m.SkillArchery").toLowerCase(), "archery");
  279. aliasMap.put(mcLocale.getString("m.SkillAxes").toLowerCase(), "axes");
  280. aliasMap.put(mcLocale.getString("m.SkillExcavation").toLowerCase(), "excavation");
  281. aliasMap.put(mcLocale.getString("m.SkillFishing").toLowerCase(), "fishing");
  282. aliasMap.put(mcLocale.getString("m.SkillHerbalism").toLowerCase(), "herbalism");
  283. aliasMap.put(mcLocale.getString("m.SkillMining").toLowerCase(), "mining");
  284. aliasMap.put(mcLocale.getString("m.SkillRepair").toLowerCase(), "repair");
  285. aliasMap.put(mcLocale.getString("m.SkillSwords").toLowerCase(), "swords");
  286. aliasMap.put(mcLocale.getString("m.SkillTaming").toLowerCase(), "taming");
  287. aliasMap.put(mcLocale.getString("m.SkillUnarmed").toLowerCase(), "unarmed");
  288. aliasMap.put(mcLocale.getString("m.SkillWoodCutting").toLowerCase(), "woodcutting");
  289. //Register commands
  290. //Skills commands
  291. getCommand("acrobatics").setExecutor(new AcrobaticsCommand());
  292. getCommand("archery").setExecutor(new ArcheryCommand());
  293. getCommand("axes").setExecutor(new AxesCommand());
  294. getCommand("excavation").setExecutor(new ExcavationCommand());
  295. getCommand("fishing").setExecutor(new FishingCommand());
  296. getCommand("herbalism").setExecutor(new HerbalismCommand());
  297. getCommand("mining").setExecutor(new MiningCommand());
  298. getCommand("repair").setExecutor(new RepairCommand());
  299. getCommand("swords").setExecutor(new SwordsCommand());
  300. getCommand("taming").setExecutor(new TamingCommand());
  301. getCommand("unarmed").setExecutor(new UnarmedCommand());
  302. getCommand("woodcutting").setExecutor(new WoodcuttingCommand());
  303. //mc* commands
  304. if (LoadProperties.mcremoveEnable) {
  305. getCommand("mcremove").setExecutor(new McremoveCommand());
  306. }
  307. if (LoadProperties.mcabilityEnable) {
  308. getCommand("mcability").setExecutor(new McabilityCommand());
  309. }
  310. if (LoadProperties.mccEnable) {
  311. getCommand("mcc").setExecutor(new MccCommand());
  312. }
  313. if (LoadProperties.mcgodEnable) {
  314. getCommand("mcgod").setExecutor(new McgodCommand());
  315. }
  316. if (LoadProperties.mcmmoEnable) {
  317. getCommand("mcmmo").setExecutor(new McmmoCommand());
  318. }
  319. if (LoadProperties.mcrefreshEnable) {
  320. getCommand("mcrefresh").setExecutor(new McrefreshCommand(this));
  321. }
  322. if (LoadProperties.mctopEnable) {
  323. getCommand("mctop").setExecutor(new MctopCommand());
  324. }
  325. if (LoadProperties.mcstatsEnable) {
  326. getCommand("mcstats").setExecutor(new McstatsCommand());
  327. }
  328. //Party commands
  329. if (LoadProperties.acceptEnable) {
  330. getCommand("accept").setExecutor(new AcceptCommand());
  331. }
  332. if (LoadProperties.aEnable) {
  333. getCommand("a").setExecutor(new ACommand());
  334. }
  335. if (LoadProperties.inviteEnable) {
  336. getCommand("invite").setExecutor(new InviteCommand(this));
  337. }
  338. if (LoadProperties.partyEnable) {
  339. getCommand("party").setExecutor(new PartyCommand());
  340. }
  341. if (LoadProperties.pEnable) {
  342. getCommand("p").setExecutor(new PCommand());
  343. }
  344. if (LoadProperties.ptpEnable) {
  345. getCommand("ptp").setExecutor(new PtpCommand(this));
  346. }
  347. //Other commands
  348. if (LoadProperties.addxpEnable) {
  349. getCommand("addxp").setExecutor(new AddxpCommand(this));
  350. }
  351. if (LoadProperties.addlevelsEnable) {
  352. getCommand("addlevels").setExecutor(new AddlevelsCommand(this));
  353. }
  354. if (LoadProperties.mmoeditEnable) {
  355. getCommand("mmoedit").setExecutor(new MmoeditCommand(this));
  356. }
  357. if (LoadProperties.inspectEnable) {
  358. getCommand("inspect").setExecutor(new InspectCommand(this));
  359. }
  360. if (LoadProperties.xprateEnable) {
  361. getCommand("xprate").setExecutor(new XprateCommand());
  362. }
  363. getCommand("mmoupdate").setExecutor(new MmoupdateCommand());
  364. //Spout commands
  365. if (LoadProperties.xplockEnable) {
  366. getCommand("xplock").setExecutor(new XplockCommand());
  367. }
  368. getCommand("mchud").setExecutor(new MchudCommand());
  369. }
  370. /**
  371. * Update mcMMO from a given version
  372. * </p>
  373. * It is important to always assume that you are updating from the lowest possible version.
  374. * Thus, every block of updater code should be complete and self-contained; finishing all
  375. * SQL transactions and closing all file handlers, such that the next block of updater code
  376. * if called will handle updating as expected.
  377. *
  378. * @param age Specifies which updater code to run
  379. */
  380. public void updateFrom(int age) {
  381. //No updater code needed, just update the version.
  382. if (age == -1) {
  383. updateVersion();
  384. return;
  385. }
  386. //Updater code from age 1 goes here
  387. if (age <= 1) {
  388. //Since age 1 is an example for now, we will just let it do nothing.
  389. }
  390. //If we are updating from age 1 but we need more to reach age 2, this will run too.
  391. if (age <= 2) {
  392. }
  393. updateVersion();
  394. }
  395. /**
  396. * Update the version file.
  397. */
  398. public void updateVersion() {
  399. try {
  400. versionFile.createNewFile();
  401. BufferedWriter vout = new BufferedWriter(new FileWriter(versionFile));
  402. vout.write(this.getDescription().getVersion());
  403. vout.close();
  404. }
  405. catch (IOException ex) {
  406. ex.printStackTrace();
  407. }
  408. catch (SecurityException ex) {
  409. ex.printStackTrace();
  410. }
  411. }
  412. /**
  413. * Get the current mcMMO version.
  414. *
  415. * @return a String representing the current mcMMO version
  416. */
  417. public String readVersion() {
  418. byte[] buffer = new byte[(int) versionFile.length()];
  419. BufferedInputStream f = null;
  420. try {
  421. f = new BufferedInputStream(new FileInputStream(versionFile));
  422. f.read(buffer);
  423. }
  424. catch (FileNotFoundException ex) {
  425. ex.printStackTrace();
  426. }
  427. catch (IOException ex) {
  428. ex.printStackTrace();
  429. }
  430. finally {
  431. if (f != null) {
  432. try {
  433. f.close();
  434. }
  435. catch (IOException ignored) {}
  436. }
  437. }
  438. return new String(buffer);
  439. }
  440. /*
  441. * Boilerplate Custom Config Stuff
  442. */
  443. private FileConfiguration treasuresConfig = null;
  444. private File treasuresConfigFile = null;
  445. /**
  446. * Reload the Treasures.yml file.
  447. */
  448. public void reloadTreasuresConfig() {
  449. if (treasuresConfigFile == null) {
  450. treasuresConfigFile = new File(getDataFolder(), "treasures.yml");
  451. }
  452. treasuresConfig = YamlConfiguration.loadConfiguration(treasuresConfigFile);
  453. InputStream defConfigStream = getResource("treasures.yml"); // Look for defaults in the jar
  454. if (defConfigStream != null) {
  455. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
  456. treasuresConfig.setDefaults(defConfig);
  457. }
  458. }
  459. /**
  460. * Get the Treasures config information.
  461. *
  462. * @return the configuration object for treasures.yml
  463. */
  464. public FileConfiguration getTreasuresConfig() {
  465. if (treasuresConfig == null) {
  466. reloadTreasuresConfig();
  467. }
  468. return treasuresConfig;
  469. }
  470. /**
  471. * Save the Treasures config informtion.
  472. */
  473. public void saveTreasuresConfig() {
  474. if (treasuresConfig == null || treasuresConfigFile == null) {
  475. return;
  476. }
  477. try {
  478. treasuresConfig.save(treasuresConfigFile);
  479. }
  480. catch (IOException ex) {
  481. Bukkit.getLogger().severe("Could not save config to " + treasuresConfigFile + ex.toString());
  482. }
  483. }
  484. }