Fix varArgs, fix help
Dieser Commit ist enthalten in:
Ursprung
a7bf487336
Commit
ce2cd3d1bf
@ -135,7 +135,16 @@ public abstract class SWCommand {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -110,26 +110,33 @@ public class SWCommandUtils {
|
||||
index++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) {
|
||||
arguments[i + 1] = parameters[i].map(args[index]);
|
||||
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);
|
||||
if (varArgType != null && index > args.length - 1) {
|
||||
Object varArgument = Array.newInstance(varArgType, 0);
|
||||
arguments[arguments.length - 1] = varArgument;
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
Object value = parameters[parameters.length - 1].map(args[index]);
|
||||
if (value == null) {
|
||||
} else {
|
||||
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) {
|
||||
System.out.println(index);
|
||||
arguments[i + 1] = parameters[i].map(args[index]);
|
||||
System.out.println(arguments[i + 1]);
|
||||
index++;
|
||||
if (arguments[i + 1] == null) {
|
||||
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;
|
||||
|
@ -32,9 +32,9 @@ class SubCommand {
|
||||
private SWCommand swCommand;
|
||||
private Method method;
|
||||
String[] subCommand;
|
||||
private TypeMapper<?>[] arguments;
|
||||
TypeMapper<?>[] arguments;
|
||||
private Function<CommandSender, ?> commandSenderFunction;
|
||||
private Class<?> varArgType = null;
|
||||
Class<?> varArgType = null;
|
||||
|
||||
public SubCommand(SWCommand swCommand, Method method, String[] subCommand) {
|
||||
this(swCommand, method, subCommand, new HashMap<>());
|
||||
@ -81,10 +81,10 @@ class SubCommand {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
if (varArgType == null && args.length > arguments.length) {
|
||||
if (varArgType == null && args.length > arguments.length + subCommand.length) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
@ -101,7 +101,7 @@ class SubCommand {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
List<String> argsList = new LinkedList<>(Arrays.asList(args));
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren