From 7d94d8dd6ceff2f4cb5a793c1213f61afd2e3cb7 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 9 Dec 2021 15:09:07 +0100 Subject: [PATCH] Fix CommandPart.generateTabComplete for varArg Parameter --- .../src/de/steamwar/command/CommandPart.java | 14 ++++++ .../command/SimpleCommandPartTest.java | 46 +++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/CommandPart.java b/SpigotCore_Main/src/de/steamwar/command/CommandPart.java index b396f71..b7960ed 100644 --- a/SpigotCore_Main/src/de/steamwar/command/CommandPart.java +++ b/SpigotCore_Main/src/de/steamwar/command/CommandPart.java @@ -133,6 +133,20 @@ public class CommandPart { } public void generateTabComplete(List current, CommandSender commandSender, String[] args, int startIndex) { + if (varArgType != null) { + for (int i = startIndex; i < args.length - 1; i++) { + CheckArgumentResult validArgument = checkArgument(null, commandSender, args, i); + if (!validArgument.success) { + return; + } + } + List strings = typeMapper.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), args[args.length - 1]); + if (strings != null) { + current.addAll(strings); + } + return; + } + if (args.length - 1 > startIndex) { CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, commandSender, args, startIndex); if (checkArgumentResult.success && next != null) { diff --git a/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java b/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java index d734d71..47503d2 100644 --- a/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java +++ b/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java @@ -27,14 +27,14 @@ import java.util.ArrayList; import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; public class SimpleCommandPartTest { private CommandPart stringCommandPart; private CommandPart intCommandPart; private CommandPart chainedCommandPart; + private CommandPart varArgCommandPart; private CommandPart simpleGuardPart; @@ -46,6 +46,8 @@ public class SimpleCommandPartTest { chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND); chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND)); + varArgCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, String.class, null, GuardCheckType.COMMAND); + simpleGuardPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), (commandSender, guardCheckType, previousArguments, s) -> s.equals("hello") ? GuardResult.DENIED : GuardResult.ALLOWED, null, null, GuardCheckType.COMMAND); } @@ -119,10 +121,48 @@ public class SimpleCommandPartTest { public void testChainedCommandTabCompleteOther() { List tabCompletes = new ArrayList<>(); chainedCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0); - System.out.println(tabCompletes); assertThat(tabCompletes.size(), is(0)); } + @Test + public void testVarArgsCommandTabComplete() { + List tabCompletes = new ArrayList<>(); + varArgCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello"}, 0); + assertThat(tabCompletes.size(), is(2)); + assertThat(tabCompletes.get(0), is("hello")); + assertThat(tabCompletes.get(1), is("world")); + } + + @Test + public void testVarArgsCommandTabCompleteDeeper() { + List tabCompletes = new ArrayList<>(); + varArgCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", "world", "hello", "world"}, 0); + System.out.println(tabCompletes); + assertThat(tabCompletes.size(), is(2)); + assertThat(tabCompletes.get(0), is("hello")); + assertThat(tabCompletes.get(1), is("world")); + } + + @Test + public void testVarArgsCommandArgumentParsing() { + List argumentArray = new ArrayList<>(); + varArgCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0); + assertThat(argumentArray.size(), is(1)); + assertThat(argumentArray.get(0), instanceOf(String[].class)); + assertThat((String[]) argumentArray.get(0), arrayWithSize(1)); + assertThat((String[]) argumentArray.get(0), is(new String[]{"hello"})); + } + + @Test + public void testVarArgsCommandArgumentParsingDeeper() { + List argumentArray = new ArrayList<>(); + varArgCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello", "world", "hello", "world"}, 0); + assertThat(argumentArray.size(), is(1)); + assertThat(argumentArray.get(0), instanceOf(String[].class)); + assertThat((String[]) argumentArray.get(0), arrayWithSize(4)); + assertThat((String[]) argumentArray.get(0), is(new String[]{"hello", "world", "hello", "world"})); + } + @Test public void testGuardCommandExecute() { List argumentArray = new ArrayList<>();