Optimize some code
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-12-18 21:49:36 +01:00
Ursprung 2e9e4e2e49
Commit 518207f343
2 geänderte Dateien mit 21 neuen und 54 gelöschten Zeilen

Datei anzeigen

@ -65,7 +65,9 @@ class CommandPart<T> {
this.parameter = parameter;
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) {
@ -80,28 +82,18 @@ class CommandPart<T> {
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) {
if (varArgType != null) {
Object array = Array.newInstance(varArgType, args.length - startIndex);
for (int i = startIndex; i < args.length; i++) {
CheckArgumentResult validArgument = checkArgument(null, sender, args, i);
if (!validArgument.success) {
throw new CommandParseException();
}
if (!validArgument.success) throw new CommandParseException();
Array.set(array, i - startIndex, validArgument.value);
}
for (AbstractValidator<T, Object> validator : validators) {
if (!validator.validate(sender, array, (s, objects) -> {
errors.accept(() -> command.sendMessage(sender, s, objects));
})) {
throw new CommandParseException();
}
})) throw new CommandParseException();
}
current.add(array);
return;
@ -137,11 +129,8 @@ class CommandPart<T> {
public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) {
if (varArgType != null) {
for (int i = startIndex; i < args.length - 1; i++) {
CheckArgumentResult validArgument = checkArgument((ignore) -> {
}, sender, args, i);
if (!validArgument.success) {
return;
}
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, i);
if (!validArgument.success) return;
}
Collection<String> strings = tabCompletes(sender, args, args.length - 1);
if (strings != null) {
@ -151,8 +140,7 @@ class CommandPart<T> {
}
if (args.length - 1 > startIndex) {
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {
}, sender, args, startIndex);
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {}, sender, args, startIndex);
if (checkArgumentResult.success && next != null) {
next.generateTabComplete(current, sender, args, startIndex + 1);
return;

Datei anzeigen

@ -110,13 +110,9 @@ public class SubCommand<T> {
}
List<String> tabComplete(T sender, String[] args) {
if (validator != null) {
if (!validator.validate(sender, sender, (s, objects) -> {
// ignore
})) {
if (validator != null && !validator.validate(sender, sender, (s, objects) -> {})) {
return null;
}
}
if (commandPart == null) {
return null;
}
@ -180,55 +176,38 @@ public class SubCommand<T> {
private static final AbstractValidator<?, Object> STRING_SPACE_FILTER = (sender, value, messageSender) -> {
if (!(value instanceof String)) return true;
String s = (String) value;
return !s.contains(" ");
return !((String) value).contains(" ");
};
private static AbstractValidator<?, Object> createMinValidator(Class<?> clazz, AbstractSWCommand.Min min) {
Function<Number, Number> comparator;
if (clazz == int.class || clazz == Integer.class) {
int minValue = min.intValue();
comparator = number -> Integer.compare(number.intValue(), minValue);
comparator = number -> Integer.compare(number.intValue(), min.intValue());
} else if (clazz == long.class || clazz == Long.class) {
long minValue = min.longValue();
comparator = number -> Long.compare(number.longValue(), minValue);
comparator = number -> Long.compare(number.longValue(), min.longValue());
} else if (clazz == float.class || clazz == Float.class) {
float minValue = min.floatValue();
comparator = number -> Float.compare(number.floatValue(), minValue);
comparator = number -> Float.compare(number.floatValue(), min.floatValue());
} else if (clazz == double.class || clazz == Double.class) {
double minValue = min.doubleValue();
comparator = number -> Double.compare(number.doubleValue(), minValue);
comparator = number -> Double.compare(number.doubleValue(), min.doubleValue());
} else {
throw new IllegalArgumentException("Min annotation is not supported for " + clazz);
}
if (min.inclusive()) {
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() >= 0;
} else {
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() > 0;
}
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() >= (min.inclusive() ? 0 : 1);
}
private static AbstractValidator<?, Object> createMaxValidator(Class<?> clazz, AbstractSWCommand.Max max) {
Function<Number, Number> comparator;
if (clazz == int.class || clazz == Integer.class) {
int minValue = max.intValue();
comparator = number -> Integer.compare(number.intValue(), minValue);
comparator = number -> Integer.compare(number.intValue(), max.intValue());
} else if (clazz == long.class || clazz == Long.class) {
long minValue = max.longValue();
comparator = number -> Long.compare(number.longValue(), minValue);
comparator = number -> Long.compare(number.longValue(), max.longValue());
} else if (clazz == float.class || clazz == Float.class) {
float minValue = max.floatValue();
comparator = number -> Float.compare(number.floatValue(), minValue);
comparator = number -> Float.compare(number.floatValue(), max.floatValue());
} else if (clazz == double.class || clazz == Double.class) {
double minValue = max.doubleValue();
comparator = number -> Double.compare(number.doubleValue(), minValue);
comparator = number -> Double.compare(number.doubleValue(), max.doubleValue());
} else {
throw new IllegalArgumentException("Max annotation is not supported for " + clazz);
}
if (max.inclusive()) {
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() <= 0;
} else {
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() < 0;
}
return (sender, value, messageSender) -> comparator.apply((Number) value).intValue() <= (max.inclusive() ? 0 : -1);
}
}