From f71d7d0e9bb3d4968cd8384b10d2a000214f5edf Mon Sep 17 00:00:00 2001 From: Dinnerbone Date: Wed, 22 Jun 2011 19:07:07 +0100 Subject: The server config can now specify aliases to multiple (or none) commands, for example "debug: [version, plugin]" to run both version and plugin, or "plugins: []" to disable the plugins command --- src/main/java/org/bukkit/Server.java | 2 +- .../org/bukkit/command/MultipleCommandAlias.java | 25 +++++++++++++++++ .../java/org/bukkit/command/SimpleCommandMap.java | 32 +++++++++++++++++----- 3 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/bukkit/command/MultipleCommandAlias.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java index abe41f05..124e5d08 100644 --- a/src/main/java/org/bukkit/Server.java +++ b/src/main/java/org/bukkit/Server.java @@ -270,7 +270,7 @@ public interface Server { * * @return Map of aliases to command names */ - public Map getCommandAliases(); + public Map getCommandAliases(); /** * Gets the radius, in blocks, around each worlds spawn point to protect diff --git a/src/main/java/org/bukkit/command/MultipleCommandAlias.java b/src/main/java/org/bukkit/command/MultipleCommandAlias.java new file mode 100644 index 00000000..e1474542 --- /dev/null +++ b/src/main/java/org/bukkit/command/MultipleCommandAlias.java @@ -0,0 +1,25 @@ + +package org.bukkit.command; + +/** + * Represents a command that delegates to one or more other commands + */ +public class MultipleCommandAlias extends Command { + private Command[] commands; + + public MultipleCommandAlias(String name, Command[] commands) { + super(name); + this.commands = commands; + } + + @Override + public boolean execute(CommandSender sender, String commandLabel, String[] args) { + boolean result = false; + + for (Command command : commands) { + result |= command.execute(sender, commandLabel, args); + } + + return result; + } +} diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java index dfb066af..7a5e48fa 100644 --- a/src/main/java/org/bukkit/command/SimpleCommandMap.java +++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java @@ -151,17 +151,35 @@ public final class SimpleCommandMap implements CommandMap { } public void registerServerAliases() { - Map values = server.getCommandAliases(); + Map values = server.getCommandAliases(); for (String alias : values.keySet()) { - String target = values.get(alias); - Command command = getCommand(target); + String[] targetNames = values.get(alias); + List targets = new ArrayList(); + String bad = ""; - if (command != null) { - // We register these as commands so they have absolute priority. - knownCommands.put(alias.toLowerCase(), command); + for (String name : targetNames) { + Command command = getCommand(name); + + if (command == null) { + if (bad.length() > 0) { + bad += ", "; + } + } else { + targets.add(command); + } + } + + // We register these as commands so they have absolute priority. + + if (targets.size() > 0) { + knownCommands.put(alias.toLowerCase(), new MultipleCommandAlias(alias.toLowerCase(), targets.toArray(new Command[0]))); } else { - server.getLogger().warning("Could not register custom alias '" + alias + "' to command '" + target + "' because the command does not exist."); + knownCommands.remove(alias.toLowerCase()); + } + + if (bad.length() > 0) { + server.getLogger().warning("The following command(s) could not be aliased under '" + alias + "' because they do not exist: " + bad); } } } -- cgit v1.2.3