PageRenderTime 275ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/src/us/icebrg/imp/IMPPlugin.java

https://bitbucket.org/jyc/imp
Java | 146 lines | 78 code | 26 blank | 42 comment | 5 complexity | b5b8951689f28a81b5fb1ff9bfccf37a MD5 | raw file
Possible License(s): GPL-3.0
  1. package us.icebrg.imp;
  2. import java.util.logging.Level;
  3. import java.util.logging.Logger;
  4. import org.bukkit.plugin.PluginDescriptionFile;
  5. import org.bukkit.plugin.java.JavaPlugin;
  6. import java.io.File;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandExecutor;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.event.Event;
  11. import org.bukkit.event.Event.Priority;
  12. import org.bukkit.event.Listener;
  13. import us.icebrg.util.JarLibLoader;
  14. /**
  15. * A "liaison" for interacting with Bukkit.
  16. */
  17. public class IMPPlugin extends JavaPlugin {
  18. /**
  19. * The Logger to log to.
  20. */
  21. private Logger log;
  22. /**
  23. * The IMP instance this plugin is bound to.
  24. */
  25. private IMP imp;
  26. /**
  27. * The Bukkit PlayerListener that Bukkit will use to call us on special
  28. * events.
  29. */
  30. private IMPPlayerListener playerListener;
  31. @Override
  32. public void onEnable() {
  33. this.log = Logger.getLogger("Minecraft");
  34. PluginDescriptionFile pdf = this.getDescription();
  35. // Try to load all of the required classes - this must happen as early
  36. // as possible, or we will get ClassDefNotFound errors!
  37. if (!(JarLibLoader.load(
  38. new File("./lib/gson-1.7.1.jar"),
  39. new String[] { "com.google.gson" })
  40. && JarLibLoader.load(
  41. new File("./lib/commons-io-2.0.1.jar"),
  42. new String[] { "org.apache.commons.io" })
  43. && JarLibLoader.load(
  44. new File("./lib/mongo-2.6.5.jar"),
  45. new String[] { "com.mongodb" })
  46. && JarLibLoader.load(
  47. new File("./lib/morphia-0.99.jar"),
  48. new String[] { "com.google.code.morphia" }))) {
  49. this.log(Level.SEVERE, "Failed to load one or more required"
  50. + " libraries. Disabling ourselves.");
  51. this.disable();
  52. return;
  53. }
  54. // Create a new IMP instance and bind it to ourselves
  55. this.imp = new IMP(this);
  56. // Try to initialize the IMP
  57. if ( ! this.imp.init()) {
  58. this.log(Level.SEVERE, "Disabling ourselves.");
  59. this.disable();
  60. return;
  61. }
  62. // Register events
  63. this.playerListener = new IMPPlayerListener(this.imp);
  64. this.getServer().getPluginManager().registerEvent(
  65. Event.Type.PLAYER_COMMAND_PREPROCESS,
  66. this.playerListener, Priority.Highest, this);
  67. // Allow the anonymous class to access the IMP
  68. final IMP that = this.imp;
  69. // Register the command
  70. this.getCommand("imp").setExecutor(new CommandExecutor() {
  71. @Override
  72. public boolean onCommand(CommandSender sender, Command command,
  73. String raw, String[] args) {
  74. that.executeCommand(sender, raw, args);
  75. return true;
  76. }
  77. });
  78. }
  79. /**
  80. * Wrapper for
  81. * <code>this.getServer().getPluginmanager().registerEvent()</code>.
  82. * @param type The type of event to register.
  83. * @param listener The listener who will act on the event.
  84. * @param priority The event priority. Lowest is executed first, Highest and
  85. * then Monitor last.
  86. */
  87. public void registerEvent(Event.Type type, Listener listener,
  88. Priority priority) {
  89. this.getServer().getPluginManager().registerEvent(
  90. type, listener, priority, this);
  91. }
  92. /**
  93. * Logs a message to the server's logger ("Minecraft").
  94. * @param level The log level (Level.INFO, Level.SEVERE, etc.).
  95. * @param message The message to log.
  96. */
  97. public void log(Level level, String message) {
  98. message = IMP.logPrefix + message;
  99. this.log.log(level, message);
  100. }
  101. /**
  102. * Logs a message to the server's logger ("Minecraft").
  103. * @param level The log level to log with.
  104. * @param message The message to log.
  105. * @param e The exception to log with - its stack trace will be printed.
  106. */
  107. public void log(Level level, String message, Exception e) {
  108. message = IMP.logPrefix + message;
  109. this.log.log(level, message, e);
  110. }
  111. @Override
  112. public void onDisable() {
  113. }
  114. /**
  115. * Convenience function for disabling ourselves.
  116. */
  117. private void disable() {
  118. this.getServer().getPluginManager().disablePlugin(this);
  119. }
  120. }