From 4e6f73c63728ca5ef6bcb68db88dadc29feb46b0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 22 Jan 2023 22:04:00 +0100 Subject: [PATCH] Add ArrayLength visual update for tabcompletions --- .../steamwar/command/AbstractSWCommand.java | 40 +++++++++++++++++-- src/de/steamwar/command/CommandPart.java | 7 +++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 5cdb213..6bc3177 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -26,7 +26,10 @@ import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.*; import java.util.concurrent.TimeUnit; -import java.util.function.*; +import java.util.function.BiConsumer; +import java.util.function.BiPredicate; +import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; public abstract class AbstractSWCommand { @@ -608,7 +611,8 @@ public abstract class AbstractSWCommand { @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER}) @CommandMetaData.Parameter(handler = ArrayLength.Type.class) - @CommandMetaData.ImplicitValidator(handler = ArrayLength.Handler.class, order = 1) + @CommandMetaData.ImplicitTypeMapper(handler = ArrayLength.HandlerTypeMapper.class) + @CommandMetaData.ImplicitValidator(handler = ArrayLength.HandlerValidator.class, order = 1) protected @interface ArrayLength { int min() default 0; @@ -621,12 +625,40 @@ public abstract class AbstractSWCommand { } } - class Handler implements AbstractValidator { + class HandlerTypeMapper implements AbstractTypeMapper { + + private int max; + private AbstractTypeMapper inner; + + public HandlerTypeMapper(ArrayLength arrayLength, AbstractTypeMapper inner) { + this.max = arrayLength.max(); + this.inner = inner; + } + + @Override + public Object map(T sender, PreviousArguments previousArguments, String s) { + return inner.map(sender, previousArguments, s); + } + + @Override + public Collection tabCompletes(T sender, PreviousArguments previousArguments, String s) { + Object[] mapped = previousArguments.getMappedArg(0); + if (mapped.length >= max) return Collections.emptyList(); + return inner.tabCompletes(sender, previousArguments, s); + } + + @Override + public String normalize(T sender, String s) { + return inner.normalize(sender, s); + } + } + + class HandlerValidator implements AbstractValidator { private int min; private int max; - public Handler(ArrayLength arrayLength) { + public HandlerValidator(ArrayLength arrayLength) { this.min = arrayLength.min(); this.max = arrayLength.max(); } diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index 8a45791..971775a 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -129,11 +129,16 @@ class CommandPart { public void generateTabComplete(List current, T sender, String[] args, List mappedArgs, int startIndex) { if (varArgType != null) { + List currentArgs = new ArrayList<>(mappedArgs); + List varArgs = new ArrayList<>(); for (int i = startIndex; i < args.length - 1; i++) { CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, mappedArgs, i); if (!validArgument.success) return; + varArgs.add(validArgument.value); } - Collection strings = tabCompletes(sender, args, mappedArgs, args.length - 1); + + currentArgs.add(varArgs.toArray()); + Collection strings = tabCompletes(sender, args, currentArgs, args.length - 1); if (strings != null) { current.addAll(strings); }