Made every CommandNode support an executor

This commit is contained in:
Nathan Adams 2014-09-24 12:04:33 +02:00
parent 3ef22ef317
commit d13587a4df
6 changed files with 34 additions and 8 deletions

View file

@ -30,7 +30,7 @@ public class RequiredArgumentBuilder<T> extends ArgumentBuilder<RequiredArgument
} }
public ArgumentCommandNode<T> build() { public ArgumentCommandNode<T> build() {
ArgumentCommandNode<T> result = new ArgumentCommandNode<T>(getName(), getType()); ArgumentCommandNode<T> result = new ArgumentCommandNode<T>(getName(), getType(), getExecutor());
for (ArgumentBuilder argument : getArguments()) { for (ArgumentBuilder argument : getArguments()) {
result.addChild(argument.build()); result.addChild(argument.build());

View file

@ -8,7 +8,8 @@ public class ArgumentCommandNode<T> extends CommandNode {
private final String name; private final String name;
private final CommandArgumentType<T> type; private final CommandArgumentType<T> type;
public ArgumentCommandNode(String name, CommandArgumentType<T> type) { public ArgumentCommandNode(String name, CommandArgumentType<T> type, Runnable executor) {
super(executor);
this.name = name; this.name = name;
this.type = type; this.type = type;
} }

View file

@ -7,8 +7,17 @@ import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
import java.util.List; import java.util.List;
public abstract class CommandNode { public abstract class CommandNode {
private final Runnable executor;
private final List<CommandNode> children = Lists.newArrayList(); private final List<CommandNode> children = Lists.newArrayList();
protected CommandNode(Runnable executor) {
this.executor = executor;
}
public Runnable getExecutor() {
return executor;
}
public List<CommandNode> getChildren() { public List<CommandNode> getChildren() {
return children; return children;
} }

View file

@ -5,21 +5,16 @@ import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
public class LiteralCommandNode extends CommandNode { public class LiteralCommandNode extends CommandNode {
private final String literal; private final String literal;
private final Runnable executor;
public LiteralCommandNode(String literal, Runnable executor) { public LiteralCommandNode(String literal, Runnable executor) {
super(executor);
this.literal = literal; this.literal = literal;
this.executor = executor;
} }
public String getLiteral() { public String getLiteral() {
return literal; return literal;
} }
public Runnable getExecutor() {
return executor;
}
@Override @Override
public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException { public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
if (command.startsWith(literal)) { if (command.startsWith(literal)) {

View file

@ -3,6 +3,7 @@ package net.minecraft.commands.builder;
import net.minecraft.commands.tree.LiteralCommandNode; import net.minecraft.commands.tree.LiteralCommandNode;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock;
import static net.minecraft.commands.arguments.IntegerArgumentType.integer; import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
@ -12,6 +13,8 @@ import static org.junit.Assert.assertThat;
public class LiteralArgumentBuilderTest { public class LiteralArgumentBuilderTest {
LiteralArgumentBuilder builder; LiteralArgumentBuilder builder;
@Mock
Runnable executor;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -25,6 +28,14 @@ public class LiteralArgumentBuilderTest {
assertThat(node.getLiteral(), is("foo")); assertThat(node.getLiteral(), is("foo"));
} }
@Test
public void testBuildWithExecutor() throws Exception {
LiteralCommandNode node = builder.executes(executor).build();
assertThat(node.getLiteral(), is("foo"));
assertThat(node.getExecutor(), is(executor));
}
@Test @Test
public void testBuildWithChildren() throws Exception { public void testBuildWithChildren() throws Exception {
builder.then(argument("bar", integer())); builder.then(argument("bar", integer()));

View file

@ -15,6 +15,7 @@ import static org.junit.Assert.assertThat;
public class RequiredArgumentBuilderTest { public class RequiredArgumentBuilderTest {
@Mock CommandArgumentType<Integer> type; @Mock CommandArgumentType<Integer> type;
RequiredArgumentBuilder<Integer> builder; RequiredArgumentBuilder<Integer> builder;
@Mock Runnable executor;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -29,6 +30,15 @@ public class RequiredArgumentBuilderTest {
assertThat(node.getType(), is(type)); assertThat(node.getType(), is(type));
} }
@Test
public void testBuildWithExecutor() throws Exception {
ArgumentCommandNode<Integer> node = builder.executes(executor).build();
assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type));
assertThat(node.getExecutor(), is(executor));
}
@Test @Test
public void testBuildWithChildren() throws Exception { public void testBuildWithChildren() throws Exception {
builder.then(argument("bar", integer())); builder.then(argument("bar", integer()));