From add406a8aaa79040e863a0c6ea1123b83bc23779 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 13 Mar 2021 16:17:31 +0100 Subject: [PATCH] Add better TabComplete for only Command implementations --- .../de/steamwar/command/InternalCommand.java | 25 +++++++++++++++++++ .../src/de/steamwar/command/SWCommand.java | 7 +++++- .../de/steamwar/command/SWCommandUtils.java | 3 ++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/InternalCommand.java b/SpigotCore_Main/src/de/steamwar/command/InternalCommand.java index 5595992..945c78a 100644 --- a/SpigotCore_Main/src/de/steamwar/command/InternalCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/InternalCommand.java @@ -24,17 +24,22 @@ import org.bukkit.command.CommandSender; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Parameter; +import java.util.Collections; +import java.util.List; class InternalCommand { private SWCommand swCommand; private Method method; + private int increment = 0; private Parameter[] parameters; InternalCommand(SWCommand swCommand, Method method) { this.swCommand = swCommand; this.method = method; parameters = method.getParameters(); + + increment = method.getAnnotation(SWCommand.Register.class).subCommand().length; } boolean invoke(CommandSender commandSender, String[] args) { @@ -51,4 +56,24 @@ class InternalCommand { return true; } + List tabComplete(String[] args) { + if (args.length > parameters.length - increment) { + if (parameters[parameters.length - 1].isVarArgs()) { + Class clazz = parameters[parameters.length - 1].getType().getComponentType(); + try { + return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(clazz, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]); + } catch (Exception e) { + // Ignored + } + } + return Collections.emptyList(); + } + Class clazz = parameters[args.length - increment + 1].getType(); + try { + return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(clazz, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]); + } catch (Exception e) { + return Collections.emptyList(); + } + } + } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 350d574..7f83dc4 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -57,7 +57,6 @@ public abstract class SWCommand { @Override public List tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { - // TODO: add better tab complete, command based List strings = new ArrayList<>(subCommandTabCompletes.getOrDefault(args.length, new HashSet<>())); for (InternalTabComplete internalTabComplete : tabCompleteSet) { SWCommandUtils.TabComplete tabComplete = internalTabComplete.invoke(sender, args); @@ -65,6 +64,12 @@ public abstract class SWCommand { strings.addAll(tabComplete.tabCompletes); } } + for (InternalCommand internalCommand : commandSet) { + List stringList = internalCommand.tabComplete(args); + if (stringList != null) { + strings.addAll(stringList); + } + } strings = new ArrayList<>(strings); for (int i = strings.size() - 1; i >= 0; i--) { if (!strings.get(i).startsWith(args[args.length - 1])) { diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 2ee5b0b..636f08b 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -225,7 +225,8 @@ class SWCommandUtils { } } - public static void addMapper(Class clazz, TypeMapper mapper) { + public static void addMapper(Class clazz, TypeMapper mapper) { + if (clazz.isEnum()) return; if (MAPPER_FUNCTIONS.containsKey(clazz)) return; MAPPER_FUNCTIONS.put(clazz, mapper); }