SteamWar/SpigotCore
Archiviert
13
0

Fix varArgs, fix help

Dieser Commit ist enthalten in:
yoyosource 2021-03-30 16:08:51 +02:00
Ursprung a7bf487336
Commit ce2cd3d1bf
3 geänderte Dateien mit 39 neuen und 23 gelöschten Zeilen

Datei anzeigen

@ -135,7 +135,16 @@ public abstract class SWCommand {
commandSet.add(new SubCommand(this, method, anno.value())); commandSet.add(new SubCommand(this, method, anno.value()));
}); });
commandSet.sort(Comparator.comparingInt(o -> -o.subCommand.length)); this.commandSet.sort((o1, o2) -> {
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
if (compare != 0) {
return compare;
} else {
int i1 = o1.varArgType != null ? Integer.MAX_VALUE : o1.arguments.length;
int i2 = o2.varArgType != null ? Integer.MAX_VALUE : o2.arguments.length;
return Integer.compare(i1, i2);
}
});
commandHelpSet.sort(Comparator.comparingInt(o -> -o.subCommand.length)); commandHelpSet.sort(Comparator.comparingInt(o -> -o.subCommand.length));
} }
} }

Datei anzeigen

@ -110,26 +110,33 @@ public class SWCommandUtils {
index++; index++;
} }
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) { if (varArgType != null && index > args.length - 1) {
arguments[i + 1] = parameters[i].map(args[index]); Object varArgument = Array.newInstance(varArgType, 0);
index++;
if (arguments[i + 1] == null) {
throw new CommandParseException();
}
}
if (varArgType != null) {
int length = args.length - parameters.length + 1;
Object varArgument = Array.newInstance(varArgType, length);
arguments[arguments.length - 1] = varArgument; arguments[arguments.length - 1] = varArgument;
} else {
for (int i = 0; i < length; i++) { for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) {
Object value = parameters[parameters.length - 1].map(args[index]); System.out.println(index);
if (value == null) { arguments[i + 1] = parameters[i].map(args[index]);
System.out.println(arguments[i + 1]);
index++;
if (arguments[i + 1] == null) {
throw new CommandParseException(); throw new CommandParseException();
} }
Array.set(varArgument, i, value); }
index++;
if (varArgType != null) {
int length = args.length - parameters.length - subCommand.length + 1;
Object varArgument = Array.newInstance(varArgType, length);
arguments[arguments.length - 1] = varArgument;
for (int i = 0; i < length; i++) {
Object value = parameters[parameters.length - 1].map(args[index]);
if (value == null) {
throw new CommandParseException();
}
Array.set(varArgument, i, value);
index++;
}
} }
} }
return arguments; return arguments;

Datei anzeigen

@ -32,9 +32,9 @@ class SubCommand {
private SWCommand swCommand; private SWCommand swCommand;
private Method method; private Method method;
String[] subCommand; String[] subCommand;
private TypeMapper<?>[] arguments; TypeMapper<?>[] arguments;
private Function<CommandSender, ?> commandSenderFunction; private Function<CommandSender, ?> commandSenderFunction;
private Class<?> varArgType = null; Class<?> varArgType = null;
public SubCommand(SWCommand swCommand, Method method, String[] subCommand) { public SubCommand(SWCommand swCommand, Method method, String[] subCommand) {
this(swCommand, method, subCommand, new HashMap<>()); this(swCommand, method, subCommand, new HashMap<>());
@ -81,10 +81,10 @@ class SubCommand {
} }
boolean invoke(CommandSender commandSender, String[] args) { boolean invoke(CommandSender commandSender, String[] args) {
if (args.length < arguments.length - 1) { if (args.length < arguments.length + subCommand.length - (varArgType != null ? 1 : 0)) {
return false; return false;
} }
if (varArgType == null && args.length > arguments.length) { if (varArgType == null && args.length > arguments.length + subCommand.length) {
return false; return false;
} }
try { try {
@ -101,7 +101,7 @@ class SubCommand {
} }
List<String> tabComplete(CommandSender commandSender, String[] args) { List<String> tabComplete(CommandSender commandSender, String[] args) {
if (varArgType == null && args.length < arguments.length - 1) { if (varArgType == null && args.length < arguments.length + subCommand.length - 1) {
return null; return null;
} }
List<String> argsList = new LinkedList<>(Arrays.asList(args)); List<String> argsList = new LinkedList<>(Arrays.asList(args));