From 811bc921a4c6057d411407e8161dec1dcdcbea44 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 12 Mar 2021 18:12:07 +0100 Subject: [PATCH] Add SWCommand.subCommandTabCompletes --- .../src/de/steamwar/command/SWCommand.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 52b8da1..dbd5a66 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -35,6 +35,7 @@ public abstract class SWCommand { private final Set commandSet = new HashSet<>(); private final Set tabCompleteSet = new HashSet<>(); + private final Map> subCommandTabCompletes = new HashMap<>(); private Consumer helpMessage = sender -> {}; protected SWCommand(String command) { @@ -55,19 +56,30 @@ public abstract class SWCommand { @Override public List tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { - List strings = new ArrayList<>(); + List strings = new ArrayList<>(subCommandTabCompletes.getOrDefault(args.length, new HashSet<>())); for (InternalTabComplete internalTabComplete : tabCompleteSet) { SWCommandUtils.TabComplete tabComplete = internalTabComplete.invoke(sender, args); if (tabComplete != null) { strings.addAll(tabComplete.tabCompletes); } } + strings = new ArrayList<>(strings); + for (int i = strings.size() - 1; i >= 0; i--) { + if (!strings.get(i).startsWith(args[args.length - 1])) { + strings.remove(i); + } + } return strings; } }); for (Method method : getClass().getDeclaredMethods()) { - if (method.getDeclaredAnnotation(Register.class) == null) continue; + Register register = method.getDeclaredAnnotation(Register.class); + if (register == null) continue; + for (int i = 0; i < register.subCommand().length; i++) { + subCommandTabCompletes.computeIfAbsent(i, integer -> new HashSet<>()).add(register.subCommand()[i]); + } + if (!validMethod(method)) continue; if (method.getReturnType() == Void.TYPE) { commandSet.add(new InternalCommand(this, method));