PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/massivecraft/factions/zcore/MPlugin.java

https://github.com/dotblank/Factions
Java | 244 lines | 168 code | 43 blank | 33 comment | 16 complexity | 0a9d71f3a29bfcd9f0e18271eb586000 MD5 | raw file
  1. package com.massivecraft.factions.zcore;
  2. import java.lang.reflect.Modifier;
  3. import java.lang.reflect.Type;
  4. import java.util.*;
  5. import java.util.Map.Entry;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.event.Event;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.plugin.PluginManager;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14. import com.google.gson.Gson;
  15. import com.google.gson.GsonBuilder;
  16. import com.google.gson.reflect.TypeToken;
  17. import com.massivecraft.factions.zcore.persist.EM;
  18. import com.massivecraft.factions.zcore.persist.SaveTask;
  19. import com.massivecraft.factions.zcore.util.LibLoader;
  20. import com.massivecraft.factions.zcore.util.PermUtil;
  21. import com.massivecraft.factions.zcore.util.Persist;
  22. import com.massivecraft.factions.zcore.util.TextUtil;
  23. public abstract class MPlugin extends JavaPlugin
  24. {
  25. // Some utils
  26. public Persist persist;
  27. public TextUtil txt;
  28. public LibLoader lib;
  29. public PermUtil perm;
  30. // Persist related
  31. public Gson gson;
  32. private Integer saveTask = null;
  33. private boolean autoSave = true;
  34. public boolean getAutoSave() {return this.autoSave;}
  35. public void setAutoSave(boolean val) {this.autoSave = val;}
  36. // Listeners
  37. private MPluginSecretPlayerListener mPluginSecretPlayerListener;
  38. private MPluginSecretServerListener mPluginSecretServerListener;
  39. // Our stored base commands
  40. private List<MCommand<?>> baseCommands = new ArrayList<MCommand<?>>();
  41. public List<MCommand<?>> getBaseCommands() { return this.baseCommands; }
  42. // -------------------------------------------- //
  43. // ENABLE
  44. // -------------------------------------------- //
  45. private long timeEnableStart;
  46. public boolean preEnable()
  47. {
  48. log("=== ENABLE START ===");
  49. timeEnableStart = System.currentTimeMillis();
  50. // Ensure basefolder exists!
  51. this.getDataFolder().mkdirs();
  52. // Create Utility Instances
  53. this.perm = new PermUtil(this);
  54. this.persist = new Persist(this);
  55. this.lib = new LibLoader(this);
  56. if ( ! lib.require("gson.jar", "http://search.maven.org/remotecontent?filepath=com/google/code/gson/gson/1.7.1/gson-1.7.1.jar")) return false;
  57. this.gson = this.getGsonBuilder().create();
  58. this.txt = new TextUtil();
  59. initTXT();
  60. // Create and register listeners
  61. this.mPluginSecretPlayerListener = new MPluginSecretPlayerListener(this);
  62. this.mPluginSecretServerListener = new MPluginSecretServerListener(this);
  63. PluginManager pm = this.getServer().getPluginManager();
  64. pm.registerEvent(Event.Type.PLAYER_PRELOGIN, this.mPluginSecretPlayerListener, Event.Priority.Lowest, this);
  65. pm.registerEvent(Event.Type.PLAYER_CHAT, this.mPluginSecretPlayerListener, Event.Priority.Low, this);
  66. pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, this.mPluginSecretPlayerListener, Event.Priority.Lowest, this);
  67. pm.registerEvent(Event.Type.SERVER_COMMAND, this.mPluginSecretServerListener, Event.Priority.Lowest, this);
  68. // Register recurring tasks
  69. long saveTicks = 20 * 60 * 30; // Approximately every 30 min
  70. if (saveTask == null)
  71. {
  72. saveTask = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new SaveTask(this), saveTicks, saveTicks);
  73. }
  74. return true;
  75. }
  76. public void postEnable()
  77. {
  78. log("=== ENABLE DONE (Took "+(System.currentTimeMillis()-timeEnableStart)+"ms) ===");
  79. }
  80. public void onDisable()
  81. {
  82. if (saveTask != null)
  83. {
  84. this.getServer().getScheduler().cancelTask(saveTask);
  85. saveTask = null;
  86. }
  87. EM.saveAllToDisc();
  88. log("Disabled");
  89. }
  90. public void suicide()
  91. {
  92. log("Now I suicide!");
  93. this.getServer().getPluginManager().disablePlugin(this);
  94. }
  95. // -------------------------------------------- //
  96. // Register Event convenience method
  97. // -------------------------------------------- //
  98. public void registerEvent(Event.Type type, Listener listener, Event.Priority priority)
  99. {
  100. Bukkit.getServer().getPluginManager().registerEvent(type, listener, priority, this);
  101. }
  102. public void registerEvent(Event.Type type, Listener listener)
  103. {
  104. registerEvent(type, listener, Event.Priority.Normal);
  105. }
  106. // -------------------------------------------- //
  107. // Some inits...
  108. // You are supposed to override these in the plugin if you aren't satisfied with the defaults
  109. // The goal is that you always will be satisfied though.
  110. // -------------------------------------------- //
  111. public GsonBuilder getGsonBuilder()
  112. {
  113. return new GsonBuilder()
  114. .setPrettyPrinting()
  115. .disableHtmlEscaping()
  116. .serializeNulls()
  117. .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE);
  118. }
  119. // -------------------------------------------- //
  120. // LANG AND TAGS
  121. // -------------------------------------------- //
  122. // These are not supposed to be used directly.
  123. // They are loaded and used through the TextUtil instance for the plugin.
  124. public Map<String, String> rawTags = new LinkedHashMap<String, String>();
  125. public void addRawTags()
  126. {
  127. this.rawTags.put("l", "<green>"); // logo
  128. this.rawTags.put("a", "<gold>"); // art
  129. this.rawTags.put("n", "<silver>"); // notice
  130. this.rawTags.put("i", "<yellow>"); // info
  131. this.rawTags.put("g", "<lime>"); // good
  132. this.rawTags.put("b", "<rose>"); // bad
  133. this.rawTags.put("h", "<pink>"); // highligh
  134. this.rawTags.put("c", "<aqua>"); // command
  135. this.rawTags.put("p", "<teal>"); // parameter
  136. }
  137. public void initTXT()
  138. {
  139. this.addRawTags();
  140. Type type = new TypeToken<Map<String, String>>(){}.getType();
  141. Map<String, String> tagsFromFile = this.persist.load(type, "tags");
  142. if (tagsFromFile != null) this.rawTags.putAll(tagsFromFile);
  143. this.persist.save(this.rawTags, "tags");
  144. for (Entry<String, String> rawTag : this.rawTags.entrySet())
  145. {
  146. this.txt.tags.put(rawTag.getKey(), TextUtil.parseColor(rawTag.getValue()));
  147. }
  148. }
  149. // -------------------------------------------- //
  150. // COMMAND HANDLING
  151. // -------------------------------------------- //
  152. public boolean handleCommand(CommandSender sender, String commandString, boolean testOnly)
  153. {
  154. boolean noSlash = true;
  155. if (commandString.startsWith("/"))
  156. {
  157. noSlash = false;
  158. commandString = commandString.substring(1);
  159. }
  160. for (MCommand<?> command : this.getBaseCommands())
  161. {
  162. if (noSlash && ! command.allowNoSlashAccess) continue;
  163. for (String alias : command.aliases)
  164. {
  165. if (commandString.startsWith(alias+" ") || commandString.equals(alias))
  166. {
  167. List<String> args = new ArrayList<String>(Arrays.asList(commandString.split("\\s+")));
  168. args.remove(0);
  169. if (testOnly) return true;
  170. command.execute(sender, args);
  171. return true;
  172. }
  173. }
  174. }
  175. return false;
  176. }
  177. public boolean handleCommand(CommandSender sender, String commandString)
  178. {
  179. return this.handleCommand(sender, commandString, false);
  180. }
  181. // -------------------------------------------- //
  182. // HOOKS
  183. // -------------------------------------------- //
  184. public void preAutoSave()
  185. {
  186. }
  187. public void postAutoSave()
  188. {
  189. }
  190. // -------------------------------------------- //
  191. // LOGGING
  192. // -------------------------------------------- //
  193. public void log(Object msg)
  194. {
  195. log(Level.INFO, msg);
  196. }
  197. public void log(Level level, Object msg)
  198. {
  199. Logger.getLogger("Minecraft").log(level, "["+this.getDescription().getFullName()+"] "+msg);
  200. }
  201. }