13
0
geforkt von Mirrors/Paper

[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.

By: t00thpick1 <t00thpick1dirko@gmail.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2014-02-08 04:07:11 -05:00
Ursprung 2a23e6bfaf
Commit 67a85bef54
2 geänderte Dateien mit 128 neuen und 8 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,119 @@
package org.bukkit.command;
import java.util.ArrayList;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.RemoteServerCommandEvent;
import org.bukkit.event.server.ServerCommandEvent;
public class FormattedCommandAlias extends Command {
private final String[] formatStrings;
public FormattedCommandAlias(String alias, String[] formatStrings) {
super(alias);
this.formatStrings = formatStrings;
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
boolean result = false;
ArrayList<String> commands = new ArrayList<String>();
for (String formatString : formatStrings) {
try {
if (sender instanceof Player) {
PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent((Player) sender, "/" + formatString);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return false;
} else {
formatString = event.getMessage().substring(1);
}
} else if (sender instanceof RemoteConsoleCommandSender) {
RemoteServerCommandEvent event = new RemoteServerCommandEvent(sender, formatString);
Bukkit.getPluginManager().callEvent(event);
formatString = event.getCommand();
} else if (sender instanceof ConsoleCommandSender) {
ServerCommandEvent event = new ServerCommandEvent(sender, formatString);
Bukkit.getPluginManager().callEvent(event);
formatString = event.getCommand();
}
commands.add(buildCommand(formatString, args));
} catch (Throwable throwable) {
if (throwable instanceof IllegalArgumentException) {
sender.sendMessage(throwable.getMessage());
} else {
sender.sendMessage(org.bukkit.ChatColor.RED + "An internal error occurred while attempting to perform this command");
}
Bukkit.getLogger().log(Level.WARNING, "Failed to parse command alias " + commandLabel + ": " + formatString, throwable);
return false;
}
}
for (String command : commands) {
result |= Bukkit.dispatchCommand(sender, command);
}
return result;
}
private String buildCommand(String formatString, String[] args) {
int index = formatString.indexOf("$");
while (index != -1) {
int start = index;
boolean required = false;
if (formatString.charAt(index + 1) == '$') {
required = true;
// Move index past the second $
index++;
}
// Move index past the $
index++;
int position = Character.getNumericValue(formatString.charAt(index)) - 1;
// Move index past the position
index++;
boolean rest = false;
if (index < formatString.length() && formatString.charAt(index) == '-') {
rest = true;
// Move index past the -
index++;
}
int end = index;
if (required && position >= args.length) {
throw new IllegalArgumentException("Missing required argument " + (position + 1));
}
String replacement = null;
if (rest && position < args.length) {
StringBuilder builder = new StringBuilder();
for (int i = position; i < args.length; i++) {
if (i != position) {
builder.append(' ');
}
builder.append(args[i]);
}
replacement = builder.toString();
} else if (position < args.length) {
replacement = args[position];
}
if (replacement != null && replacement.length() > 0) {
formatString = formatString.substring(0, start) + replacement + formatString.substring(end);
// Move index past the replaced data so we don't process it again
index = start + replacement.length();
}
index = formatString.indexOf("$", index);
}
return formatString;
}
}

Datei anzeigen

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