PageRenderTime 65ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/gmail/nossr50/mcMMO.java

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