Command suggestion improvements

This commit is contained in:
Alexander Söderberg 2020-09-17 09:50:36 +02:00
parent bc261676e7
commit 14b5d9fc3c
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
4 changed files with 14 additions and 6 deletions

View file

@ -280,7 +280,8 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
final Node<CommandComponent<C, ?>> child = children.get(0); final Node<CommandComponent<C, ?>> child = children.get(0);
if (child.getValue() != null) { if (child.getValue() != null) {
if (commandQueue.isEmpty()) { if (commandQueue.isEmpty()) {
return child.getValue().getParser().suggestions(commandContext, ""); return Collections.emptyList();
// return child.getValue().getParser().suggestions(commandContext, "");
} else if (child.isLeaf() && commandQueue.size() < 2) { } else if (child.isLeaf() && commandQueue.size() < 2) {
return child.getValue().getParser().suggestions(commandContext, commandQueue.peek()); return child.getValue().getParser().suggestions(commandContext, commandQueue.peek());
} else if (child.isLeaf()) { } else if (child.isLeaf()) {
@ -296,7 +297,7 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
} }
} }
/* There are 0 or more static components as children. No variable child components are present */ /* There are 0 or more static components as children. No variable child components are present */
if (children.isEmpty()) { if (children.isEmpty() || commandQueue.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} else { } else {
final Iterator<Node<CommandComponent<C, ?>>> childIterator = root.getChildren().iterator(); final Iterator<Node<CommandComponent<C, ?>>> childIterator = root.getChildren().iterator();

View file

@ -58,20 +58,26 @@ public class CommandSuggestionsTest {
void testSimple() { void testSimple() {
final String input = "test"; final String input = "test";
final List<String> suggestions = manager.suggest(new TestCommandSender(), input); final List<String> suggestions = manager.suggest(new TestCommandSender(), input);
Assertions.assertEquals(Arrays.asList("one", "two", "var"), suggestions); Assertions.assertTrue(suggestions.isEmpty());
final String input2 = "test ";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertEquals(Arrays.asList("one", "two","var"), suggestions2);
} }
@Test @Test
void testVar() { void testVar() {
final String input = "test var"; final String input = "test var";
final List<String> suggestions = manager.suggest(new TestCommandSender(), input); final List<String> suggestions = manager.suggest(new TestCommandSender(), input);
Assertions.assertEquals(Arrays.asList("one", "two"), suggestions); Assertions.assertTrue(suggestions.isEmpty());
final String input2 = "test var one"; final String input2 = "test var one";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2); final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertEquals(Arrays.asList("foo", "bar"), suggestions2); Assertions.assertTrue(suggestions2.isEmpty());
final String input3 = "test var one f"; final String input3 = "test var one f";
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3); final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
Assertions.assertEquals(Collections.singletonList("foo"), suggestions3); Assertions.assertEquals(Collections.singletonList("foo"), suggestions3);
final String input4 = "test var one ";
final List<String> suggestions4 = manager.suggest(new TestCommandSender(), input4);
Assertions.assertEquals(Arrays.asList("foo", "bar"), suggestions4);
} }
@Test @Test

View file

@ -93,7 +93,7 @@ class CommandTreeTest {
void getSuggestions() { void getSuggestions() {
Assertions.assertFalse( Assertions.assertFalse(
commandManager.getCommandTree().getSuggestions(new CommandContext<>(new TestCommandSender()), new LinkedList<>( commandManager.getCommandTree().getSuggestions(new CommandContext<>(new TestCommandSender()), new LinkedList<>(
Collections.singletonList("test"))).isEmpty()); Collections.singletonList("test "))).isEmpty());
} }
@Test @Test

View file

@ -146,6 +146,7 @@ public final class BukkitTest extends JavaPlugin {
.component(IntegerComponent.required("int")) .component(IntegerComponent.required("int"))
.component(BooleanComponent.required("bool")) .component(BooleanComponent.required("bool"))
.component(StringComponent.required("string")) .component(StringComponent.required("string"))
.handler(c -> c.getSender().sendMessage("Executed the command"))
.build()); .build());
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();