mcMMO.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package com.gmail.nossr50;
  2. import com.gmail.nossr50.datatypes.PlayerProfile;
  3. import com.gmail.nossr50.commands.skills.*;
  4. import com.gmail.nossr50.commands.spout.*;
  5. import com.gmail.nossr50.commands.mc.*;
  6. import com.gmail.nossr50.commands.party.*;
  7. import com.gmail.nossr50.commands.general.*;
  8. import com.gmail.nossr50.config.*;
  9. import com.gmail.nossr50.runnables.*;
  10. import com.gmail.nossr50.listeners.mcBlockListener;
  11. import com.gmail.nossr50.listeners.mcEntityListener;
  12. import com.gmail.nossr50.listeners.mcPlayerListener;
  13. import com.gmail.nossr50.locale.mcLocale;
  14. import com.gmail.nossr50.party.Party;
  15. import java.io.BufferedInputStream;
  16. import java.io.BufferedWriter;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.HashMap;
  24. import org.bukkit.Bukkit;
  25. import org.bukkit.plugin.Plugin;
  26. import org.bukkit.plugin.PluginDescriptionFile;
  27. import org.bukkit.plugin.java.JavaPlugin;
  28. import org.bukkit.plugin.PluginManager;
  29. import org.bukkit.scheduler.BukkitScheduler;
  30. import org.bukkit.configuration.file.FileConfiguration;
  31. import org.bukkit.configuration.file.YamlConfiguration;
  32. import org.bukkit.entity.Entity;
  33. import org.bukkit.entity.Player;
  34. public class mcMMO extends JavaPlugin {
  35. public static String maindirectory = "plugins" + File.separator + "mcMMO";
  36. public static File file = new File(maindirectory + File.separator + "config.yml");
  37. public static File versionFile = new File(maindirectory + File.separator + "VERSION");
  38. private final mcPlayerListener playerListener = new mcPlayerListener(this);
  39. private final mcBlockListener blockListener = new mcBlockListener(this);
  40. private final mcEntityListener entityListener = new mcEntityListener(this);
  41. //Alias - Command
  42. public HashMap<String, String> aliasMap = new HashMap<String, String>();
  43. public HashMap<Entity, Integer> arrowTracker = new HashMap<Entity, Integer>();
  44. public HashMap<Integer, Player> tntTracker = new HashMap<Integer, Player>();
  45. public static Database database = null;
  46. //Config file stuff
  47. LoadProperties config;
  48. LoadTreasures config2;
  49. //Jar stuff
  50. public static File mcmmo;
  51. /**
  52. * Things to be run when the plugin is enabled.
  53. */
  54. public void onEnable() {
  55. final Plugin thisPlugin = this;
  56. mcmmo = this.getFile();
  57. new File(maindirectory).mkdir();
  58. if (!versionFile.exists()) {
  59. updateVersion();
  60. }
  61. else {
  62. String vnum = readVersion();
  63. //This will be changed to whatever version preceded when we actually need updater code.
  64. //Version 1.0.48 is the first to implement this, no checking before that version can be done.
  65. if (vnum.equalsIgnoreCase("1.0.48")) {
  66. updateFrom(1);
  67. }
  68. //Just add in more else if blocks for versions that need updater code. Increment the updateFrom age int as we do so.
  69. //Catch all for versions not matching and no specific code being needed
  70. else if (!vnum.equalsIgnoreCase(this.getDescription().getVersion())) {
  71. updateFrom(-1);
  72. }
  73. }
  74. this.config = new LoadProperties(this);
  75. this.config.load();
  76. this.config2 = new LoadTreasures(this);
  77. this.config2.load();
  78. Party.getInstance().loadParties();
  79. new Party(this);
  80. if (!LoadProperties.useMySQL) {
  81. Users.getInstance().loadUsers();
  82. }
  83. PluginManager pm = getServer().getPluginManager();
  84. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new SpoutStart(this), 20); //Schedule Spout Activation 1 second after start-up
  85. //Register events
  86. pm.registerEvents(playerListener, this);
  87. pm.registerEvents(blockListener, this);
  88. pm.registerEvents(entityListener, this);
  89. PluginDescriptionFile pdfFile = this.getDescription();
  90. //Setup the leaderboards
  91. if (LoadProperties.useMySQL) {
  92. database = new Database(this);
  93. database.createStructure();
  94. }
  95. else {
  96. Leaderboard.makeLeaderboards();
  97. }
  98. for (Player player : getServer().getOnlinePlayers()) {
  99. Users.addUser(player); //In case of reload add all users back into PlayerProfile
  100. }
  101. System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
  102. BukkitScheduler scheduler = getServer().getScheduler();
  103. //Periodic save timer (Saves every 10 minutes)
  104. scheduler.scheduleSyncRepeatingTask(this, new mcSaveTimer(this), 0, LoadProperties.saveInterval * 1200);
  105. //Regen & Cooldown timer (Runs every second)
  106. scheduler.scheduleSyncRepeatingTask(this, new mcTimer(this), 0, 20);
  107. //Bleed timer (Runs every two seconds)
  108. scheduler.scheduleSyncRepeatingTask(this, new mcBleedTimer(this), 0, 40);
  109. registerCommands();
  110. if (LoadProperties.statsTracking) {
  111. //Plugin Metrics running in a new thread
  112. new Thread(new Runnable() {
  113. public void run() {
  114. try {
  115. // create a new metrics object
  116. Metrics metrics = new Metrics();
  117. // 'this' in this context is the Plugin object
  118. metrics.beginMeasuringPlugin(thisPlugin);
  119. }
  120. catch (IOException e) {
  121. System.out.println("Failed to submit stats.");
  122. }
  123. }
  124. }).start();
  125. }
  126. }
  127. /**
  128. * Get profile of the player.
  129. * </br>
  130. * This function is designed for API usage.
  131. *
  132. * @param player Player whose profile to get
  133. * @return the PlayerProfile object
  134. */
  135. public static PlayerProfile getPlayerProfile(Player player) {
  136. return Users.getProfile(player);
  137. }
  138. /**
  139. * Things to be run when the plugin is disabled.
  140. */
  141. public void onDisable() {
  142. //Make sure to save player information if the server shuts down
  143. for (PlayerProfile x : Users.getProfiles().values()) {
  144. x.save();
  145. }
  146. Bukkit.getServer().getScheduler().cancelTasks(this); //This removes our tasks
  147. System.out.println("mcMMO was disabled."); //How informative!
  148. }
  149. /**
  150. * Register the commands.
  151. */
  152. private void registerCommands() {
  153. //Register aliases with the aliasmap (used in the playercommandpreprocessevent to ugly alias them to actual commands)
  154. //Skills commands
  155. aliasMap.put(mcLocale.getString("m.SkillAcrobatics").toLowerCase(), "acrobatics");
  156. aliasMap.put(mcLocale.getString("m.SkillArchery").toLowerCase(), "archery");
  157. aliasMap.put(mcLocale.getString("m.SkillAxes").toLowerCase(), "axes");
  158. aliasMap.put(mcLocale.getString("m.SkillExcavation").toLowerCase(), "excavation");
  159. aliasMap.put(mcLocale.getString("m.SkillFishing").toLowerCase(), "fishing");
  160. aliasMap.put(mcLocale.getString("m.SkillHerbalism").toLowerCase(), "herbalism");
  161. aliasMap.put(mcLocale.getString("m.SkillMining").toLowerCase(), "mining");
  162. aliasMap.put(mcLocale.getString("m.SkillRepair").toLowerCase(), "repair");
  163. aliasMap.put(mcLocale.getString("m.SkillSwords").toLowerCase(), "swords");
  164. aliasMap.put(mcLocale.getString("m.SkillTaming").toLowerCase(), "taming");
  165. aliasMap.put(mcLocale.getString("m.SkillUnarmed").toLowerCase(), "unarmed");
  166. aliasMap.put(mcLocale.getString("m.SkillWoodCutting").toLowerCase(), "woodcutting");
  167. //Register commands
  168. //Skills commands
  169. getCommand("acrobatics").setExecutor(new AcrobaticsCommand());
  170. getCommand("archery").setExecutor(new ArcheryCommand());
  171. getCommand("axes").setExecutor(new AxesCommand());
  172. getCommand("excavation").setExecutor(new ExcavationCommand());
  173. getCommand("fishing").setExecutor(new FishingCommand());
  174. getCommand("herbalism").setExecutor(new HerbalismCommand());
  175. getCommand("mining").setExecutor(new MiningCommand());
  176. getCommand("repair").setExecutor(new RepairCommand());
  177. getCommand("swords").setExecutor(new SwordsCommand());
  178. getCommand("taming").setExecutor(new TamingCommand());
  179. getCommand("unarmed").setExecutor(new UnarmedCommand());
  180. getCommand("woodcutting").setExecutor(new WoodcuttingCommand());
  181. //mc* commands
  182. if (LoadProperties.mcremoveEnable) {
  183. getCommand("mcremove").setExecutor(new McremoveCommand());
  184. }
  185. if (LoadProperties.mcabilityEnable) {
  186. getCommand("mcability").setExecutor(new McabilityCommand());
  187. }
  188. if (LoadProperties.mccEnable) {
  189. getCommand("mcc").setExecutor(new MccCommand());
  190. }
  191. if (LoadProperties.mcgodEnable) {
  192. getCommand("mcgod").setExecutor(new McgodCommand());
  193. }
  194. if (LoadProperties.mcmmoEnable) {
  195. getCommand("mcmmo").setExecutor(new McmmoCommand());
  196. }
  197. if (LoadProperties.mcrefreshEnable) {
  198. getCommand("mcrefresh").setExecutor(new McrefreshCommand(this));
  199. }
  200. if (LoadProperties.mctopEnable) {
  201. getCommand("mctop").setExecutor(new MctopCommand());
  202. }
  203. if (LoadProperties.mcstatsEnable) {
  204. getCommand("mcstats").setExecutor(new McstatsCommand());
  205. }
  206. //Party commands
  207. if (LoadProperties.acceptEnable) {
  208. getCommand("accept").setExecutor(new AcceptCommand());
  209. }
  210. if (LoadProperties.aEnable) {
  211. getCommand("a").setExecutor(new ACommand());
  212. }
  213. if (LoadProperties.inviteEnable) {
  214. getCommand("invite").setExecutor(new InviteCommand(this));
  215. }
  216. if (LoadProperties.partyEnable) {
  217. getCommand("party").setExecutor(new PartyCommand());
  218. }
  219. if (LoadProperties.pEnable) {
  220. getCommand("p").setExecutor(new PCommand());
  221. }
  222. if (LoadProperties.ptpEnable) {
  223. getCommand("ptp").setExecutor(new PtpCommand(this));
  224. }
  225. //Other commands
  226. if (LoadProperties.addxpEnable) {
  227. getCommand("addxp").setExecutor(new AddxpCommand(this));
  228. }
  229. if (LoadProperties.addlevelsEnable) {
  230. getCommand("addlevels").setExecutor(new AddlevelsCommand(this));
  231. }
  232. if (LoadProperties.mmoeditEnable) {
  233. getCommand("mmoedit").setExecutor(new MmoeditCommand());
  234. }
  235. if (LoadProperties.inspectEnable) {
  236. getCommand("inspect").setExecutor(new InspectCommand(this));
  237. }
  238. if (LoadProperties.xprateEnable) {
  239. getCommand("xprate").setExecutor(new XprateCommand());
  240. }
  241. getCommand("mmoupdate").setExecutor(new MmoupdateCommand());
  242. //Spout commands
  243. if (LoadProperties.xplockEnable) {
  244. getCommand("xplock").setExecutor(new XplockCommand());
  245. }
  246. getCommand("mchud").setExecutor(new MchudCommand());
  247. }
  248. /**
  249. * Update mcMMO from a given version
  250. * </p>
  251. * It is important to always assume that you are updating from the lowest possible version.
  252. * Thus, every block of updater code should be complete and self-contained; finishing all
  253. * SQL transactions and closing all file handlers, such that the next block of updater code
  254. * if called will handle updating as expected.
  255. *
  256. * @param age Specifies which updater code to run
  257. */
  258. public void updateFrom(int age) {
  259. //No updater code needed, just update the version.
  260. if (age == -1) {
  261. updateVersion();
  262. return;
  263. }
  264. //Updater code from age 1 goes here
  265. if (age <= 1) {
  266. //Since age 1 is an example for now, we will just let it do nothing.
  267. }
  268. //If we are updating from age 1 but we need more to reach age 2, this will run too.
  269. if (age <= 2) {
  270. }
  271. updateVersion();
  272. }
  273. /**
  274. * Update the version file.
  275. */
  276. public void updateVersion() {
  277. try {
  278. versionFile.createNewFile();
  279. BufferedWriter vout = new BufferedWriter(new FileWriter(versionFile));
  280. vout.write(this.getDescription().getVersion());
  281. vout.close();
  282. }
  283. catch (IOException ex) {
  284. ex.printStackTrace();
  285. }
  286. catch (SecurityException ex) {
  287. ex.printStackTrace();
  288. }
  289. }
  290. /**
  291. * Get the current mcMMO version.
  292. *
  293. * @return a String representing the current mcMMO version
  294. */
  295. public String readVersion() {
  296. byte[] buffer = new byte[(int) versionFile.length()];
  297. BufferedInputStream f = null;
  298. try {
  299. f = new BufferedInputStream(new FileInputStream(versionFile));
  300. f.read(buffer);
  301. }
  302. catch (FileNotFoundException ex) {
  303. ex.printStackTrace();
  304. }
  305. catch (IOException ex) {
  306. ex.printStackTrace();
  307. }
  308. finally {
  309. if (f != null) {
  310. try {
  311. f.close();
  312. }
  313. catch (IOException ignored) {}
  314. }
  315. }
  316. return new String(buffer);
  317. }
  318. /*
  319. * Boilerplate Custom Config Stuff
  320. */
  321. private FileConfiguration treasuresConfig = null;
  322. private File treasuresConfigFile = null;
  323. /**
  324. * Reload the Treasures.yml file.
  325. */
  326. public void reloadTreasuresConfig() {
  327. if (treasuresConfigFile == null) {
  328. treasuresConfigFile = new File(getDataFolder(), "treasures.yml");
  329. }
  330. treasuresConfig = YamlConfiguration.loadConfiguration(treasuresConfigFile);
  331. InputStream defConfigStream = getResource("treasures.yml"); // Look for defaults in the jar
  332. if (defConfigStream != null) {
  333. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
  334. treasuresConfig.setDefaults(defConfig);
  335. }
  336. }
  337. /**
  338. * Get the Treasures config information.
  339. *
  340. * @return the configuration object for treasures.yml
  341. */
  342. public FileConfiguration getTreasuresConfig() {
  343. if (treasuresConfig == null) {
  344. reloadTreasuresConfig();
  345. }
  346. return treasuresConfig;
  347. }
  348. /**
  349. * Save the Treasures config informtion.
  350. */
  351. public void saveTreasuresConfig() {
  352. if (treasuresConfig == null || treasuresConfigFile == null) {
  353. return;
  354. }
  355. try {
  356. treasuresConfig.save(treasuresConfigFile);
  357. }
  358. catch (IOException ex) {
  359. Bukkit.getLogger().severe("Could not save config to " + treasuresConfigFile + ex.toString());
  360. }
  361. }
  362. }