Added more flexibility to the Bukkit dynamic command registration system.

This commit is contained in:
zml2008 2011-12-29 13:59:48 -08:00
parent 76f1ea9cf2
commit 4fb44ebc1d
4 changed files with 36 additions and 10 deletions

View File

@ -27,6 +27,7 @@ import java.util.Set;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.util.ReflectionUtil; import com.sk89q.util.ReflectionUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.command.SimpleCommandMap; import org.bukkit.command.SimpleCommandMap;
import org.bukkit.event.Event; import org.bukkit.event.Event;
@ -37,10 +38,16 @@ import org.bukkit.plugin.Plugin;
*/ */
public class CommandRegistration { public class CommandRegistration {
private final Plugin plugin; private final Plugin plugin;
private final CommandExecutor executor;
private CommandMap fallbackCommands; private CommandMap fallbackCommands;
public CommandRegistration(Plugin plugin) { public CommandRegistration(Plugin plugin) {
this(plugin, plugin);
}
public CommandRegistration(Plugin plugin, CommandExecutor executor) {
this.plugin = plugin; this.plugin = plugin;
this.executor = executor;
} }
public boolean registerAll(List<Command> registered) { public boolean registerAll(List<Command> registered) {
@ -49,12 +56,13 @@ public class CommandRegistration {
return false; return false;
} }
for (Command command : registered) { 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; return true;
} }
private CommandMap getCommandMap() { public CommandMap getCommandMap() {
CommandMap commandMap = ReflectionUtil.getField(plugin.getServer().getPluginManager(), "commandMap"); CommandMap commandMap = ReflectionUtil.getField(plugin.getServer().getPluginManager(), "commandMap");
if (commandMap == null) { if (commandMap == null) {
if (fallbackCommands != null) { if (fallbackCommands != null) {
@ -80,7 +88,7 @@ public class CommandRegistration {
} }
for (Iterator<org.bukkit.command.Command> i = knownCommands.values().iterator(); i.hasNext();) { for (Iterator<org.bukkit.command.Command> i = knownCommands.values().iterator(); i.hasNext();) {
org.bukkit.command.Command cmd = i.next(); 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(); i.remove();
for (String alias : cmd.getAliases()) { for (String alias : cmd.getAliases()) {
org.bukkit.command.Command aliasCmd = knownCommands.get(alias); org.bukkit.command.Command aliasCmd = knownCommands.get(alias);

View File

@ -20,6 +20,7 @@ package com.sk89q.bukkit.util;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandsManager; import com.sk89q.minecraft.util.commands.CommandsManager;
import org.bukkit.command.CommandExecutor;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.List; import java.util.List;
@ -35,6 +36,11 @@ public class CommandsManagerRegistration extends CommandRegistration {
this.commands = commands; this.commands = commands;
} }
public CommandsManagerRegistration(Plugin plugin, CommandExecutor executor, CommandsManager<?> commands) {
super(plugin, executor);
this.commands = commands;
}
public boolean register(Class<?> clazz) { public boolean register(Class<?> clazz) {
List<Command> registered = commands.registerAndReturn(clazz); List<Command> registered = commands.registerAndReturn(clazz);
return registerAll(registered); return registerAll(registered);

View File

@ -19,6 +19,7 @@
package com.sk89q.bukkit.util; package com.sk89q.bukkit.util;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -29,19 +30,19 @@ import java.util.Arrays;
*/ */
public class DynamicPluginCommand extends org.bukkit.command.Command { public class DynamicPluginCommand extends org.bukkit.command.Command {
protected final Plugin plugin; protected final CommandExecutor owner;
public DynamicPluginCommand(Command command, Plugin plugin) { public DynamicPluginCommand(String[] aliases, String desc, String usage, CommandExecutor owner) {
super(command.aliases()[0], command.desc(), command.usage(), Arrays.asList(command.aliases())); super(aliases[0], desc, usage, Arrays.asList(aliases));
this.plugin = plugin; this.owner = owner;
} }
@Override @Override
public boolean execute(CommandSender sender, String label, String[] args) { 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() { public Object getOwner() {
return plugin; return owner;
} }
} }

View File

@ -20,8 +20,10 @@ package com.sk89q.minecraft.util.commands;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;
public class SimpleInjector implements Injector { public class SimpleInjector implements Injector {
private static final Logger logger = Logger.getLogger(SimpleInjector.class.getCanonicalName());
private Object[] args; private Object[] args;
private Class<?>[] argClasses; private Class<?>[] argClasses;
@ -36,14 +38,23 @@ public class SimpleInjector implements Injector {
public Object getInstance(Class<?> clazz) { public Object getInstance(Class<?> clazz) {
try { try {
Constructor<?> ctr = clazz.getConstructor(argClasses); Constructor<?> ctr = clazz.getConstructor(argClasses);
ctr.setAccessible(true);
return ctr.newInstance(args); return ctr.newInstance(args);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
logger.severe("Error initializing commands class " + clazz + ": ");
e.printStackTrace();
return null; return null;
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
logger.severe("Error initializing commands class " + clazz + ": ");
e.printStackTrace();
return null; return null;
} catch (InstantiationException e) { } catch (InstantiationException e) {
logger.severe("Error initializing commands class " + clazz + ": ");
e.printStackTrace();
return null; return null;
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
logger.severe("Error initializing commands class " + clazz + ": ");
e.printStackTrace();
return null; return null;
} }
} }