Commands should return an int success count
This commit is contained in:
@@ -2,6 +2,9 @@ package com.mojang.brigadier;
|
|||||||
|
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
public interface Command<S> {
|
public interface Command<S> {
|
||||||
void run(CommandContext<S> context);
|
int SINGLE_SUCCESS = 1;
|
||||||
|
|
||||||
|
int run(CommandContext<S> context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,9 +39,9 @@ public class CommandDispatcher<S> {
|
|||||||
root.addChild(command.build());
|
root.addChild(command.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(String command, S source) throws CommandException {
|
public int execute(String command, S source) throws CommandException {
|
||||||
CommandContext<S> context = parseNodes(root, command, new CommandContextBuilder<>(source));
|
CommandContext<S> context = parseNodes(root, command, new CommandContextBuilder<>(source));
|
||||||
context.getCommand().run(context);
|
return context.getCommand().run(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CommandContext<S> parseNodes(CommandNode<S> node, String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
private CommandContext<S> parseNodes(CommandNode<S> node, String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import com.mojang.brigadier.tree.RootCommandNode;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, ?>> {
|
public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
|
||||||
private final RootCommandNode<S> arguments = new RootCommandNode<>();
|
private final RootCommandNode<S> arguments = new RootCommandNode<>();
|
||||||
private Command<S> command;
|
private Command<S> command;
|
||||||
private Predicate<S> requirement = s -> true;
|
private Predicate<S> requirement = s -> true;
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public class CommandDispatcherTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
subject = new CommandDispatcher<>();
|
subject = new CommandDispatcher<>();
|
||||||
|
when(command.run(any())).thenReturn(42);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -40,7 +41,7 @@ public class CommandDispatcherTest {
|
|||||||
public void testCreateAndExecuteCommand() throws Exception {
|
public void testCreateAndExecuteCommand() throws Exception {
|
||||||
subject.register(literal("foo").executes(command));
|
subject.register(literal("foo").executes(command));
|
||||||
|
|
||||||
subject.execute("foo", source);
|
assertThat(subject.execute("foo", source), is(42));
|
||||||
verify(command).run(any(CommandContext.class));
|
verify(command).run(any(CommandContext.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,8 +51,8 @@ public class CommandDispatcherTest {
|
|||||||
subject.register(literal("base").then(literal("foo")).executes(command));
|
subject.register(literal("base").then(literal("foo")).executes(command));
|
||||||
subject.register(literal("base").then(literal("bar")).executes(command));
|
subject.register(literal("base").then(literal("bar")).executes(command));
|
||||||
|
|
||||||
subject.execute("base foo", source);
|
assertThat(subject.execute("base foo", source), is(42));
|
||||||
subject.execute("base bar", source);
|
assertThat(subject.execute("base bar", source), is(42));
|
||||||
verify(command, times(2)).run(any(CommandContext.class));
|
verify(command, times(2)).run(any(CommandContext.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +63,10 @@ public class CommandDispatcherTest {
|
|||||||
Command<Object> two = mock(Command.class);
|
Command<Object> two = mock(Command.class);
|
||||||
Command<Object> three = mock(Command.class);
|
Command<Object> three = mock(Command.class);
|
||||||
|
|
||||||
|
when(one.run(any())).thenReturn(111);
|
||||||
|
when(two.run(any())).thenReturn(222);
|
||||||
|
when(three.run(any())).thenReturn(333);
|
||||||
|
|
||||||
subject.register(
|
subject.register(
|
||||||
literal("foo").then(
|
literal("foo").then(
|
||||||
argument("one", integer()).then(
|
argument("one", integer()).then(
|
||||||
@@ -78,13 +83,13 @@ public class CommandDispatcherTest {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
subject.execute("foo 1 one", source);
|
assertThat(subject.execute("foo 1 one", source), is(111));
|
||||||
verify(one).run(any(CommandContext.class));
|
verify(one).run(any(CommandContext.class));
|
||||||
|
|
||||||
subject.execute("foo 2 two", source);
|
assertThat(subject.execute("foo 2 two", source), is(222));
|
||||||
verify(two).run(any(CommandContext.class));
|
verify(two).run(any(CommandContext.class));
|
||||||
|
|
||||||
subject.execute("foo 3 three", source);
|
assertThat(subject.execute("foo 3 three", source), is(333));
|
||||||
verify(three).run(any(CommandContext.class));
|
verify(three).run(any(CommandContext.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,6 +134,7 @@ public class CommandDispatcherTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testExecuteSubcommand() throws Exception {
|
public void testExecuteSubcommand() throws Exception {
|
||||||
Command<Object> subCommand = mock(Command.class);
|
Command<Object> subCommand = mock(Command.class);
|
||||||
|
when(subCommand.run(any())).thenReturn(100);
|
||||||
|
|
||||||
subject.register(literal("foo").then(
|
subject.register(literal("foo").then(
|
||||||
literal("a")
|
literal("a")
|
||||||
@@ -138,7 +144,7 @@ public class CommandDispatcherTest {
|
|||||||
literal("c")
|
literal("c")
|
||||||
).executes(command));
|
).executes(command));
|
||||||
|
|
||||||
subject.execute("foo b", source);
|
assertThat(subject.execute("foo b", source), is(100));
|
||||||
verify(subCommand).run(any(CommandContext.class));
|
verify(subCommand).run(any(CommandContext.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user