From 5050580774202bbed2a9d2618f1010b39d5b5c69 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 16 Aug 2022 17:11:16 +0200 Subject: [PATCH] Add better boolean handling for StaticValue annotation --- src/de/steamwar/command/AbstractSWCommand.java | 6 ++++-- src/de/steamwar/command/SWCommandUtils.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 1de5785..aa33a64 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -371,10 +371,12 @@ public abstract class AbstractSWCommand { * and can be set to true if you want to allow int as well as boolean as annotated parameter types. * The value array needs to be at least 2 long for this flag to be considered. * While using an int, the value will represent the index into the value array. - * While using a boolean, the value array must only be 2 long and the value will be {@code false} - * for the first index and {@code true} for the second index. + * While using a boolean, the {@link #falseValues()} defines which indices are + * considered {@code false} or {@code true}. */ boolean allowISE() default false; + + int[] falseValues() default { 0 }; } @Retention(RetentionPolicy.RUNTIME) diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 9620631..3cac23c 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -136,11 +136,13 @@ public class SWCommandUtils { return createMapper(staticValue.value()); } if (staticValue.allowISE()) { - if ((parameter.getType() == boolean.class || parameter.getType() == Boolean.class) && staticValue.value().length == 2) { + if ((parameter.getType() == boolean.class || parameter.getType() == Boolean.class)) { List tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value())); + Set trueValues = new HashSet<>(); + for (int i : staticValue.falseValues()) trueValues.add(i); return createMapper(s -> { int index = tabCompletes.indexOf(s); - return index == -1 ? null : index != 0; + return index == -1 ? null : !trueValues.contains(index); }, (commandSender, s) -> tabCompletes); } if ((parameter.getType() == int.class || parameter.getType() == Integer.class) && staticValue.value().length >= 2) {