From 4fb44ebc1db17d524fe37a186d5225d2296a1c70 Mon Sep 17 00:00:00 2001 From: zml2008 Date: Thu, 29 Dec 2011 13:59:48 -0800 Subject: [PATCH] Added more flexibility to the Bukkit dynamic command registration system. --- .../sk89q/bukkit/util/CommandRegistration.java | 14 +++++++++++--- .../bukkit/util/CommandsManagerRegistration.java | 6 ++++++ .../sk89q/bukkit/util/DynamicPluginCommand.java | 15 ++++++++------- .../minecraft/util/commands/SimpleInjector.java | 11 +++++++++++ 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/sk89q/bukkit/util/CommandRegistration.java b/src/main/java/com/sk89q/bukkit/util/CommandRegistration.java index 53c140bb3..a7d68a1d9 100644 --- a/src/main/java/com/sk89q/bukkit/util/CommandRegistration.java +++ b/src/main/java/com/sk89q/bukkit/util/CommandRegistration.java @@ -27,6 +27,7 @@ import java.util.Set; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.util.ReflectionUtil; import org.bukkit.Bukkit; +import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandMap; import org.bukkit.command.SimpleCommandMap; import org.bukkit.event.Event; @@ -37,10 +38,16 @@ import org.bukkit.plugin.Plugin; */ public class CommandRegistration { private final Plugin plugin; + private final CommandExecutor executor; private CommandMap fallbackCommands; public CommandRegistration(Plugin plugin) { + this(plugin, plugin); + } + + public CommandRegistration(Plugin plugin, CommandExecutor executor) { this.plugin = plugin; + this.executor = executor; } public boolean registerAll(List registered) { @@ -49,12 +56,13 @@ public class CommandRegistration { return false; } for (Command command : registered) { - commandMap.register(plugin.getDescription().getName(), new DynamicPluginCommand(command, plugin)); + commandMap.register(plugin.getDescription().getName(), + new DynamicPluginCommand(command.aliases(), command.desc(), command.usage(), executor)); } return true; } - private CommandMap getCommandMap() { + public CommandMap getCommandMap() { CommandMap commandMap = ReflectionUtil.getField(plugin.getServer().getPluginManager(), "commandMap"); if (commandMap == null) { if (fallbackCommands != null) { @@ -80,7 +88,7 @@ public class CommandRegistration { } for (Iterator i = knownCommands.values().iterator(); i.hasNext();) { org.bukkit.command.Command cmd = i.next(); - if (cmd instanceof DynamicPluginCommand && ((DynamicPluginCommand) cmd).getPlugin().equals(plugin)) { + if (cmd instanceof DynamicPluginCommand && ((DynamicPluginCommand) cmd).getOwner().equals(plugin)) { i.remove(); for (String alias : cmd.getAliases()) { org.bukkit.command.Command aliasCmd = knownCommands.get(alias); diff --git a/src/main/java/com/sk89q/bukkit/util/CommandsManagerRegistration.java b/src/main/java/com/sk89q/bukkit/util/CommandsManagerRegistration.java index a74391e95..3da67842b 100644 --- a/src/main/java/com/sk89q/bukkit/util/CommandsManagerRegistration.java +++ b/src/main/java/com/sk89q/bukkit/util/CommandsManagerRegistration.java @@ -20,6 +20,7 @@ package com.sk89q.bukkit.util; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandsManager; +import org.bukkit.command.CommandExecutor; import org.bukkit.plugin.Plugin; import java.util.List; @@ -35,6 +36,11 @@ public class CommandsManagerRegistration extends CommandRegistration { this.commands = commands; } + public CommandsManagerRegistration(Plugin plugin, CommandExecutor executor, CommandsManager commands) { + super(plugin, executor); + this.commands = commands; + } + public boolean register(Class clazz) { List registered = commands.registerAndReturn(clazz); return registerAll(registered); diff --git a/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java b/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java index 017e6a6e2..4c11d194a 100644 --- a/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java +++ b/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java @@ -19,6 +19,7 @@ package com.sk89q.bukkit.util; import com.sk89q.minecraft.util.commands.Command; +import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; @@ -29,19 +30,19 @@ import java.util.Arrays; */ public class DynamicPluginCommand extends org.bukkit.command.Command { - protected final Plugin plugin; + protected final CommandExecutor owner; - public DynamicPluginCommand(Command command, Plugin plugin) { - super(command.aliases()[0], command.desc(), command.usage(), Arrays.asList(command.aliases())); - this.plugin = plugin; + public DynamicPluginCommand(String[] aliases, String desc, String usage, CommandExecutor owner) { + super(aliases[0], desc, usage, Arrays.asList(aliases)); + this.owner = owner; } @Override public boolean execute(CommandSender sender, String label, String[] args) { - return plugin.onCommand(sender, this, label, args); + return owner.onCommand(sender, this, label, args); } - public Plugin getPlugin() { - return plugin; + public Object getOwner() { + return owner; } } diff --git a/src/main/java/com/sk89q/minecraft/util/commands/SimpleInjector.java b/src/main/java/com/sk89q/minecraft/util/commands/SimpleInjector.java index af71b54df..50e773a1e 100644 --- a/src/main/java/com/sk89q/minecraft/util/commands/SimpleInjector.java +++ b/src/main/java/com/sk89q/minecraft/util/commands/SimpleInjector.java @@ -20,8 +20,10 @@ package com.sk89q.minecraft.util.commands; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.util.logging.Logger; public class SimpleInjector implements Injector { + private static final Logger logger = Logger.getLogger(SimpleInjector.class.getCanonicalName()); private Object[] args; private Class[] argClasses; @@ -36,14 +38,23 @@ public class SimpleInjector implements Injector { public Object getInstance(Class clazz) { try { Constructor ctr = clazz.getConstructor(argClasses); + ctr.setAccessible(true); return ctr.newInstance(args); } catch (NoSuchMethodException e) { + logger.severe("Error initializing commands class " + clazz + ": "); + e.printStackTrace(); return null; } catch (InvocationTargetException e) { + logger.severe("Error initializing commands class " + clazz + ": "); + e.printStackTrace(); return null; } catch (InstantiationException e) { + logger.severe("Error initializing commands class " + clazz + ": "); + e.printStackTrace(); return null; } catch (IllegalAccessException e) { + logger.severe("Error initializing commands class " + clazz + ": "); + e.printStackTrace(); return null; } }