Skip to content

Instantly share code, notes, and snippets.

@Twisol Twisol/ Secret
Created Jan 23, 2011

What would you like to do?
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)
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) {
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
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);
public boolean onCommand(Player player, Command command, String label, String[] args) {
return manager.dispatch(player, command, label, args);
public void onEnable() {
public void onDisable() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.