mcMMO.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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;
  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. this.config = new LoadProperties(this);
  109. this.config.load();
  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. //Register commands
  238. //Skills commands
  239. getCommand("acrobatics").setExecutor(new AcrobaticsCommand());
  240. getCommand("archery").setExecutor(new ArcheryCommand());
  241. getCommand("axes").setExecutor(new AxesCommand());
  242. getCommand("excavation").setExecutor(new ExcavationCommand());
  243. getCommand("fishing").setExecutor(new FishingCommand());
  244. getCommand("herbalism").setExecutor(new HerbalismCommand());
  245. getCommand("mining").setExecutor(new MiningCommand());
  246. getCommand("repair").setExecutor(new RepairCommand());
  247. getCommand("swords").setExecutor(new SwordsCommand());
  248. getCommand("taming").setExecutor(new TamingCommand());
  249. getCommand("unarmed").setExecutor(new UnarmedCommand());
  250. getCommand("woodcutting").setExecutor(new WoodcuttingCommand());
  251. //Mc* commands
  252. if(LoadProperties.mcabilityEnable) getCommand("mcability").setExecutor(new McabilityCommand());
  253. if(LoadProperties.mccEnable) getCommand("mcc").setExecutor(new MccCommand());
  254. if(LoadProperties.mcgodEnable) getCommand("mcgod").setExecutor(new McgodCommand());
  255. if(LoadProperties.mcmmoEnable) getCommand("mcmmo").setExecutor(new McmmoCommand());
  256. if(LoadProperties.mcrefreshEnable) getCommand("mcrefresh").setExecutor(new McrefreshCommand(this));
  257. if(LoadProperties.mctopEnable) getCommand("mctop").setExecutor(new MctopCommand());
  258. //Party commands
  259. if(LoadProperties.acceptEnable) getCommand("accept").setExecutor(new AcceptCommand());
  260. if(LoadProperties.aEnable) getCommand("a").setExecutor(new ACommand());
  261. if(LoadProperties.inviteEnable) getCommand("invite").setExecutor(new InviteCommand(this));
  262. if(LoadProperties.partyEnable) getCommand("party").setExecutor(new PartyCommand());
  263. if(LoadProperties.pEnable) getCommand("p").setExecutor(new PCommand());
  264. if(LoadProperties.ptpEnable) getCommand("ptp").setExecutor(new PtpCommand(this));
  265. //Other commands
  266. if(LoadProperties.addxpEnable) getCommand("addxp").setExecutor(new AddxpCommand(this));
  267. if(LoadProperties.clearmyspawnEnable) getCommand("clearmyspawn").setExecutor(new ClearmyspawnCommand());
  268. if(LoadProperties.mmoeditEnable) getCommand("mmoedit").setExecutor(new MmoeditCommand(this));
  269. getCommand("mmoupdate").setExecutor(new MmoupdateCommand());
  270. if(LoadProperties.myspawnEnable) getCommand("myspawn").setExecutor(new MyspawnCommand());
  271. if(LoadProperties.statsEnable) getCommand("stats").setExecutor(new StatsCommand());
  272. if(LoadProperties.whoisEnable) getCommand("whois").setExecutor(new WhoisCommand(this));
  273. if(LoadProperties.xprateEnable) getCommand("xprate").setExecutor(new XprateCommand());
  274. //Spout commands
  275. getCommand("mchud").setExecutor(new MchudCommand());
  276. if(LoadProperties.xplockEnable) getCommand("xplock").setExecutor(new XplockCommand());
  277. }
  278. /*
  279. * It is important to always assume that you are updating from the lowest possible version.
  280. * Thus, every block of updater code should be complete and self-contained; finishing all
  281. * SQL transactions and closing all file handlers, such that the next block of updater code
  282. * if called will handle updating as expected.
  283. */
  284. public void updateFrom(int age) {
  285. //No updater code needed, just update the version.
  286. if(age == -1) {
  287. updateVersion();
  288. return;
  289. }
  290. //Updater code from age 1 goes here
  291. if(age <= 1) {
  292. //Since age 1 is an example for now, we will just let it do nothing.
  293. }
  294. //If we are updating from age 1 but we need more to reach age 2, this will run too.
  295. if(age <= 2) {
  296. }
  297. updateVersion();
  298. }
  299. public void updateVersion() {
  300. try {
  301. versionFile.createNewFile();
  302. BufferedWriter vout = new BufferedWriter(new FileWriter(versionFile));
  303. vout.write(this.getDescription().getVersion());
  304. vout.close();
  305. } catch (IOException ex) {
  306. ex.printStackTrace();
  307. } catch (SecurityException ex) {
  308. ex.printStackTrace();
  309. }
  310. }
  311. public String readVersion() {
  312. byte[] buffer = new byte[(int) versionFile.length()];
  313. BufferedInputStream f = null;
  314. try {
  315. f = new BufferedInputStream(new FileInputStream(versionFile));
  316. f.read(buffer);
  317. } catch (FileNotFoundException ex) {
  318. ex.printStackTrace();
  319. } catch (IOException ex) {
  320. ex.printStackTrace();
  321. } finally {
  322. if (f != null) try { f.close(); } catch (IOException ignored) { }
  323. }
  324. return new String(buffer);
  325. }
  326. }