Skip to content

Instantly share code, notes, and snippets.

@Twisol
Created January 23, 2011 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Twisol/b7e40e5a07611738e467 to your computer and use it in GitHub Desktop.
Save Twisol/b7e40e5a07611738e467 to your computer and use it in GitHub Desktop.
class CommandManager {
private Plugin plugin;
private Map<String, Class<? extends ICommandable>> commands = new Hashtable<String, Class<? extends ICommandable>>();
public CommandManager(Plugin plugin) {
this.plugin = plugin;
}
public void loadFromDescription(PluginDescriptionFile desc, ClassLoader loader) {
Object object = desc.getCommands();
if (object == null)
return;
@SuppressWarnings("unchecked")
Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) object;
for (Entry<String, Map<String, Object>> entry : map.entrySet()) {
String label = entry.getKey();
String classname = entry.getValue().get("class").toString();
try {
Class<?> klass = Class.forName(classname, true, loader);
Class<? extends ICommandable> commandClass = klass.asSubclass(ICommandable.class);
addCommand(label, commandClass);
} catch (ClassNotFoundException e) {
System.out.println("Unable to load command class for command /" + label);
}
}
}
public void addCommand(String label, Class<? extends ICommandable> klass) {
commands.put(label, klass);
}
public boolean dispatch(Player player, Command command, String label, String[] args) {
if (!commands.containsKey(label))
return false;
boolean handled = true;
Class<? extends ICommandable> klass = commands.get(label);
try {
Constructor<? extends ICommandable> ctor = klass.getConstructor(Plugin.class);
ICommandable c = ctor.newInstance(plugin);
handled = c.onCommand(player, command, label, args);
} catch (NoSuchMethodException e) {
System.out.println("No constructor that accepts a Plugin.");
} catch (InstantiationException e) {
System.out.println("Error while creating a Commandable object.");
} catch (IllegalAccessException e) {
System.out.println("Illegal access to the Commandable object constructor.");
} catch (InvocationTargetException e) {
System.out.println(e.getCause().getMessage());
}
return handled;
}
}
public interface ICommandable {
boolean onCommand(Player player, Command command, String label, String[] args);
}
name: Tester
main: com.jonathan.minecraft.Tester.Tester
version: 1.00
commands:
test:
class: com.jonathan.minecraft.Tester.commands.TestCommand
description: Displays a test message.
usage: /<command>
public class Tester extends JavaPlugin {
private CommandManager manager = new CommandManager(this);
public Waypoint(PluginLoader pluginLoader, Server instance,
PluginDescriptionFile desc, File folder, File plugin,
ClassLoader cLoader) {
super(pluginLoader, instance, desc, folder, plugin, cLoader);
manager.loadFromDescription(desc, cLoader);
}
@Override
public boolean onCommand(Player player, Command command, String label, String[] args) {
return manager.dispatch(player, command, label, args);
}
@Override
public void onEnable() {
}
@Override
public void onDisable() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment