mcMMO.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package com.gmail.nossr50;
  2. import com.gmail.nossr50.datatypes.Mob;
  3. import com.gmail.nossr50.datatypes.PlayerProfile;
  4. import com.gmail.nossr50.datatypes.SkillType;
  5. import com.gmail.nossr50.command.Commands;
  6. import com.gmail.nossr50.config.*;
  7. import com.gmail.nossr50.spout.SpoutStuff;
  8. import com.gmail.nossr50.spout.mmoHelper;
  9. import com.gmail.nossr50.listeners.mcBlockListener;
  10. import com.gmail.nossr50.listeners.mcEntityListener;
  11. import com.gmail.nossr50.listeners.mcPlayerListener;
  12. import com.gmail.nossr50.party.Party;
  13. import com.gmail.nossr50.skills.*;
  14. import com.nijikokun.bukkit.Permissions.Permissions;
  15. import org.bukkit.Bukkit;
  16. import java.io.BufferedInputStream;
  17. import java.io.BufferedReader;
  18. import java.io.BufferedWriter;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileNotFoundException;
  22. import java.io.FileReader;
  23. import java.io.FileWriter;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import org.bukkit.command.Command;
  29. import org.bukkit.command.CommandSender;
  30. import org.bukkit.event.Event.Priority;
  31. import org.bukkit.event.Event;
  32. import org.bukkit.plugin.PluginDescriptionFile;
  33. import org.bukkit.plugin.java.JavaPlugin;
  34. import org.bukkit.plugin.PluginManager;
  35. import org.bukkit.entity.Player;
  36. public class mcMMO extends JavaPlugin
  37. {
  38. /*
  39. * I never expected mcMMO to get so popular!
  40. * Thanks for all the support for the mod
  41. * Thanks to the people who have worked on the code
  42. * Thanks to the donators who helped me out financially
  43. * Thanks to the server admins who use my mod :)
  44. *
  45. * This mod is open source, and its going to stay that way >:3
  46. *
  47. * Donate via paypal to nossr50@gmail.com (A million thanks to anyone that does!)
  48. */
  49. public static String maindirectory = "plugins" + File.separator + "mcMMO";
  50. File file = new File(maindirectory + File.separator + "config.yml");
  51. static File versionFile = new File(maindirectory + File.separator + "VERSION");
  52. public static final Logger log = Logger.getLogger("Minecraft");
  53. private final mcPlayerListener playerListener = new mcPlayerListener(this);
  54. private final mcBlockListener blockListener = new mcBlockListener(this);
  55. private final mcEntityListener entityListener = new mcEntityListener(this);
  56. public static mcPermissions permissionHandler = new mcPermissions();
  57. private Permissions permissions;
  58. private Runnable mcMMO_Timer = new mcTimer(this); //BLEED AND REGENERATION
  59. //private Timer mcMMO_SpellTimer = new Timer(true);
  60. public static Database database = null;
  61. public Mob mob = new Mob();
  62. public Misc misc = new Misc(this);
  63. //Config file stuff
  64. LoadProperties config = new LoadProperties();
  65. public void onEnable()
  66. {
  67. new File(maindirectory).mkdir();
  68. if(!versionFile.exists())
  69. {
  70. updateVersion();
  71. } else
  72. {
  73. String vnum = readVersion();
  74. //This will be changed to whatever version preceded when we actually need updater code.
  75. //Version 1.0.48 is the first to implement this, no checking before that version can be done.
  76. if(vnum.equalsIgnoreCase("1.0.48")) {
  77. updateFrom(1);
  78. }
  79. //Just add in more else if blocks for versions that need updater code. Increment the updateFrom age int as we do so.
  80. //Catch all for versions not matching and no specific code being needed
  81. else if(!vnum.equalsIgnoreCase(this.getDescription().getVersion())) updateFrom(-1);
  82. }
  83. mcPermissions.initialize(getServer());
  84. config.configCheck();
  85. Party.getInstance().loadParties();
  86. new Party(this);
  87. if(!LoadProperties.useMySQL)
  88. Users.getInstance().loadUsers(); //Load Users file
  89. /*
  90. * REGISTER EVENTS
  91. */
  92. PluginManager pm = getServer().getPluginManager();
  93. if(pm.getPlugin("Spout") != null)
  94. LoadProperties.spoutEnabled = true;
  95. else
  96. LoadProperties.spoutEnabled = false;
  97. //Player Stuff
  98. pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
  99. pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
  100. pm.registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Priority.Normal, this);
  101. pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.Highest, this);
  102. pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Priority.Monitor, this);
  103. pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener, Priority.Normal, this);
  104. pm.registerEvent(Event.Type.PLAYER_PICKUP_ITEM, playerListener, Priority.Normal, this);
  105. //Block Stuff
  106. pm.registerEvent(Event.Type.BLOCK_DAMAGE, blockListener, Priority.Highest, this);
  107. pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, Priority.Monitor, this);
  108. pm.registerEvent(Event.Type.BLOCK_FROMTO, blockListener, Priority.Normal, this);
  109. pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener, Priority.Normal, this);
  110. //Entity Stuff
  111. pm.registerEvent(Event.Type.ENTITY_TARGET, entityListener, Priority.Normal, this);
  112. pm.registerEvent(Event.Type.ENTITY_DEATH, entityListener, Priority.Normal, this);
  113. pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Priority.High, this);
  114. pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Priority.Normal, this);
  115. //Spout Stuff
  116. if(LoadProperties.spoutEnabled)
  117. {
  118. SpoutStuff.setupSpoutConfigs();
  119. SpoutStuff.registerCustomEvent();
  120. }
  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().scheduleAsyncRepeatingTask(this, mcMMO_Timer, 0, 20);
  132. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this,
  133. new Runnable() {
  134. @Override
  135. public void run() {
  136. mmoHelper.updateAll();
  137. }
  138. }, 20, 20);
  139. }
  140. public PlayerProfile getPlayerProfile(Player player)
  141. {
  142. return Users.getProfile(player);
  143. }
  144. public void checkXp(Player player, SkillType skillType)
  145. {
  146. if(skillType == SkillType.ALL)
  147. Skills.XpCheckAll(player);
  148. else
  149. Skills.XpCheckSkill(skillType, player);
  150. }
  151. public boolean inSameParty(Player playera, Player playerb)
  152. {
  153. if(Users.getProfile(playera).inParty() && Users.getProfile(playerb).inParty()){
  154. if(Users.getProfile(playera).getParty().equals(Users.getProfile(playerb).getParty())){
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. } else {
  160. return false;
  161. }
  162. }
  163. public ArrayList<String> getParties(){
  164. String location = "plugins/mcMMO/mcmmo.users";
  165. ArrayList<String> parties = new ArrayList<String>();
  166. try {
  167. //Open the users file
  168. FileReader file = new FileReader(location);
  169. BufferedReader in = new BufferedReader(file);
  170. String line = "";
  171. while((line = in.readLine()) != null)
  172. {
  173. String[] character = line.split(":");
  174. String theparty = null;
  175. //Party
  176. if(character.length > 3)
  177. theparty = character[3];
  178. if(!parties.contains(theparty))
  179. parties.add(theparty);
  180. }
  181. in.close();
  182. } catch (Exception e) {
  183. log.log(Level.SEVERE, "Exception while reading "
  184. + location + " (Are you sure you formatted it correctly?)", e);
  185. }
  186. return parties;
  187. }
  188. public static String getPartyName(Player player){
  189. PlayerProfile PP = Users.getProfile(player);
  190. return PP.getParty();
  191. }
  192. public static boolean inParty(Player player){
  193. PlayerProfile PP = Users.getProfile(player);
  194. return PP.inParty();
  195. }
  196. public Permissions getPermissions() {
  197. return permissions;
  198. }
  199. public void onDisable() {
  200. Bukkit.getServer().getScheduler().cancelTasks(this);
  201. System.out.println("mcMMO was disabled.");
  202. }
  203. public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
  204. {
  205. return Commands.processCommands(sender, command, label, args);
  206. }
  207. /*
  208. * It is important to always assume that you are updating from the lowest possible version.
  209. * Thus, every block of updater code should be complete and self-contained; finishing all
  210. * SQL transactions and closing all file handlers, such that the next block of updater code
  211. * if called will handle updating as expected.
  212. */
  213. public void updateFrom(int age) {
  214. //No updater code needed, just update the version.
  215. if(age == -1) {
  216. updateVersion();
  217. return;
  218. }
  219. //Updater code from age 1 goes here
  220. if(age <= 1) {
  221. //Since age 1 is an example for now, we will just let it do nothing.
  222. }
  223. //If we are updating from age 1 but we need more to reach age 2, this will run too.
  224. if(age <= 2) {
  225. }
  226. updateVersion();
  227. }
  228. public void updateVersion() {
  229. try {
  230. versionFile.createNewFile();
  231. BufferedWriter vout = new BufferedWriter(new FileWriter(versionFile));
  232. vout.write(this.getDescription().getVersion());
  233. vout.close();
  234. } catch (IOException ex) {
  235. ex.printStackTrace();
  236. } catch (SecurityException ex) {
  237. ex.printStackTrace();
  238. }
  239. }
  240. public String readVersion() {
  241. byte[] buffer = new byte[(int) versionFile.length()];
  242. BufferedInputStream f = null;
  243. try {
  244. f = new BufferedInputStream(new FileInputStream(versionFile));
  245. f.read(buffer);
  246. } catch (FileNotFoundException ex) {
  247. ex.printStackTrace();
  248. } catch (IOException ex) {
  249. ex.printStackTrace();
  250. } finally {
  251. if (f != null) try { f.close(); } catch (IOException ignored) { }
  252. }
  253. return new String(buffer);
  254. }
  255. }