From 7e733fca7cb2ac3c74c589933ed328ec6c7039d4 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 29 Mar 2021 22:14:52 +0200 Subject: [PATCH] Make HelpMessage more advanced --- .../src/de/steamwar/acommand/TestCommand.java | 3 ++- .../src/de/steamwar/command/SWCommand.java | 6 ++++++ .../src/de/steamwar/command/SubCommand.java | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java b/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java index 90800e1..7b77f97 100644 --- a/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java +++ b/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java @@ -36,8 +36,9 @@ public class TestCommand extends SWCommand { } // One Help Command, the first Parameter should be some kind of CommandSender + // The second argument can only be a varAgrs string of what arguments was tried to map @RegisterHelp - public void testHelp(Player player) { + public void testHelp(Player player, String... args) { player.sendMessage("This is your help message"); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 5623169..ed6c7c1 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -90,6 +90,12 @@ public abstract class SWCommand { } }); add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> { + if (!parameters[parameters.length - 1].isVarArgs()) { + return; + } + if (parameters[parameters.length - 1].getType().getComponentType() != String.class) { + return; + } commandHelpSet.add(new SubCommand(this, method, anno.value())); }); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index b807c03..ce98811 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -24,10 +24,7 @@ import org.bukkit.command.CommandSender; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Parameter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.function.Function; class SubCommand { @@ -63,7 +60,7 @@ class SubCommand { for (Enum enumConstant : enumClass.getEnumConstants()) { tabCompletes.add(enumConstant.name().toLowerCase()); } - arguments[i] = SWCommandUtils.createMapper(s -> SWCommandUtils.ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes); + arguments[i - 1] = SWCommandUtils.createMapper(s -> SWCommandUtils.ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes); continue; } @@ -72,7 +69,7 @@ class SubCommand { name = mapper.value(); } - arguments[i] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION); + arguments[i - 1] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION); } } @@ -80,6 +77,9 @@ class SubCommand { if (args.length < arguments.length - 1) { return false; } + if (!varArgs && args.length > arguments.length) { + return false; + } try { Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgs, subCommand); objects[0] = commandSenderFunction.apply(commandSender); @@ -97,7 +97,7 @@ class SubCommand { if (!varArgs && args.length < arguments.length - 1) { return Collections.emptyList(); } - List argsList = Arrays.asList(args); + List argsList = new LinkedList<>(Arrays.asList(args)); for (String value : subCommand) { String s = argsList.remove(0); if (argsList.isEmpty()) return Collections.singletonList(value); @@ -107,7 +107,9 @@ class SubCommand { String s = argsList.remove(0); if (argsList.isEmpty()) return argument.tabCompletes(s); try { - argument.map(s); + if (argument.map(s) == null) { + return Collections.emptyList(); + } } catch (Exception e) { return Collections.emptyList(); }