mcMMO.java 14 KB

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