Add the full input string to command context

This commit is contained in:
Nathan Adams 2017-11-14 12:04:17 +01:00
parent 45966d1d1e
commit 8bf57b4a9a
9 changed files with 36 additions and 25 deletions

View file

@ -95,7 +95,7 @@ public class CommandDispatcher<S> {
while (!contexts.isEmpty()) {
final CommandContextBuilder<S> builder = contexts.removeFirst();
final CommandContextBuilder<S> child = builder.getChild();
final CommandContext<S> context = builder.build();
final CommandContext<S> context = builder.build(parse.getReader().getString());
if (child != null) {
if (!child.getNodes().isEmpty()) {
final RedirectModifier<S> modifier = Iterators.getLast(builder.getNodes().keySet().iterator()).getRedirectModifier();
@ -128,7 +128,7 @@ public class CommandDispatcher<S> {
}
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<S> {
@SuppressWarnings("unchecked") final CompletableFuture<Collection<String>>[] futures = new CompletableFuture[parent.getChildren().size()];
int i = 0;
for (final CommandNode<S> 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<CommandSuggestions> result = new CompletableFuture<>();

View file

@ -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<S> {
CompletableFuture<Collection<String>> getSuggestions(final CommandContext<S> context, final String prefix);
CompletableFuture<Collection<String>> getSuggestions(final CommandContext<S> context, final String prefix) throws CommandSyntaxException;
}
}

View file

@ -9,14 +9,16 @@ import java.util.Map;
public class CommandContext<S> {
private final S source;
private final String input;
private final Command<S> command;
private final Map<String, ParsedArgument<S, ?>> arguments;
private final Map<CommandNode<S>, StringRange> nodes;
private final StringRange range;
private final CommandContext<S> child;
public CommandContext(final S source, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final Map<CommandNode<S>, StringRange> nodes, final StringRange range, final CommandContext<S> child) {
public CommandContext(final S source, final String input, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final Map<CommandNode<S>, StringRange> nodes, final StringRange range, final CommandContext<S> child) {
this.source = source;
this.input = input;
this.arguments = arguments;
this.command = command;
this.nodes = nodes;
@ -82,6 +84,10 @@ public class CommandContext<S> {
return range;
}
public String getInput() {
return input;
}
public Map<CommandNode<S>, StringRange> getNodes() {
return nodes;
}

View file

@ -78,8 +78,8 @@ public class CommandContextBuilder<S> {
return nodes;
}
public CommandContext<S> build() {
return new CommandContext<>(source, arguments, command, nodes, range, child == null ? null : child.build());
public CommandContext<S> build(final String input) {
return new CommandContext<>(source, input, arguments, command, nodes, range, child == null ? null : child.build(input));
}
public CommandDispatcher<S> getDispatcher() {

View file

@ -59,7 +59,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
}
@Override
public CompletableFuture<Collection<String>> listSuggestions(final CommandContext<S> context, final String command) {
public CompletableFuture<Collection<String>> listSuggestions(final CommandContext<S> context, final String command) throws CommandSyntaxException {
if (customSuggestions == null) {
return type.listSuggestions(context, command);
} else {

View file

@ -104,7 +104,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
public abstract void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException;
public abstract CompletableFuture<Collection<String>> listSuggestions(CommandContext<S> context, String command);
public abstract CompletableFuture<Collection<String>> listSuggestions(CommandContext<S> context, String command) throws CommandSyntaxException;
public abstract ArgumentBuilder<S, ?> createBuilder();

View file

@ -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<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build();
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
context.getArgument("foo", String.class);
}
@Test
public void testGetArgument() throws Exception {
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build();
final CommandContext<Object> 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<Object> node = mock(CommandNode.class);
final CommandNode<Object> 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();
}
}

View file

@ -50,7 +50,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testSuggestions() throws Exception {
final Collection<String> result = node.listSuggestions(contextBuilder.build(), "").join();
final Collection<String> result = node.listSuggestions(contextBuilder.build(""), "").join();
assertThat(result, is(empty()));
}

View file

@ -79,16 +79,16 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testSuggestions() throws Exception {
final Collection<String> empty = node.listSuggestions(contextBuilder.build(), "").join();
final Collection<String> empty = node.listSuggestions(contextBuilder.build(""), "").join();
assertThat(empty, equalTo(Sets.newHashSet("foo")));
final Collection<String> foo = node.listSuggestions(contextBuilder.build(), "foo").join();
final Collection<String> foo = node.listSuggestions(contextBuilder.build("foo"), "foo").join();
assertThat(foo, equalTo(Sets.newHashSet("foo")));
final Collection<String> food = node.listSuggestions(contextBuilder.build(), "food").join();
final Collection<String> food = node.listSuggestions(contextBuilder.build("food"), "food").join();
assertThat(food, is(empty()));
final Collection<String> b = node.listSuggestions(contextBuilder.build(), "b").join();
final Collection<String> b = node.listSuggestions(contextBuilder.build("b"), "b").join();
assertThat(b, is(empty()));
}