/src/us/icebrg/imp/IMPPlugin.java
Java | 146 lines | 78 code | 26 blank | 42 comment | 5 complexity | b5b8951689f28a81b5fb1ff9bfccf37a MD5 | raw file
Possible License(s): GPL-3.0
- package us.icebrg.imp;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.io.File;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.event.Event;
- import org.bukkit.event.Event.Priority;
- import org.bukkit.event.Listener;
- import us.icebrg.util.JarLibLoader;
- /**
- * A "liaison" for interacting with Bukkit.
- */
- public class IMPPlugin extends JavaPlugin {
- /**
- * The Logger to log to.
- */
- private Logger log;
- /**
- * The IMP instance this plugin is bound to.
- */
- private IMP imp;
- /**
- * The Bukkit PlayerListener that Bukkit will use to call us on special
- * events.
- */
- private IMPPlayerListener playerListener;
- @Override
- public void onEnable() {
- this.log = Logger.getLogger("Minecraft");
- PluginDescriptionFile pdf = this.getDescription();
- // Try to load all of the required classes - this must happen as early
- // as possible, or we will get ClassDefNotFound errors!
- if (!(JarLibLoader.load(
- new File("./lib/gson-1.7.1.jar"),
- new String[] { "com.google.gson" })
- && JarLibLoader.load(
- new File("./lib/commons-io-2.0.1.jar"),
- new String[] { "org.apache.commons.io" })
- && JarLibLoader.load(
- new File("./lib/mongo-2.6.5.jar"),
- new String[] { "com.mongodb" })
- && JarLibLoader.load(
- new File("./lib/morphia-0.99.jar"),
- new String[] { "com.google.code.morphia" }))) {
- this.log(Level.SEVERE, "Failed to load one or more required"
- + " libraries. Disabling ourselves.");
- this.disable();
- return;
- }
- // Create a new IMP instance and bind it to ourselves
- this.imp = new IMP(this);
- // Try to initialize the IMP
- if ( ! this.imp.init()) {
- this.log(Level.SEVERE, "Disabling ourselves.");
- this.disable();
- return;
- }
- // Register events
- this.playerListener = new IMPPlayerListener(this.imp);
- this.getServer().getPluginManager().registerEvent(
- Event.Type.PLAYER_COMMAND_PREPROCESS,
- this.playerListener, Priority.Highest, this);
- // Allow the anonymous class to access the IMP
- final IMP that = this.imp;
- // Register the command
- this.getCommand("imp").setExecutor(new CommandExecutor() {
- @Override
- public boolean onCommand(CommandSender sender, Command command,
- String raw, String[] args) {
- that.executeCommand(sender, raw, args);
- return true;
- }
- });
- }
- /**
- * Wrapper for
- * <code>this.getServer().getPluginmanager().registerEvent()</code>.
- * @param type The type of event to register.
- * @param listener The listener who will act on the event.
- * @param priority The event priority. Lowest is executed first, Highest and
- * then Monitor last.
- */
- public void registerEvent(Event.Type type, Listener listener,
- Priority priority) {
- this.getServer().getPluginManager().registerEvent(
- type, listener, priority, this);
- }
- /**
- * Logs a message to the server's logger ("Minecraft").
- * @param level The log level (Level.INFO, Level.SEVERE, etc.).
- * @param message The message to log.
- */
- public void log(Level level, String message) {
- message = IMP.logPrefix + message;
- this.log.log(level, message);
- }
- /**
- * Logs a message to the server's logger ("Minecraft").
- * @param level The log level to log with.
- * @param message The message to log.
- * @param e The exception to log with - its stack trace will be printed.
- */
- public void log(Level level, String message, Exception e) {
- message = IMP.logPrefix + message;
- this.log.log(level, message, e);
- }
- @Override
- public void onDisable() {
- }
- /**
- * Convenience function for disabling ourselves.
- */
- private void disable() {
- this.getServer().getPluginManager().disablePlugin(this);
- }
- }