diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index bb39119..dc8c6a9 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -65,7 +65,9 @@ class CommandPart { 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 validator) { @@ -80,28 +82,18 @@ class CommandPart { 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 errors, List 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 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 { public void generateTabComplete(List 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 strings = tabCompletes(sender, args, args.length - 1); if (strings != null) { @@ -151,8 +140,7 @@ class CommandPart { } 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; diff --git a/src/de/steamwar/command/SubCommand.java b/src/de/steamwar/command/SubCommand.java index 0e75d7b..9084fe7 100644 --- a/src/de/steamwar/command/SubCommand.java +++ b/src/de/steamwar/command/SubCommand.java @@ -110,12 +110,8 @@ public class SubCommand { } List tabComplete(T sender, String[] args) { - if (validator != null) { - if (!validator.validate(sender, sender, (s, objects) -> { - // ignore - })) { - return null; - } + if (validator != null && !validator.validate(sender, sender, (s, objects) -> {})) { + return null; } if (commandPart == null) { return null; @@ -180,55 +176,38 @@ public class SubCommand { private static final AbstractValidator 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 createMinValidator(Class clazz, AbstractSWCommand.Min min) { Function 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 createMaxValidator(Class clazz, AbstractSWCommand.Max max) { Function 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); } }