mcMMO.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50;
  15. import com.gmail.nossr50.datatypes.PlayerProfile;
  16. import com.gmail.nossr50.datatypes.SkillType;
  17. import com.gmail.nossr50.commands.skills.*;
  18. import com.gmail.nossr50.commands.spout.*;
  19. import com.gmail.nossr50.commands.mc.*;
  20. import com.gmail.nossr50.commands.party.*;
  21. import com.gmail.nossr50.commands.general.*;
  22. import com.gmail.nossr50.config.*;
  23. import com.gmail.nossr50.runnables.*;
  24. import com.gmail.nossr50.spout.SpoutStuff;
  25. import com.gmail.nossr50.listeners.mcBlockListener;
  26. import com.gmail.nossr50.listeners.mcEntityListener;
  27. import com.gmail.nossr50.listeners.mcPlayerListener;
  28. import com.gmail.nossr50.locale.mcLocale;
  29. import com.gmail.nossr50.party.Party;
  30. import com.gmail.nossr50.skills.*;
  31. import org.bukkit.Bukkit;
  32. import java.io.BufferedInputStream;
  33. import java.io.BufferedReader;
  34. import java.io.BufferedWriter;
  35. import java.io.File;
  36. import java.io.FileInputStream;
  37. import java.io.FileNotFoundException;
  38. import java.io.FileReader;
  39. import java.io.FileWriter;
  40. import java.io.IOException;
  41. import java.util.ArrayDeque;
  42. import java.util.ArrayList;
  43. import java.util.HashMap;
  44. import java.util.logging.Level;
  45. import java.util.logging.Logger;
  46. import org.bukkit.plugin.PluginDescriptionFile;
  47. import org.bukkit.plugin.java.JavaPlugin;
  48. import org.bukkit.plugin.PluginManager;
  49. import org.bukkit.block.Block;
  50. import org.bukkit.entity.Player;
  51. import org.getspout.spoutapi.SpoutManager;
  52. import org.getspout.spoutapi.player.FileManager;
  53. public class mcMMO extends JavaPlugin
  54. {
  55. /*
  56. * I never expected mcMMO to get so popular!
  57. * Thanks for all the support for the mod
  58. * Thanks to the people who have worked on the code
  59. * Thanks to the donators who helped me out financially
  60. * Thanks to the server admins who use my mod :)
  61. *
  62. * This mod is open source, and its going to stay that way >:3
  63. *
  64. * Donate via paypal to nossr50@gmail.com (A million thanks to anyone that does!)
  65. */
  66. public static String maindirectory = "plugins" + File.separator + "mcMMO";
  67. File file = new File(maindirectory + File.separator + "config.yml");
  68. static File versionFile = new File(maindirectory + File.separator + "VERSION");
  69. public static final Logger log = Logger.getLogger("Minecraft");
  70. private final mcPlayerListener playerListener = new mcPlayerListener(this);
  71. private final mcBlockListener blockListener = new mcBlockListener(this);
  72. private final mcEntityListener entityListener = new mcEntityListener(this);
  73. private Runnable mcMMO_Timer = new mcTimer(this); //BLEED AND REGENERATION
  74. private Runnable ChangeDataValueTimer = new ChangeDataValueTimer(this); //R2 block place workaround
  75. //private Timer mcMMO_SpellTimer = new Timer(true);
  76. //Alias - Command
  77. public HashMap<String, String> aliasMap = new HashMap<String, String>();
  78. public static Database database = null;
  79. public Misc misc = new Misc(this);
  80. //Config file stuff
  81. LoadProperties config;
  82. //Jar stuff
  83. public static File mcmmo;
  84. //Queue for block data change for R2+ fix
  85. public ArrayDeque<Block> changeQueue = new ArrayDeque<Block>();
  86. public void onEnable()
  87. {
  88. mcmmo = this.getFile();
  89. new File(maindirectory).mkdir();
  90. if(!versionFile.exists())
  91. {
  92. updateVersion();
  93. } else
  94. {
  95. String vnum = readVersion();
  96. //This will be changed to whatever version preceded when we actually need updater code.
  97. //Version 1.0.48 is the first to implement this, no checking before that version can be done.
  98. if(vnum.equalsIgnoreCase("1.0.48")) {
  99. updateFrom(1);
  100. }
  101. //Just add in more else if blocks for versions that need updater code. Increment the updateFrom age int as we do so.
  102. //Catch all for versions not matching and no specific code being needed
  103. else if(!vnum.equalsIgnoreCase(this.getDescription().getVersion())) updateFrom(-1);
  104. }
  105. this.config = new LoadProperties(this);
  106. this.config.load();
  107. Party.getInstance().loadParties();
  108. new Party(this);
  109. if(!LoadProperties.useMySQL)
  110. Users.getInstance().loadUsers(); //Load Users file
  111. /*
  112. * REGISTER EVENTS
  113. */
  114. PluginManager pm = getServer().getPluginManager();
  115. if(pm.getPlugin("Spout") != null)
  116. LoadProperties.spoutEnabled = true;
  117. else
  118. LoadProperties.spoutEnabled = false;
  119. //Register events
  120. pm.registerEvents(playerListener, this);
  121. pm.registerEvents(blockListener, this);
  122. pm.registerEvents(entityListener, this);
  123. PluginDescriptionFile pdfFile = this.getDescription();
  124. mcPermissions.initialize(getServer());
  125. if(LoadProperties.useMySQL)
  126. {
  127. database = new Database(this);
  128. database.createStructure();
  129. } else
  130. Leaderboard.makeLeaderboards(); //Make the leaderboards
  131. for(Player player : getServer().getOnlinePlayers()){Users.addUser(player);} //In case of reload add all users back into PlayerProfile
  132. System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
  133. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, mcMMO_Timer, 0, 20);
  134. //R2+ block place fix
  135. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, ChangeDataValueTimer, 0, 10);
  136. registerCommands();
  137. //Spout Stuff
  138. if(LoadProperties.spoutEnabled)
  139. {
  140. SpoutStuff.setupSpoutConfigs();
  141. SpoutStuff.registerCustomEvent();
  142. SpoutStuff.extractFiles(); //Extract source materials
  143. FileManager FM = SpoutManager.getFileManager();
  144. FM.addToPreLoginCache(this, SpoutStuff.getFiles());
  145. /*
  146. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this,
  147. new Runnable() {
  148. @Override
  149. public void run() {
  150. mmoHelper.updateAll();
  151. }
  152. }, 20, 20);
  153. */
  154. }
  155. }
  156. public PlayerProfile getPlayerProfile(Player player)
  157. {
  158. return Users.getProfile(player);
  159. }
  160. public void checkXp(Player player, SkillType skillType)
  161. {
  162. if(skillType == SkillType.ALL)
  163. Skills.XpCheckAll(player);
  164. else
  165. Skills.XpCheckSkill(skillType, player);
  166. }
  167. public boolean inSameParty(Player playera, Player playerb)
  168. {
  169. if(Users.getProfile(playera).inParty() && Users.getProfile(playerb).inParty()){
  170. if(Users.getProfile(playera).getParty().equals(Users.getProfile(playerb).getParty())){
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. } else {
  176. return false;
  177. }
  178. }
  179. public ArrayList<String> getParties(){
  180. String location = "plugins/mcMMO/mcmmo.users";
  181. ArrayList<String> parties = new ArrayList<String>();
  182. try {
  183. //Open the users file
  184. FileReader file = new FileReader(location);
  185. BufferedReader in = new BufferedReader(file);
  186. String line = "";
  187. while((line = in.readLine()) != null)
  188. {
  189. String[] character = line.split(":");
  190. String theparty = null;
  191. //Party
  192. if(character.length > 3)
  193. theparty = character[3];
  194. if(!parties.contains(theparty))
  195. parties.add(theparty);
  196. }
  197. in.close();
  198. } catch (Exception e) {
  199. log.log(Level.SEVERE, "Exception while reading "
  200. + location + " (Are you sure you formatted it correctly?)", e);
  201. }
  202. return parties;
  203. }
  204. public static String getPartyName(Player player){
  205. PlayerProfile PP = Users.getProfile(player);
  206. return PP.getParty();
  207. }
  208. public static boolean inParty(Player player){
  209. PlayerProfile PP = Users.getProfile(player);
  210. return PP.inParty();
  211. }
  212. public void onDisable() {
  213. Bukkit.getServer().getScheduler().cancelTasks(this);
  214. System.out.println("mcMMO was disabled.");
  215. }
  216. private void registerCommands() {
  217. //Register aliases with the aliasmap (used in the playercommandpreprocessevent to ugly alias them to actual commands)
  218. //Skills commands
  219. aliasMap.put(mcLocale.getString("m.SkillAcrobatics").toLowerCase(), "acrobatics");
  220. aliasMap.put(mcLocale.getString("m.SkillArchery").toLowerCase(), "archery");
  221. aliasMap.put(mcLocale.getString("m.SkillAxes").toLowerCase(), "axes");
  222. aliasMap.put(mcLocale.getString("m.SkillExcavation").toLowerCase(), "excavation");
  223. aliasMap.put(mcLocale.getString("m.SkillFishing").toLowerCase(), "fishing");
  224. aliasMap.put(mcLocale.getString("m.SkillHerbalism").toLowerCase(), "herbalism");
  225. aliasMap.put(mcLocale.getString("m.SkillMining").toLowerCase(), "mining");
  226. aliasMap.put(mcLocale.getString("m.SkillRepair").toLowerCase(), "repair");
  227. aliasMap.put(mcLocale.getString("m.SkillSwords").toLowerCase(), "swords");
  228. aliasMap.put(mcLocale.getString("m.SkillTaming").toLowerCase(), "taming");
  229. aliasMap.put(mcLocale.getString("m.SkillUnarmed").toLowerCase(), "unarmed");
  230. aliasMap.put(mcLocale.getString("m.SkillWoodCutting").toLowerCase(), "woodcutting");
  231. //Register commands
  232. //Skills commands
  233. getCommand("acrobatics").setExecutor(new AcrobaticsCommand());
  234. getCommand("archery").setExecutor(new ArcheryCommand());
  235. getCommand("axes").setExecutor(new AxesCommand());
  236. getCommand("excavation").setExecutor(new ExcavationCommand());
  237. getCommand("fishing").setExecutor(new FishingCommand());
  238. getCommand("herbalism").setExecutor(new HerbalismCommand());
  239. getCommand("mining").setExecutor(new MiningCommand());
  240. getCommand("repair").setExecutor(new RepairCommand());
  241. getCommand("swords").setExecutor(new SwordsCommand());
  242. getCommand("taming").setExecutor(new TamingCommand());
  243. getCommand("unarmed").setExecutor(new UnarmedCommand());
  244. getCommand("woodcutting").setExecutor(new WoodcuttingCommand());
  245. //Mc* commands
  246. if(LoadProperties.mcabilityEnable) getCommand("mcability").setExecutor(new McabilityCommand());
  247. if(LoadProperties.mccEnable) getCommand("mcc").setExecutor(new MccCommand());
  248. if(LoadProperties.mcgodEnable) getCommand("mcgod").setExecutor(new McgodCommand());
  249. if(LoadProperties.mcmmoEnable) getCommand("mcmmo").setExecutor(new McmmoCommand());
  250. if(LoadProperties.mcrefreshEnable) getCommand("mcrefresh").setExecutor(new McrefreshCommand(this));
  251. if(LoadProperties.mctopEnable) getCommand("mctop").setExecutor(new MctopCommand());
  252. //Party commands
  253. if(LoadProperties.acceptEnable) getCommand("accept").setExecutor(new AcceptCommand());
  254. if(LoadProperties.aEnable) getCommand("a").setExecutor(new ACommand());
  255. if(LoadProperties.inviteEnable) getCommand("invite").setExecutor(new InviteCommand(this));
  256. if(LoadProperties.partyEnable) getCommand("party").setExecutor(new PartyCommand());
  257. if(LoadProperties.pEnable) getCommand("p").setExecutor(new PCommand());
  258. if(LoadProperties.ptpEnable) getCommand("ptp").setExecutor(new PtpCommand(this));
  259. //Other commands
  260. if(LoadProperties.addxpEnable) getCommand("addxp").setExecutor(new AddxpCommand(this));
  261. if(LoadProperties.clearmyspawnEnable) getCommand("clearmyspawn").setExecutor(new ClearmyspawnCommand());
  262. if(LoadProperties.mmoeditEnable) getCommand("mmoedit").setExecutor(new MmoeditCommand(this));
  263. getCommand("mmoupdate").setExecutor(new MmoupdateCommand());
  264. if(LoadProperties.myspawnEnable) getCommand("myspawn").setExecutor(new MyspawnCommand());
  265. if(LoadProperties.statsEnable) getCommand("stats").setExecutor(new StatsCommand());
  266. if(LoadProperties.whoisEnable) getCommand("whois").setExecutor(new WhoisCommand(this));
  267. if(LoadProperties.xprateEnable) getCommand("xprate").setExecutor(new XprateCommand());
  268. //Spout commands
  269. getCommand("mchud").setExecutor(new MchudCommand());
  270. if(LoadProperties.xplockEnable) getCommand("xplock").setExecutor(new XplockCommand());
  271. }
  272. /*
  273. * It is important to always assume that you are updating from the lowest possible version.
  274. * Thus, every block of updater code should be complete and self-contained; finishing all
  275. * SQL transactions and closing all file handlers, such that the next block of updater code
  276. * if called will handle updating as expected.
  277. */
  278. public void updateFrom(int age) {
  279. //No updater code needed, just update the version.
  280. if(age == -1) {
  281. updateVersion();
  282. return;
  283. }
  284. //Updater code from age 1 goes here
  285. if(age <= 1) {
  286. //Since age 1 is an example for now, we will just let it do nothing.
  287. }
  288. //If we are updating from age 1 but we need more to reach age 2, this will run too.
  289. if(age <= 2) {
  290. }
  291. updateVersion();
  292. }
  293. public void updateVersion() {
  294. try {
  295. versionFile.createNewFile();
  296. BufferedWriter vout = new BufferedWriter(new FileWriter(versionFile));
  297. vout.write(this.getDescription().getVersion());
  298. vout.close();
  299. } catch (IOException ex) {
  300. ex.printStackTrace();
  301. } catch (SecurityException ex) {
  302. ex.printStackTrace();
  303. }
  304. }
  305. public String readVersion() {
  306. byte[] buffer = new byte[(int) versionFile.length()];
  307. BufferedInputStream f = null;
  308. try {
  309. f = new BufferedInputStream(new FileInputStream(versionFile));
  310. f.read(buffer);
  311. } catch (FileNotFoundException ex) {
  312. ex.printStackTrace();
  313. } catch (IOException ex) {
  314. ex.printStackTrace();
  315. } finally {
  316. if (f != null) try { f.close(); } catch (IOException ignored) { }
  317. }
  318. return new String(buffer);
  319. }
  320. }