From 8bf57b4a9a72965490ccfc063bb5ce49c8782218 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Tue, 14 Nov 2017 12:04:17 +0100 Subject: [PATCH] Add the full input string to command context --- .../mojang/brigadier/CommandDispatcher.java | 10 ++++++--- .../mojang/brigadier/CommandSuggestions.java | 3 ++- .../brigadier/context/CommandContext.java | 8 ++++++- .../context/CommandContextBuilder.java | 4 ++-- .../brigadier/tree/ArgumentCommandNode.java | 2 +- .../mojang/brigadier/tree/CommandNode.java | 2 +- .../brigadier/context/CommandContextTest.java | 22 +++++++++---------- .../tree/ArgumentCommandNodeTest.java | 2 +- .../tree/LiteralCommandNodeTest.java | 8 +++---- 9 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 7632448..b94937f 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -95,7 +95,7 @@ public class CommandDispatcher { while (!contexts.isEmpty()) { final CommandContextBuilder builder = contexts.removeFirst(); final CommandContextBuilder child = builder.getChild(); - final CommandContext context = builder.build(); + final CommandContext context = builder.build(parse.getReader().getString()); if (child != null) { if (!child.getNodes().isEmpty()) { final RedirectModifier modifier = Iterators.getLast(builder.getNodes().keySet().iterator()).getRedirectModifier(); @@ -128,7 +128,7 @@ public class CommandDispatcher { } if (!foundCommand) { - consumer.onCommandComplete(parse.getContext().build(), false, 0); + consumer.onCommandComplete(parse.getContext().build(parse.getReader().getString()), false, 0); throw ERROR_UNKNOWN_COMMAND.createWithContext(parse.getReader()); } @@ -334,7 +334,11 @@ public class CommandDispatcher { @SuppressWarnings("unchecked") final CompletableFuture>[] futures = new CompletableFuture[parent.getChildren().size()]; int i = 0; for (final CommandNode node : parent.getChildren()) { - futures[i++] = node.listSuggestions(context.build(), parse.getReader().getString().substring(start)); + try { + futures[i++] = node.listSuggestions(context.build(parse.getReader().getString()), parse.getReader().getString().substring(start)); + } catch (final CommandSyntaxException e) { + futures[i++] = CompletableFuture.completedFuture(Collections.emptyList()); + } } final CompletableFuture result = new CompletableFuture<>(); diff --git a/src/main/java/com/mojang/brigadier/CommandSuggestions.java b/src/main/java/com/mojang/brigadier/CommandSuggestions.java index 25a945b..6bde3ec 100644 --- a/src/main/java/com/mojang/brigadier/CommandSuggestions.java +++ b/src/main/java/com/mojang/brigadier/CommandSuggestions.java @@ -2,6 +2,7 @@ package com.mojang.brigadier; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.StringRange; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import java.util.Collection; import java.util.Collections; @@ -49,6 +50,6 @@ public class CommandSuggestions { @FunctionalInterface public interface Provider { - CompletableFuture> getSuggestions(final CommandContext context, final String prefix); + CompletableFuture> getSuggestions(final CommandContext context, final String prefix) throws CommandSyntaxException; } } diff --git a/src/main/java/com/mojang/brigadier/context/CommandContext.java b/src/main/java/com/mojang/brigadier/context/CommandContext.java index 0dc6505..08883fe 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContext.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContext.java @@ -9,14 +9,16 @@ import java.util.Map; public class CommandContext { private final S source; + private final String input; private final Command command; private final Map> arguments; private final Map, StringRange> nodes; private final StringRange range; private final CommandContext child; - public CommandContext(final S source, final Map> arguments, final Command command, final Map, StringRange> nodes, final StringRange range, final CommandContext child) { + public CommandContext(final S source, final String input, final Map> arguments, final Command command, final Map, StringRange> nodes, final StringRange range, final CommandContext child) { this.source = source; + this.input = input; this.arguments = arguments; this.command = command; this.nodes = nodes; @@ -82,6 +84,10 @@ public class CommandContext { return range; } + public String getInput() { + return input; + } + public Map, StringRange> getNodes() { return nodes; } diff --git a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java index a4bc14d..10ce85b 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java @@ -78,8 +78,8 @@ public class CommandContextBuilder { return nodes; } - public CommandContext build() { - return new CommandContext<>(source, arguments, command, nodes, range, child == null ? null : child.build()); + public CommandContext build(final String input) { + return new CommandContext<>(source, input, arguments, command, nodes, range, child == null ? null : child.build(input)); } public CommandDispatcher getDispatcher() { diff --git a/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java b/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java index 20f8a78..9399346 100644 --- a/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java @@ -59,7 +59,7 @@ public class ArgumentCommandNode extends CommandNode { } @Override - public CompletableFuture> listSuggestions(final CommandContext context, final String command) { + public CompletableFuture> listSuggestions(final CommandContext context, final String command) throws CommandSyntaxException { if (customSuggestions == null) { return type.listSuggestions(context, command); } else { diff --git a/src/main/java/com/mojang/brigadier/tree/CommandNode.java b/src/main/java/com/mojang/brigadier/tree/CommandNode.java index bc7554a..8c0b76e 100644 --- a/src/main/java/com/mojang/brigadier/tree/CommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/CommandNode.java @@ -104,7 +104,7 @@ public abstract class CommandNode implements Comparable> { public abstract void parse(StringReader reader, CommandContextBuilder contextBuilder) throws CommandSyntaxException; - public abstract CompletableFuture> listSuggestions(CommandContext context, String command); + public abstract CompletableFuture> listSuggestions(CommandContext context, String command) throws CommandSyntaxException; public abstract ArgumentBuilder createBuilder(); diff --git a/src/test/java/com/mojang/brigadier/context/CommandContextTest.java b/src/test/java/com/mojang/brigadier/context/CommandContextTest.java index d7b9429..fcde78b 100644 --- a/src/test/java/com/mojang/brigadier/context/CommandContextTest.java +++ b/src/test/java/com/mojang/brigadier/context/CommandContextTest.java @@ -32,24 +32,24 @@ public class CommandContextTest { @Test(expected = IllegalArgumentException.class) public void testGetArgument_nonexistent() throws Exception { - builder.build().getArgument("foo", Object.class); + builder.build("").getArgument("foo", Object.class); } @Test(expected = IllegalArgumentException.class) public void testGetArgument_wrongType() throws Exception { - final CommandContext context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build(); + final CommandContext context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123"); context.getArgument("foo", String.class); } @Test public void testGetArgument() throws Exception { - final CommandContext context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build(); + final CommandContext context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123"); assertThat(context.getArgument("foo", int.class), is(123)); } @Test public void testSource() throws Exception { - assertThat(builder.build().getSource(), is(source)); + assertThat(builder.build("").getSource(), is(source)); } @SuppressWarnings("unchecked") @@ -61,13 +61,13 @@ public class CommandContextTest { final CommandNode node = mock(CommandNode.class); final CommandNode otherNode = mock(CommandNode.class); new EqualsTester() - .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).build(), new CommandContextBuilder<>(dispatcher, source, 0).build()) - .addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource, 0).build(), new CommandContextBuilder<>(dispatcher, otherSource, 0).build()) - .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withCommand(command).build(), new CommandContextBuilder<>(dispatcher, source, 0).withCommand(command).build()) - .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withCommand(otherCommand).build(), new CommandContextBuilder<>(dispatcher, source, 0).withCommand(otherCommand).build()) - .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build(), new CommandContextBuilder<>(dispatcher, source, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build()) - .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withNode(node, new StringRange(0, 3)).withNode(otherNode, new StringRange(4, 6)).build(), new CommandContextBuilder<>(dispatcher, source, 0).withNode(node, new StringRange(0, 3)).withNode(otherNode, new StringRange(4, 6)).build()) - .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withNode(otherNode, new StringRange(0, 3)).withNode(node, new StringRange(4, 6)).build(), new CommandContextBuilder<>(dispatcher, source, 0).withNode(otherNode, new StringRange(0, 3)).withNode(node, new StringRange(4, 6)).build()) + .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).build(""), new CommandContextBuilder<>(dispatcher, source, 0).build("")) + .addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource, 0).build(""), new CommandContextBuilder<>(dispatcher, otherSource, 0).build("")) + .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withCommand(command).build(""), new CommandContextBuilder<>(dispatcher, source, 0).withCommand(command).build("")) + .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withCommand(otherCommand).build(""), new CommandContextBuilder<>(dispatcher, source, 0).withCommand(otherCommand).build("")) + .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123"), new CommandContextBuilder<>(dispatcher, source, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123")) + .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withNode(node, new StringRange(0, 3)).withNode(otherNode, new StringRange(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, 0).withNode(node, new StringRange(0, 3)).withNode(otherNode, new StringRange(4, 6)).build("123 456")) + .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withNode(otherNode, new StringRange(0, 3)).withNode(node, new StringRange(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, 0).withNode(otherNode, new StringRange(0, 3)).withNode(node, new StringRange(4, 6)).build("123 456")) .testEquals(); } } \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java index 1b38852..92ecf58 100644 --- a/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java @@ -50,7 +50,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest { @Test public void testSuggestions() throws Exception { - final Collection result = node.listSuggestions(contextBuilder.build(), "").join(); + final Collection result = node.listSuggestions(contextBuilder.build(""), "").join(); assertThat(result, is(empty())); } diff --git a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java index d130147..fe12b00 100644 --- a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java @@ -79,16 +79,16 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest { @Test public void testSuggestions() throws Exception { - final Collection empty = node.listSuggestions(contextBuilder.build(), "").join(); + final Collection empty = node.listSuggestions(contextBuilder.build(""), "").join(); assertThat(empty, equalTo(Sets.newHashSet("foo"))); - final Collection foo = node.listSuggestions(contextBuilder.build(), "foo").join(); + final Collection foo = node.listSuggestions(contextBuilder.build("foo"), "foo").join(); assertThat(foo, equalTo(Sets.newHashSet("foo"))); - final Collection food = node.listSuggestions(contextBuilder.build(), "food").join(); + final Collection food = node.listSuggestions(contextBuilder.build("food"), "food").join(); assertThat(food, is(empty())); - final Collection b = node.listSuggestions(contextBuilder.build(), "b").join(); + final Collection b = node.listSuggestions(contextBuilder.build("b"), "b").join(); assertThat(b, is(empty())); }