diff options
author | VictorD <victor.danell@gmail.com> | 2011-01-18 01:12:50 +0100 |
---|---|---|
committer | VictorD <victor.danell@gmail.com> | 2011-01-18 01:12:50 +0100 |
commit | 0951ed5de8e6c21aa20797dbaa4106ab67bae06e (patch) | |
tree | fb8616b0ac0293d8fe2a83d5c7faa2922004021a /src | |
parent | d54b9dea8b54b31587416a19e9d4e81670eb451b (diff) | |
download | bukkit-0951ed5de8e6c21aa20797dbaa4106ab67bae06e.tar bukkit-0951ed5de8e6c21aa20797dbaa4106ab67bae06e.tar.gz bukkit-0951ed5de8e6c21aa20797dbaa4106ab67bae06e.tar.lz bukkit-0951ed5de8e6c21aa20797dbaa4106ab67bae06e.tar.xz bukkit-0951ed5de8e6c21aa20797dbaa4106ab67bae06e.zip |
Updated commands to have optional aliases, and to fallback to /pluginName:cmdName on name conflict.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/bukkit/command/Command.java | 52 | ||||
-rw-r--r-- | src/main/java/org/bukkit/command/CommandMap.java | 29 | ||||
-rw-r--r-- | src/main/java/org/bukkit/command/PluginCommand.java | 33 | ||||
-rw-r--r-- | src/main/java/org/bukkit/command/PluginCommandYamlParser.java | 51 | ||||
-rw-r--r-- | src/main/java/org/bukkit/command/SimpleCommandMap.java | 61 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/Command.java | 31 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/CommandManager.java | 28 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/CommandParserYaml.java | 37 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/Plugin.java | 5 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/SimpleCommandManager.java | 62 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/java/JavaPlugin.java | 5 |
11 files changed, 233 insertions, 161 deletions
diff --git a/src/main/java/org/bukkit/command/Command.java b/src/main/java/org/bukkit/command/Command.java new file mode 100644 index 00000000..3bb4cb69 --- /dev/null +++ b/src/main/java/org/bukkit/command/Command.java @@ -0,0 +1,52 @@ +package org.bukkit.command; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.entity.Player; + +public abstract class Command { + private final String name; + private List<String> aliases; + protected String tooltip = ""; + protected String usageMessage; + + public Command(String name) { + this.name = name; + this.aliases = new ArrayList<String>(); + this.usageMessage = "/" + name; + } + + public abstract boolean execute(Player player, String currentAlias, String[] args); + + public String getName() { + return name; + } + + public List<String> getAliases() { + return aliases; + } + + public String getTooltip() { + return tooltip; + } + + public String getUsage() { + return usageMessage; + } + + public Command setAliases(List<String> aliases) { + this.aliases = aliases; + return this; + } + + public Command setTooltip(String tooltip) { + this.tooltip = tooltip; + return this; + } + + public Command setUsage(String usage) { + this.usageMessage = usage; + return this; + } +}
\ No newline at end of file diff --git a/src/main/java/org/bukkit/command/CommandMap.java b/src/main/java/org/bukkit/command/CommandMap.java new file mode 100644 index 00000000..a60ebd17 --- /dev/null +++ b/src/main/java/org/bukkit/command/CommandMap.java @@ -0,0 +1,29 @@ +package org.bukkit.command; + +import java.util.List; +import org.bukkit.entity.Player; + +public interface CommandMap { + /** + * Registers all the commands belonging to a certain plugin. + * @param plugin + * @return + */ + public void registerAll(String fallbackPrefix, List<Command> commands); + + /** + * Registers a command. Returns true on success; false if name is already taken and fallback had to be used. + * + * @param a label for this command, without the '/'-prefix. + * @return Returns true if command was registered; false if label was already in use. + */ + public boolean register(String label, String fallbackPrefix, Command command); + + /** Looks for the requested command and executes it if found. + * + * @param cmdLine command + arguments. Example: "/test abc 123" + * @return targetFound returns false if no target is found. + */ + public boolean dispatch(Player sender, String cmdLine); + +} diff --git a/src/main/java/org/bukkit/command/PluginCommand.java b/src/main/java/org/bukkit/command/PluginCommand.java new file mode 100644 index 00000000..201645fc --- /dev/null +++ b/src/main/java/org/bukkit/command/PluginCommand.java @@ -0,0 +1,33 @@ +package org.bukkit.command; + +import java.awt.Color; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +public final class PluginCommand extends Command { + private final Plugin owningPlugin; + + public PluginCommand(String name, Plugin owner) { + super(name); + this.owningPlugin = owner; + this.usageMessage = ""; + } + + public boolean execute(Player player, String commandLabel, String[] args) { + boolean cmdSuccess = owningPlugin.onCommand(player, this, commandLabel, args); + if (!cmdSuccess && usageMessage != "") { + String tmpMsg = usageMessage.replace("<command>", commandLabel); + String[] usageLines = tmpMsg.split("\\n"); + for(String line: usageLines) { + while (line.length() > 0) { + int stripChars = (line.length() > 53 ? 53:line.length()); + player.sendMessage(ChatColor.RED + line.substring(0, stripChars)); + line = line.substring(stripChars); + } + } + } + return cmdSuccess; + } +}
\ No newline at end of file diff --git a/src/main/java/org/bukkit/command/PluginCommandYamlParser.java b/src/main/java/org/bukkit/command/PluginCommandYamlParser.java new file mode 100644 index 00000000..8aa60d78 --- /dev/null +++ b/src/main/java/org/bukkit/command/PluginCommandYamlParser.java @@ -0,0 +1,51 @@ +package org.bukkit.command; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.bukkit.plugin.Plugin; + +public class PluginCommandYamlParser { + + public static List<Command> parse(Plugin plugin) { + List<Command> pluginCmds = new ArrayList<Command>(); + Object object = plugin.getDescription().getCommands(); + if (object == null) + return pluginCmds; + + @SuppressWarnings("unchecked") + Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>)object; + + if (map != null) { + for(Entry<String, Map<String, Object>> entry : map.entrySet()) { + Command newCmd = new PluginCommand(entry.getKey(),plugin); + Object description = entry.getValue().get("description"); + Object usage = entry.getValue().get("usage"); + Object aliases = entry.getValue().get("aliases"); + + if (description != null) + newCmd.setTooltip(description.toString()); + + if (usage != null) { + newCmd.setUsage(usage.toString()); + } + + if (aliases != null) { + List<String> aliasList = new ArrayList<String>(); + + for(String a : aliases.toString().split(",")) { + aliasList.add(a); + } + + newCmd.setAliases(aliasList); + } + + pluginCmds.add(newCmd); + } + } + return pluginCmds; + } + +} diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java new file mode 100644 index 00000000..a0617ee3 --- /dev/null +++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java @@ -0,0 +1,61 @@ +package org.bukkit.command; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.bukkit.entity.Player; + +public final class SimpleCommandMap implements CommandMap { + private final Map<String, Command> knownCommands = new HashMap<String, Command>(); + + /** + * Registers multiple commands. Returns name of first command for which fallback had to be used if any. + * @param plugin + * @return + */ + public void registerAll(String fallbackPrefix, List<Command> commands) { + if (commands != null) { + for(Command c : commands) { + List<String> names = new ArrayList<String>(); + names.add(c.getName()); + names.addAll(c.getAliases()); + + for(String name : names) { + register(name, fallbackPrefix, c); + } + } + } + } + + /** + * {@inheritDoc} + */ + public boolean register(String name, String fallbackPrefix, Command command) { + boolean nameInUse = (knownCommands.get(command.getName()) != null); + if (nameInUse) + name = fallbackPrefix + ":" + name; + + knownCommands.put(name, command); + System.out.println("Adding cmd: " + name + " for plugin " + fallbackPrefix); + + return !nameInUse; + } + + /** + * {@inheritDoc} + */ + public boolean dispatch(Player sender, String commandLine) { + String[] args = commandLine.split(" "); + String sentCommandLabel = args[0].substring(1); + + Command target = knownCommands.get(sentCommandLabel); + boolean isRegisteredCommand = (target != null); + if (isRegisteredCommand) { + target.execute(sender, sentCommandLabel, args); + } + return isRegisteredCommand; + } + +} diff --git a/src/main/java/org/bukkit/plugin/Command.java b/src/main/java/org/bukkit/plugin/Command.java deleted file mode 100644 index d48e503e..00000000 --- a/src/main/java/org/bukkit/plugin/Command.java +++ /dev/null @@ -1,31 +0,0 @@ -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/src/main/java/org/bukkit/plugin/CommandManager.java b/src/main/java/org/bukkit/plugin/CommandManager.java deleted file mode 100644 index 98747d23..00000000 --- a/src/main/java/org/bukkit/plugin/CommandManager.java +++ /dev/null @@ -1,28 +0,0 @@ -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/src/main/java/org/bukkit/plugin/CommandParserYaml.java b/src/main/java/org/bukkit/plugin/CommandParserYaml.java deleted file mode 100644 index 50784ba1..00000000 --- a/src/main/java/org/bukkit/plugin/CommandParserYaml.java +++ /dev/null @@ -1,37 +0,0 @@ -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<Command> parse(Plugin plugin) { - List<Command> cmds = new ArrayList<Command>(); - Object object = plugin.getDescription().getCommands(); - - @SuppressWarnings("unchecked") - Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>)object; - - if (map != null) { - for(Entry<String, Map<String, Object>> entry : map.entrySet()) { - Object d = entry.getValue().get("description"); - Object u = entry.getValue().get("usage"); - String description = ""; - String usageText = ""; - - if (d != null) - description = d.toString(); - - if (u != null) - usageText = u.toString(); - - cmds.add(new Command(entry.getKey(), description, usageText, plugin)); - } - } - - return cmds; - } - -} diff --git a/src/main/java/org/bukkit/plugin/Plugin.java b/src/main/java/org/bukkit/plugin/Plugin.java index 74d662ff..73656c8f 100644 --- a/src/main/java/org/bukkit/plugin/Plugin.java +++ b/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.command.Command; import org.bukkit.entity.Player; import org.bukkit.util.config.Configuration; @@ -65,6 +66,8 @@ public interface Plugin { /** * Called when a command registered by this plugin is received. + * @param commandLabel + * @return TODO */ - public void onCommand(Player player, String command, String[] args); + public boolean onCommand(Player player, Command command, String commandLabel, String[] args); } diff --git a/src/main/java/org/bukkit/plugin/SimpleCommandManager.java b/src/main/java/org/bukkit/plugin/SimpleCommandManager.java deleted file mode 100644 index 6c4c8c60..00000000 --- a/src/main/java/org/bukkit/plugin/SimpleCommandManager.java +++ /dev/null @@ -1,62 +0,0 @@ -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<String, Command> registeredCommands = new HashMap<String, Command>(); - - /** - * Registers all the commands specified in the description file of a plugin. - * @param plugin - * @return - */ - public boolean registerCommands(Plugin plugin) { - List<Command> 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/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index cbf82bc5..69e50861 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/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.command.Command; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; @@ -141,7 +142,7 @@ 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! + public boolean onCommand(Player player, Command cmd, String commandLabel, String[] args) { + return false; // default implementation: do nothing! } } |