mcMMO.java 14 KB

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