From 6cbee16c19bd60e3fa11c5bc24c83d5c27d156b0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 22 Jan 2023 13:45:22 +0100 Subject: [PATCH] Remove Quotable because it was a dumb idea to add Add AbstractSWCommand.Length and AbstractSWCommand.ArrayLength --- .../steamwar/command/AbstractSWCommand.java | 177 +++++++++++------- src/de/steamwar/command/CommandMetaData.java | 8 +- src/de/steamwar/command/SWCommandUtils.java | 10 +- src/de/steamwar/command/SubCommand.java | 8 - .../de/steamwar/command/ArgumentCommand.java | 2 +- .../steamwar/command/ArgumentCommandTest.java | 23 --- 6 files changed, 117 insertions(+), 111 deletions(-) diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 86a00f5..9b0264f 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -21,12 +21,14 @@ package de.steamwar.command; import java.lang.annotation.*; import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -67,48 +69,11 @@ public abstract class AbstractSWCommand { protected void sendMessage(T sender, String message, Object[] args) {} - private String[] quote(String[] args) { - List list = new ArrayList<>(); - StringBuilder builder = new StringBuilder(); - boolean quote = false; - for (String arg : args) { - if (arg.startsWith("\"") && arg.endsWith("\"")) { - list.add(arg); - continue; - } - if (arg.startsWith("\"")) { - quote = true; - builder.append(arg); - continue; - } - if (arg.endsWith("\"")) { - quote = false; - builder.append(" ").append(arg); - list.add(builder.toString()); - builder = new StringBuilder(); - continue; - } - if (quote) { - builder.append(" ").append(arg); - continue; - } - list.add(arg); - } - if (quote) { - builder.append("\""); - } - if (builder.length() > 0) { - list.add(builder.toString()); - } - return list.toArray(new String[0]); - } - protected final void execute(T sender, String alias, String[] args) { initialize(); - String[] finalArgs = quote(args); List errors = new ArrayList<>(); try { - if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, finalArgs))) { + if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, args))) { errors.forEach(Runnable::run); } } catch (CommandFrameworkException e) { @@ -119,11 +84,10 @@ public abstract class AbstractSWCommand { protected final List tabComplete(T sender, String alias, String[] args) throws IllegalArgumentException { initialize(); - String[] finalArgs = quote(args); String string = args[args.length - 1].toLowerCase(); return commandList.stream() .filter(s -> !s.noTabComplete) - .map(s -> s.tabComplete(sender, finalArgs)) + .map(s -> s.tabComplete(sender, args)) .filter(Objects::nonNull) .flatMap(Collection::stream) .filter(s -> !s.isEmpty()) @@ -185,9 +149,16 @@ public abstract class AbstractSWCommand { private boolean validateMethod(Method method) { if (!checkType(method.getAnnotations(), method.getReturnType(), annotation -> { CommandMetaData.Method methodMetaData = annotation.annotationType().getAnnotation(CommandMetaData.Method.class); - if (methodMetaData == null) return null; - if (method.getParameterCount() > methodMetaData.maxParameterCount() || method.getParameterCount() < methodMetaData.minParameterCount()) return new Class[0]; - return methodMetaData.value(); + if (methodMetaData == null) return aClass -> true; + if (method.getParameterCount() > methodMetaData.maxParameterCount() || method.getParameterCount() < methodMetaData.minParameterCount()) return aClass -> false; + return aClass -> { + Class[] types = methodMetaData.value(); + if (types == null) return true; + for (Class type : types) { + if (type.isAssignableFrom(aClass)) return true; + } + return false; + }; }, "The method '" + method + "'")) return false; boolean valid = true; for (Parameter parameter : method.getParameters()) { @@ -195,26 +166,32 @@ public abstract class AbstractSWCommand { if (parameter.isVarArgs()) type = type.getComponentType(); if (!checkType(parameter.getAnnotations(), type, annotation -> { CommandMetaData.Parameter parameterMetaData = annotation.annotationType().getAnnotation(CommandMetaData.Parameter.class); - if (parameterMetaData == null) return null; - return parameterMetaData.value(); + if (parameterMetaData == null) return aClass -> true; + Class handler = parameterMetaData.handler(); + if (Predicate.class.isAssignableFrom(handler)) { + try { + return (Predicate>) handler.getConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + } + } + return aClass -> { + Class[] types = parameterMetaData.value(); + if (types == null) return true; + for (Class current : types) { + if (current.isAssignableFrom(aClass)) return true; + } + return false; + }; }, "The parameter '" + parameter + "'")) valid = false; } return valid; } - private boolean checkType(Annotation[] annotations, Class clazz, Function[]> toApplicableTypes, String warning) { + private boolean checkType(Annotation[] annotations, Class clazz, Function>> toApplicableTypes, String warning) { boolean valid = true; for (Annotation annotation : annotations) { - Class[] types = toApplicableTypes.apply(annotation); - if (types == null) continue; - boolean applicable = false; - for (Class type : types) { - if (type.isAssignableFrom(clazz)) { - applicable = true; - break; - } - } - if (!applicable) { + Predicate> predicate = toApplicableTypes.apply(annotation); + if (!predicate.test(clazz)) { commandSystemWarning(() -> warning + " is using an unsupported annotation of type '" + annotation.annotationType().getName() + "'"); valid = false; } @@ -506,15 +483,6 @@ public abstract class AbstractSWCommand { protected @interface AllowNull { } - /** - * This annotation is used to mark a String to be quotable with multiple words. - */ - @Retention(RetentionPolicy.RUNTIME) - @Target({ElementType.PARAMETER}) - @CommandMetaData.Parameter({String.class}) - protected @interface Quotable { - } - @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER}) @CommandMetaData.Parameter({int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class, Double.class}) @@ -586,4 +554,83 @@ public abstract class AbstractSWCommand { throw new IllegalArgumentException(type + " annotation is not supported for " + clazz); } } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.PARAMETER}) + @CommandMetaData.ImplicitTypeMapper(handler = Length.Handler.class) + protected @interface Length { + int min() default 0; + int max() default Integer.MAX_VALUE; + + class Handler implements AbstractTypeMapper { + + private int min; + private int max; + private AbstractTypeMapper inner; + + public Handler(Length length, AbstractTypeMapper inner) { + this.min = length.min(); + this.max = length.max(); + this.inner = inner; + } + + @Override + public Object map(T sender, PreviousArguments previousArguments, String s) { + if (s.length() < min || s.length() > max) return null; + return inner.map(sender, previousArguments, s); + } + + @Override + public Collection tabCompletes(T sender, PreviousArguments previousArguments, String s) { + List tabCompletes = inner.tabCompletes(sender, previousArguments, s) + .stream() + .filter(str -> str.length() >= min) + .map(str -> str.substring(0, Math.min(str.length(), max))) + .collect(Collectors.toList()); + if (s.length() < min) { + tabCompletes.add(0, s); + } + return tabCompletes; + } + + @Override + public String normalize(T sender, String s) { + return inner.normalize(sender, s); + } + } + } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.PARAMETER}) + @CommandMetaData.Parameter(handler = ArrayLength.Type.class) + @CommandMetaData.ImplicitValidator(handler = ArrayLength.Handler.class, order = 2) + protected @interface ArrayLength { + int min() default 0; + int max() default Integer.MAX_VALUE; + + class Type implements Predicate> { + @Override + public boolean test(Class clazz) { + return clazz.isArray(); + } + } + + class Handler implements AbstractValidator { + + private int min; + private int max; + + public Handler(ArrayLength arrayLength) { + this.min = arrayLength.min(); + this.max = arrayLength.max(); + } + + @Override + public boolean validate(T sender, Object value, MessageSender messageSender) { + if (value == null) return false; + int length = Array.getLength(value); + return length >= min && length <= max; + } + } + } } diff --git a/src/de/steamwar/command/CommandMetaData.java b/src/de/steamwar/command/CommandMetaData.java index fdb1ea2..33909ac 100644 --- a/src/de/steamwar/command/CommandMetaData.java +++ b/src/de/steamwar/command/CommandMetaData.java @@ -19,10 +19,7 @@ package de.steamwar.command; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; +import java.lang.annotation.*; public @interface CommandMetaData { @@ -43,7 +40,8 @@ public @interface CommandMetaData { @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) @interface Parameter { - Class[] value(); + Class[] value() default {}; + Class handler() default void.class; } /** diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 32b7fcc..c06d584 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -61,15 +61,7 @@ public class SWCommandUtils { addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble, true))); addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt, false))); addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong, false))); - MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> { - if (s.startsWith("\"") && s.endsWith("\"")) { - return s.substring(1, s.length() - 1); - } - if (s.startsWith("'") && s.endsWith("'")) { - return s.substring(1, s.length() - 1); - } - return s; - }, Collections::singletonList)); + MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList)); } public static , K, V> void init(SWTypeMapperCreator swTypeMapperCreator) { diff --git a/src/de/steamwar/command/SubCommand.java b/src/de/steamwar/command/SubCommand.java index 961cf29..d74423b 100644 --- a/src/de/steamwar/command/SubCommand.java +++ b/src/de/steamwar/command/SubCommand.java @@ -150,9 +150,6 @@ public class SubCommand { CommandPart commandPart = new CommandPart<>(command, typeMapper, varArgType, optionalValue != null ? optionalValue.value() : null, parameter, i); commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG()); - if (parameter.getAnnotation(AbstractSWCommand.Quotable.class) == null) { - commandPart.addValidator((AbstractValidator) STRING_SPACE_FILTER); - } handleImplicitTypeValidator(parameter, commandPart, localValidator); if (parameter.getAnnotation(AbstractSWCommand.AllowNull.class) == null) { commandPart.addValidator((AbstractValidator) NULL_FILTER); @@ -270,9 +267,4 @@ public class SubCommand { } private static final AbstractValidator NULL_FILTER = (sender, value, messageSender) -> value != null; - - private static final AbstractValidator STRING_SPACE_FILTER = (sender, value, messageSender) -> { - if (!(value instanceof String)) return true; - return !((String) value).contains(" "); - }; } diff --git a/testsrc/de/steamwar/command/ArgumentCommand.java b/testsrc/de/steamwar/command/ArgumentCommand.java index ad1c1ec..a107270 100644 --- a/testsrc/de/steamwar/command/ArgumentCommand.java +++ b/testsrc/de/steamwar/command/ArgumentCommand.java @@ -54,7 +54,7 @@ public class ArgumentCommand extends TestSWCommand { } @Register - public void argument(String sender, @Quotable String arg) { + public void argument(String sender, String arg) { throw new ExecutionIdentifier("RunArgument with String"); } } diff --git a/testsrc/de/steamwar/command/ArgumentCommandTest.java b/testsrc/de/steamwar/command/ArgumentCommandTest.java index fd4fb5f..0af9327 100644 --- a/testsrc/de/steamwar/command/ArgumentCommandTest.java +++ b/testsrc/de/steamwar/command/ArgumentCommandTest.java @@ -94,29 +94,6 @@ public class ArgumentCommandTest { } } - @Test - public void testString() { - ArgumentCommand cmd = new ArgumentCommand(); - try { - cmd.execute("test", "", new String[]{"Hello World"}); - assert false; - } catch (Exception e) { - assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with String"); - } - try { - cmd.execute("test", "", new String[]{"\"Hello World\""}); - assert false; - } catch (Exception e) { - assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with String"); - } - try { - cmd.execute("test", "", new String[]{"\"Hello", "World\""}); - assert false; - } catch (Exception e) { - assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with String"); - } - } - @Test public void testTabComplete() { ArgumentCommand cmd = new ArgumentCommand();