From b3fffe398ad2a9bb263d66306c527c6683abb046 Mon Sep 17 00:00:00 2001 From: jojo Date: Mon, 28 Dec 2020 11:34:43 +0100 Subject: [PATCH] Add number TabCompleting --- .../src/de/steamwar/command/Argument.java | 8 ++++++-- .../src/de/steamwar/command/ArgumentType.java | 14 ++++++++------ .../src/de/steamwar/command/SWCommandBundle.java | 11 ++++++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/Argument.java b/SpigotCore_Main/src/de/steamwar/command/Argument.java index 591cc38..b7ec53b 100644 --- a/SpigotCore_Main/src/de/steamwar/command/Argument.java +++ b/SpigotCore_Main/src/de/steamwar/command/Argument.java @@ -71,8 +71,12 @@ public class Argument { public Optional> tabCompleteSupplier(String s) { try { - // Check if mappable - if (!s.isEmpty()) argumentType.mapper.apply(s); + if (!s.isEmpty()) { + // Check if mappable + T argumentMapped = argumentType.mapper.apply(s); + // Check number constraints if needed + if (argumentType.number && !constraint.test(argumentMapped)) return Optional.empty(); + } List strings = Arrays.stream(tabCompletes).filter(t -> t.startsWith(s)).collect(Collectors.toList()); if (strings.isEmpty()) strings.add(s); return Optional.of(strings); diff --git a/SpigotCore_Main/src/de/steamwar/command/ArgumentType.java b/SpigotCore_Main/src/de/steamwar/command/ArgumentType.java index f24cc1c..80f5531 100644 --- a/SpigotCore_Main/src/de/steamwar/command/ArgumentType.java +++ b/SpigotCore_Main/src/de/steamwar/command/ArgumentType.java @@ -25,16 +25,18 @@ import java.util.function.Function; public class ArgumentType { - public static final ArgumentType INT = new ArgumentType<>(Integer::parseInt); - public static final ArgumentType LONG = new ArgumentType<>(Long::parseLong); - public static final ArgumentType FLOAT = new ArgumentType<>(Float::parseFloat); - public static final ArgumentType DOUBLE = new ArgumentType<>(Double::parseDouble); - public static final ArgumentType STRING = new ArgumentType<>((s) -> s); + public static final ArgumentType INT = new ArgumentType<>(Integer::parseInt, true); + public static final ArgumentType LONG = new ArgumentType<>(Long::parseLong, true); + public static final ArgumentType FLOAT = new ArgumentType<>(Float::parseFloat, true); + public static final ArgumentType DOUBLE = new ArgumentType<>(Double::parseDouble, true); + public static final ArgumentType STRING = new ArgumentType<>((s) -> s, false); Function mapper; + boolean number; - private ArgumentType(Function mapper) { + private ArgumentType(Function mapper, boolean number) { this.mapper = mapper; + this.number = number; } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java index e3954ca..44f9bdf 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java @@ -37,7 +37,7 @@ public class SWCommandBundle { return this; } - public SWCommandBundle add(SWCommandBundle swCommandBundle) { + public SWCommandBundle addAll(SWCommandBundle swCommandBundle) { if (swCommandBundle == null) return this; swCommandList.addAll(swCommandBundle.swCommandList); return this; @@ -51,4 +51,13 @@ public class SWCommandBundle { return Optional.empty(); } + public List tabComplete(String[] args) { + List strings = new ArrayList<>(); + for (SWCommand swCommand : swCommandList) { + Optional> tabCompletes = swCommand.tabComplete(args); + if (tabCompletes.isPresent()) strings.addAll(tabCompletes.get()); + } + return strings; + } + }