Added more flexibility to the Bukkit dynamic command registration system.

Dieser Commit ist enthalten in:
zml2008 2011-12-29 13:59:48 -08:00
Ursprung 76f1ea9cf2
Commit 4fb44ebc1d
4 geänderte Dateien mit 36 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -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<Command> 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<org.bukkit.command.Command> 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);

Datei anzeigen

@ -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<Command> registered = commands.registerAndReturn(clazz);
return registerAll(registered);

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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;
}
}