[Bleeding] Improve alias system.

Adds a large expansion of the aliases system. Aliases can now take arguments,
reorder their arguments, and only pass certain arguments to certain commands.
New syntax added to the aliases are $1 for optional arguments, $$1 for
required arguments, $1- for optionally using all the arguments from the
specified position onward, and $$1- to do the same thing but require at least
the specified position exist. These exist for numbers 1 through 9. You are
able to pass arguments to one command of a multiple command argument and not
others. You can also use the argument as a prefix and/or suffix. A raw $ can
be represented in the arguments by using \$.

Examples:

aliases:
# Usage: /testobjective score_deaths 1 5
testobjective:
- "testfor @p[$$1=$$3,$$1_min=$$2]"

# Usage: /ban Amaranthus Because reasons
ban:
- ban $$1 $2-
- say Banned $$1

# Usage: /icanhasbukkit
icanhasbukkit:
- version

# Usage: /icanhasplugin HomeBukkit
icanhasplugin:
- version $$1

One change from the previous aliases system is that commands are no longer
passed all arguments implicitly. You must explicitly pass the arguments
you want to pass to the command.
This commit is contained in:
t00thpick1
2014-02-08 04:07:11 -05:00
committed by Travis Watkins
parent d7e0197e9d
commit 5023fe375e
2 changed files with 128 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ package org.bukkit.command;
import static org.bukkit.util.Java15Compat.Arrays_copyOfRange;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -256,27 +257,27 @@ public class SimpleCommandMap implements CommandMap {
Map<String, String[]> values = server.getCommandAliases();
for (String alias : values.keySet()) {
String[] targetNames = values.get(alias);
List<Command> targets = new ArrayList<Command>();
String[] commandStrings = values.get(alias);
List<String> targets = new ArrayList<String>();
StringBuilder bad = new StringBuilder();
for (String name : targetNames) {
Command command = getCommand(name);
for (String commandString : commandStrings) {
String[] commandArgs = commandString.split(" ");
Command command = getCommand(commandArgs[0]);
if (command == null) {
if (bad.length() > 0) {
bad.append(", ");
}
bad.append(name);
bad.append(commandString);
} else {
targets.add(command);
targets.add(commandString);
}
}
// 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])));
knownCommands.put(alias.toLowerCase(), new FormattedCommandAlias(alias.toLowerCase(), targets.toArray(new String[targets.size()])));
} else {
knownCommands.remove(alias.toLowerCase());
}