From a17e7470e8f7aadd0c0ca1be8a81e24616098c09 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sun, 16 Jan 2011 16:30:34 +0100 Subject: [PATCH] Added command registration to plugin config file. Registered commands are sent to the corresponding plugin.onCommand method when executed. By: VictorD --- .../src/main/java/org/bukkit/Server.java | 1 + .../src/main/java/org/bukkit/fillr/Fillr.java | 5 ++ .../main/java/org/bukkit/plugin/Command.java | 31 ++++++++++ .../org/bukkit/plugin/CommandManager.java | 28 +++++++++ .../org/bukkit/plugin/CommandParserYaml.java | 29 +++++++++ .../main/java/org/bukkit/plugin/Plugin.java | 6 ++ .../bukkit/plugin/PluginDescriptionFile.java | 14 +++++ .../bukkit/plugin/SimpleCommandManager.java | 62 +++++++++++++++++++ .../org/bukkit/plugin/java/JavaPlugin.java | 8 +++ 9 files changed, 184 insertions(+) create mode 100644 paper-api/src/main/java/org/bukkit/plugin/Command.java create mode 100644 paper-api/src/main/java/org/bukkit/plugin/CommandManager.java create mode 100644 paper-api/src/main/java/org/bukkit/plugin/CommandParserYaml.java create mode 100644 paper-api/src/main/java/org/bukkit/plugin/SimpleCommandManager.java diff --git a/paper-api/src/main/java/org/bukkit/Server.java b/paper-api/src/main/java/org/bukkit/Server.java index 6a3b47e2ae..b980366549 100644 --- a/paper-api/src/main/java/org/bukkit/Server.java +++ b/paper-api/src/main/java/org/bukkit/Server.java @@ -3,6 +3,7 @@ package org.bukkit; import org.bukkit.entity.Player; import java.util.List; + import org.bukkit.plugin.PluginManager; /** diff --git a/paper-api/src/main/java/org/bukkit/fillr/Fillr.java b/paper-api/src/main/java/org/bukkit/fillr/Fillr.java index 185779c1fd..2bb068cac9 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/Fillr.java +++ b/paper-api/src/main/java/org/bukkit/fillr/Fillr.java @@ -3,6 +3,7 @@ package org.bukkit.fillr; import org.bukkit.*; import org.bukkit.plugin.*; import org.bukkit.plugin.java.*; +import org.bukkit.entity.Player; import org.bukkit.event.*; import java.io.File; @@ -30,4 +31,8 @@ public class Fillr extends JavaPlugin { listener = new FillrListener(getServer()); getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, listener, Event.Priority.Normal, this); } + + public void onCommand(Player player, String command, String[] args) { + // TODO Auto-generated method stub + } } diff --git a/paper-api/src/main/java/org/bukkit/plugin/Command.java b/paper-api/src/main/java/org/bukkit/plugin/Command.java new file mode 100644 index 0000000000..d48e503ef9 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/plugin/Command.java @@ -0,0 +1,31 @@ +package org.bukkit.plugin; + +public final class Command { + private final String name; + private final String tooltip; + private final String usage; + private final Plugin owner; + + public Plugin getPlugin() { + return owner; + } + + public String getName() { + return name; + } + + public String getTooltip() { + return tooltip; + } + + public String getHelpMessage() { + return usage; + } + + public Command(String name, String tooltip, String helpMessage, Plugin owner) { + this.name = name; + this.tooltip = tooltip; + this.usage = helpMessage; + this.owner = owner; + } +} \ No newline at end of file diff --git a/paper-api/src/main/java/org/bukkit/plugin/CommandManager.java b/paper-api/src/main/java/org/bukkit/plugin/CommandManager.java new file mode 100644 index 0000000000..98747d2392 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/plugin/CommandManager.java @@ -0,0 +1,28 @@ +package org.bukkit.plugin; + +import org.bukkit.entity.Player; + +public interface CommandManager { + /** + * Registers all the commands belonging to a certain plugin. + * @param plugin + * @return + */ + public boolean registerCommands(Plugin plugin); + + /** + * Adds a command to the registeredCommands map. Returns true on success; false if name is already taken. + * + * @param command Name of command, without '/'-prefix. + * @return Returns true if command string was not already registered; false otherwise. + */ + public boolean registerCommand(String command, String tooltip, String helpMessage, Plugin plugin); + + /** Looks up given string in registeredCommands map and calls the onCommand method on the + * appropriate plugin if found. + * + * @param cmdLine command + arguments. Example: "/test abc 123" + * @return targetFound returns false if no target is found. + */ + public boolean dispatchCommand(Player sender, String cmdLine); +} diff --git a/paper-api/src/main/java/org/bukkit/plugin/CommandParserYaml.java b/paper-api/src/main/java/org/bukkit/plugin/CommandParserYaml.java new file mode 100644 index 0000000000..614b205a16 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/plugin/CommandParserYaml.java @@ -0,0 +1,29 @@ +package org.bukkit.plugin; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +public class CommandParserYaml { + + public static List parse(Plugin plugin) { + List cmds = new ArrayList(); + Object object = plugin.getDescription().getCommands(); + + @SuppressWarnings("unchecked") + Map> map = (Map>)object; + + if (map != null) { + for(Entry> entry : map.entrySet()) { + String description = entry.getValue().get("description").toString(); + String usageText = entry.getValue().get("usage").toString(); + + cmds.add(new Command(entry.getKey(), description, usageText, plugin)); + } + } + + return cmds; + } + +} diff --git a/paper-api/src/main/java/org/bukkit/plugin/Plugin.java b/paper-api/src/main/java/org/bukkit/plugin/Plugin.java index d63c647428..74d662ff52 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/Plugin.java +++ b/paper-api/src/main/java/org/bukkit/plugin/Plugin.java @@ -3,6 +3,7 @@ package org.bukkit.plugin; import java.io.File; import org.bukkit.Server; +import org.bukkit.entity.Player; import org.bukkit.util.config.Configuration; /** @@ -61,4 +62,9 @@ public interface Plugin { * Called when this plugin is enabled */ public void onEnable(); + + /** + * Called when a command registered by this plugin is received. + */ + public void onCommand(Player player, String command, String[] args); } diff --git a/paper-api/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java b/paper-api/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java index 72a26304a5..5a20026ec7 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java +++ b/paper-api/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java @@ -17,6 +17,7 @@ public final class PluginDescriptionFile { private String name = null; private String main = null; private String version = null; + private Object commands = null; @SuppressWarnings("unchecked") public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException { @@ -79,6 +80,10 @@ public final class PluginDescriptionFile { public String getMain() { return main; } + + public Object getCommands() { + return commands; + } private void loadMap(Map map) throws InvalidDescriptionException { try { @@ -104,6 +109,14 @@ public final class PluginDescriptionFile { } catch (ClassCastException ex) { throw new InvalidDescriptionException(ex, "main is of wrong type"); } + + try { + commands = map.get("commands"); + } catch (NullPointerException ex) { + throw new InvalidDescriptionException(ex, "command is not defined"); + } catch (ClassCastException ex) { + throw new InvalidDescriptionException(ex, "command is of wrong type"); + } } private Map saveMap() { @@ -111,6 +124,7 @@ public final class PluginDescriptionFile { map.put("name", name); map.put("main", main); map.put("version", version); + map.put("command", commands); return map; } } diff --git a/paper-api/src/main/java/org/bukkit/plugin/SimpleCommandManager.java b/paper-api/src/main/java/org/bukkit/plugin/SimpleCommandManager.java new file mode 100644 index 0000000000..6c4c8c6043 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/plugin/SimpleCommandManager.java @@ -0,0 +1,62 @@ +package org.bukkit.plugin; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.bukkit.entity.Player; + +public final class SimpleCommandManager implements CommandManager { + private final Map registeredCommands = new HashMap(); + + /** + * Registers all the commands specified in the description file of a plugin. + * @param plugin + * @return + */ + public boolean registerCommands(Plugin plugin) { + List commands = CommandParserYaml.parse(plugin); + boolean existsCommands = (commands != null); + + if (existsCommands) { + for(Command c : commands) { + if (!registerCommand(c)) + return false; // Command name conflict :( + } + } + return existsCommands; + } + + public boolean registerCommand(Command command) { + return registerCommand(command.getName(), command.getTooltip(), command.getHelpMessage(), command.getPlugin()); + } + + /** + * {@inheritDoc} + */ + public boolean registerCommand(String command, String tooltip, String helpMessage, Plugin plugin) { + boolean nameAvailable = (registeredCommands.get(command) == null); + + if (nameAvailable) { + Command newCmd = new Command(command, tooltip, helpMessage, plugin); + registeredCommands.put(command, newCmd); + } + return nameAvailable; + } + + /** + * {@inheritDoc} + */ + public boolean dispatchCommand(Player sender, String cmdLine) { + String[] args = cmdLine.split(" "); + + // Remove '/'-prefix and check if command is registered. + Command target = registeredCommands.get(args[0].substring(1)); + boolean targetFound = (target != null); + + if (targetFound) { + target.getPlugin().onCommand(sender, args[0], args); + } + return targetFound; + } +} diff --git a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index 859e016e6c..cbf82bc5c5 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java @@ -3,6 +3,7 @@ package org.bukkit.plugin.java; import java.io.File; import org.bukkit.Server; +import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginLoader; @@ -136,4 +137,11 @@ public abstract class JavaPlugin implements Plugin { } } } + + /** + * Called when a command registered by this plugin is received. + */ + public void onCommand(Player player, String command, String[] args) { + // default implementation: do nothing! + } }