Dieser Commit ist enthalten in:
Ursprung
2e9e4e2e49
Commit
518207f343
@ -65,7 +65,9 @@ class CommandPart<T> {
|
|||||||
this.parameter = parameter;
|
this.parameter = parameter;
|
||||||
this.parameterIndex = parameterIndex;
|
this.parameterIndex = parameterIndex;
|
||||||
|
|
||||||
validatePart();
|
if (optional != null && varArgType != null) {
|
||||||
|
throw new IllegalArgumentException("A vararg part can't have an optional part! In method " + parameter.getDeclaringExecutable() + " with parameter " + parameterIndex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void addValidator(AbstractValidator<T, Object> validator) {
|
void addValidator(AbstractValidator<T, Object> validator) {
|
||||||
@ -80,28 +82,18 @@ class CommandPart<T> {
|
|||||||
this.next = next;
|
this.next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validatePart() {
|
|
||||||
if (optional != null && varArgType != null) {
|
|
||||||
throw new IllegalArgumentException("A vararg part can't have an optional part! In method " + parameter.getDeclaringExecutable() + " with parameter " + parameterIndex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateArgumentArray(Consumer<Runnable> errors, List<Object> current, T sender, String[] args, int startIndex) {
|
public void generateArgumentArray(Consumer<Runnable> errors, List<Object> current, T sender, String[] args, int startIndex) {
|
||||||
if (varArgType != null) {
|
if (varArgType != null) {
|
||||||
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
||||||
for (int i = startIndex; i < args.length; i++) {
|
for (int i = startIndex; i < args.length; i++) {
|
||||||
CheckArgumentResult validArgument = checkArgument(null, sender, args, i);
|
CheckArgumentResult validArgument = checkArgument(null, sender, args, i);
|
||||||
if (!validArgument.success) {
|
if (!validArgument.success) throw new CommandParseException();
|
||||||
throw new CommandParseException();
|
|
||||||
}
|
|
||||||
Array.set(array, i - startIndex, validArgument.value);
|
Array.set(array, i - startIndex, validArgument.value);
|
||||||
}
|
}
|
||||||
for (AbstractValidator<T, Object> validator : validators) {
|
for (AbstractValidator<T, Object> validator : validators) {
|
||||||
if (!validator.validate(sender, array, (s, objects) -> {
|
if (!validator.validate(sender, array, (s, objects) -> {
|
||||||
errors.accept(() -> command.sendMessage(sender, s, objects));
|
errors.accept(() -> command.sendMessage(sender, s, objects));
|
||||||
})) {
|
})) throw new CommandParseException();
|
||||||
throw new CommandParseException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
current.add(array);
|
current.add(array);
|
||||||
return;
|
return;
|
||||||
@ -137,11 +129,8 @@ class CommandPart<T> {
|
|||||||
public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) {
|
public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) {
|
||||||
if (varArgType != null) {
|
if (varArgType != null) {
|
||||||
for (int i = startIndex; i < args.length - 1; i++) {
|
for (int i = startIndex; i < args.length - 1; i++) {
|
||||||
CheckArgumentResult validArgument = checkArgument((ignore) -> {
|
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, i);
|
||||||
}, sender, args, i);
|
if (!validArgument.success) return;
|
||||||
if (!validArgument.success) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Collection<String> strings = tabCompletes(sender, args, args.length - 1);
|
Collection<String> strings = tabCompletes(sender, args, args.length - 1);
|
||||||
if (strings != null) {
|
if (strings != null) {
|
||||||
@ -151,8 +140,7 @@ class CommandPart<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length - 1 > startIndex) {
|
if (args.length - 1 > startIndex) {
|
||||||
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {
|
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {}, sender, args, startIndex);
|
||||||
}, sender, args, startIndex);
|
|
||||||
if (checkArgumentResult.success && next != null) {
|
if (checkArgumentResult.success && next != null) {
|
||||||
next.generateTabComplete(current, sender, args, startIndex + 1);
|
next.generateTabComplete(current, sender, args, startIndex + 1);
|
||||||
return;
|
return;
|
||||||
|
@ -110,13 +110,9 @@ public class SubCommand<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<String> tabComplete(T sender, String[] args) {
|
List<String> tabComplete(T sender, String[] args) {
|
||||||
if (validator != null) {
|
if (validator != null && !validator.validate(sender, sender, (s, objects) -> {})) {
|
||||||
if (!validator.validate(sender, sender, (s, objects) -> {
|
|
||||||
// ignore
|
|
||||||
})) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (commandPart == null) {
|
if (commandPart == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -180,55 +176,38 @@ public class SubCommand<T> {
|
|||||||
|
|
||||||
private static final AbstractValidator<?, Object> STRING_SPACE_FILTER = (sender, value, messageSender) -> {
|
private static final AbstractValidator<?, Object> STRING_SPACE_FILTER = (sender, value, messageSender) -> {
|
||||||
if (!(value instanceof String)) return true;
|
if (!(value instanceof String)) return true;
|
||||||
String s = (String) value;
|
return !((String) value).contains(" ");
|
||||||
return !s.contains(" ");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private static AbstractValidator<?, Object> createMinValidator(Class<?> clazz, AbstractSWCommand.Min min) {
|
private static AbstractValidator<?, Object> createMinValidator(Class<?> clazz, AbstractSWCommand.Min min) {
|
||||||
Function<Number, Number> comparator;
|
Function<Number, Number> comparator;
|
||||||
if (clazz == int.class || clazz == Integer.class) {
|
if (clazz == int.class || clazz == Integer.class) {
|
||||||
int minValue = min.intValue();
|
comparator = number -> Integer.compare(number.intValue(), min.intValue());
|
||||||
comparator = number -> Integer.compare(number.intValue(), minValue);
|
|
||||||
} else if (clazz == long.class || clazz == Long.class) {
|
} else if (clazz == long.class || clazz == Long.class) {
|
||||||
long minValue = min.longValue();
|
comparator = number -> Long.compare(number.longValue(), min.longValue());
|
||||||
comparator = number -> Long.compare(number.longValue(), minValue);
|
|
||||||
} else if (clazz == float.class || clazz == Float.class) {
|
} else if (clazz == float.class || clazz == Float.class) {
|
||||||
float minValue = min.floatValue();
|
comparator = number -> Float.compare(number.floatValue(), min.floatValue());
|
||||||
comparator = number -> Float.compare(number.floatValue(), minValue);
|
|
||||||
} else if (clazz == double.class || clazz == Double.class) {
|
} else if (clazz == double.class || clazz == Double.class) {
|
||||||
double minValue = min.doubleValue();
|
comparator = number -> Double.compare(number.doubleValue(), min.doubleValue());
|
||||||
comparator = number -> Double.compare(number.doubleValue(), minValue);
|
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Min annotation is not supported for " + clazz);
|
throw new IllegalArgumentException("Min annotation is not supported for " + clazz);
|
||||||
}
|
}
|
||||||
if (min.inclusive()) {
|
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() >= (min.inclusive() ? 0 : 1);
|
||||||
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() >= 0;
|
|
||||||
} else {
|
|
||||||
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() > 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static AbstractValidator<?, Object> createMaxValidator(Class<?> clazz, AbstractSWCommand.Max max) {
|
private static AbstractValidator<?, Object> createMaxValidator(Class<?> clazz, AbstractSWCommand.Max max) {
|
||||||
Function<Number, Number> comparator;
|
Function<Number, Number> comparator;
|
||||||
if (clazz == int.class || clazz == Integer.class) {
|
if (clazz == int.class || clazz == Integer.class) {
|
||||||
int minValue = max.intValue();
|
comparator = number -> Integer.compare(number.intValue(), max.intValue());
|
||||||
comparator = number -> Integer.compare(number.intValue(), minValue);
|
|
||||||
} else if (clazz == long.class || clazz == Long.class) {
|
} else if (clazz == long.class || clazz == Long.class) {
|
||||||
long minValue = max.longValue();
|
comparator = number -> Long.compare(number.longValue(), max.longValue());
|
||||||
comparator = number -> Long.compare(number.longValue(), minValue);
|
|
||||||
} else if (clazz == float.class || clazz == Float.class) {
|
} else if (clazz == float.class || clazz == Float.class) {
|
||||||
float minValue = max.floatValue();
|
comparator = number -> Float.compare(number.floatValue(), max.floatValue());
|
||||||
comparator = number -> Float.compare(number.floatValue(), minValue);
|
|
||||||
} else if (clazz == double.class || clazz == Double.class) {
|
} else if (clazz == double.class || clazz == Double.class) {
|
||||||
double minValue = max.doubleValue();
|
comparator = number -> Double.compare(number.doubleValue(), max.doubleValue());
|
||||||
comparator = number -> Double.compare(number.doubleValue(), minValue);
|
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Max annotation is not supported for " + clazz);
|
throw new IllegalArgumentException("Max annotation is not supported for " + clazz);
|
||||||
}
|
}
|
||||||
if (max.inclusive()) {
|
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() <= (max.inclusive() ? 0 : -1);
|
||||||
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() <= 0;
|
|
||||||
} else {
|
|
||||||
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() < 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren