Changed the Runnable to a Command

This commit is contained in:
Nathan Adams
2014-09-24 14:05:12 +02:00
parent 81ed5f0521
commit 8da6087618
11 changed files with 47 additions and 28 deletions
@@ -0,0 +1,7 @@
package net.minecraft.commands;
import net.minecraft.commands.context.CommandContext;
public interface Command {
void run(CommandContext context);
}
@@ -2,10 +2,13 @@ package net.minecraft.commands;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.minecraft.commands.builder.LiteralArgumentBuilder; import net.minecraft.commands.builder.LiteralArgumentBuilder;
import net.minecraft.commands.context.CommandContext;
import net.minecraft.commands.context.ParsedArgument;
import net.minecraft.commands.exceptions.CommandException; import net.minecraft.commands.exceptions.CommandException;
import net.minecraft.commands.exceptions.UnknownCommandException; import net.minecraft.commands.exceptions.UnknownCommandException;
import net.minecraft.commands.tree.LiteralCommandNode; import net.minecraft.commands.tree.LiteralCommandNode;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class CommandDispatcher { public class CommandDispatcher {
@@ -24,6 +27,6 @@ public class CommandDispatcher {
throw new UnknownCommandException(); throw new UnknownCommandException();
} }
node.getExecutor().run(); node.getCommand().run(new CommandContext(new HashMap<String, ParsedArgument<?>>()));
} }
} }
@@ -1,13 +1,14 @@
package net.minecraft.commands.builder; package net.minecraft.commands.builder;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.minecraft.commands.Command;
import net.minecraft.commands.tree.CommandNode; import net.minecraft.commands.tree.CommandNode;
import java.util.List; import java.util.List;
public abstract class ArgumentBuilder<T extends ArgumentBuilder<?>> { public abstract class ArgumentBuilder<T extends ArgumentBuilder<?>> {
private final List<ArgumentBuilder> arguments = Lists.newArrayList(); private final List<ArgumentBuilder> arguments = Lists.newArrayList();
private Runnable executor; private Command command;
protected abstract T getThis(); protected abstract T getThis();
@@ -20,13 +21,13 @@ public abstract class ArgumentBuilder<T extends ArgumentBuilder<?>> {
return arguments; return arguments;
} }
public T executes(Runnable executor) { public T executes(Command command) {
this.executor = executor; this.command = command;
return getThis(); return getThis();
} }
public Runnable getExecutor() { public Command getCommand() {
return executor; return command;
} }
public abstract CommandNode build(); public abstract CommandNode build();
@@ -24,7 +24,7 @@ public class LiteralArgumentBuilder extends ArgumentBuilder<LiteralArgumentBuild
@Override @Override
public LiteralCommandNode build() { public LiteralCommandNode build() {
LiteralCommandNode result = new LiteralCommandNode(getLiteral(), getExecutor()); LiteralCommandNode result = new LiteralCommandNode(getLiteral(), getCommand());
for (ArgumentBuilder argument : getArguments()) { for (ArgumentBuilder argument : getArguments()) {
result.addChild(argument.build()); result.addChild(argument.build());
@@ -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(), getExecutor()); ArgumentCommandNode<T> result = new ArgumentCommandNode<T>(getName(), getType(), getCommand());
for (ArgumentBuilder argument : getArguments()) { for (ArgumentBuilder argument : getArguments()) {
result.addChild(argument.build()); result.addChild(argument.build());
@@ -1,5 +1,6 @@
package net.minecraft.commands.tree; package net.minecraft.commands.tree;
import net.minecraft.commands.Command;
import net.minecraft.commands.arguments.CommandArgumentType; import net.minecraft.commands.arguments.CommandArgumentType;
import net.minecraft.commands.context.ParsedArgument; import net.minecraft.commands.context.ParsedArgument;
import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.ArgumentValidationException;
@@ -9,8 +10,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, Runnable executor) { public ArgumentCommandNode(String name, CommandArgumentType<T> type, Command command) {
super(executor); super(command);
this.name = name; this.name = name;
this.type = type; this.type = type;
} }
@@ -1,21 +1,22 @@
package net.minecraft.commands.tree; package net.minecraft.commands.tree;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.minecraft.commands.Command;
import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; 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 Command command;
private final List<CommandNode> children = Lists.newArrayList(); private final List<CommandNode> children = Lists.newArrayList();
protected CommandNode(Runnable executor) { protected CommandNode(Command command) {
this.executor = executor; this.command = command;
} }
public Runnable getExecutor() { public Command getCommand() {
return executor; return command;
} }
public List<CommandNode> getChildren() { public List<CommandNode> getChildren() {
@@ -1,13 +1,14 @@
package net.minecraft.commands.tree; package net.minecraft.commands.tree;
import net.minecraft.commands.Command;
import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
public class LiteralCommandNode extends CommandNode { public class LiteralCommandNode extends CommandNode {
private final String literal; private final String literal;
public LiteralCommandNode(String literal, Runnable executor) { public LiteralCommandNode(String literal, Command command) {
super(executor); super(command);
this.literal = literal; this.literal = literal;
} }
@@ -1,5 +1,6 @@
package net.minecraft.commands; package net.minecraft.commands;
import net.minecraft.commands.context.CommandContext;
import net.minecraft.commands.exceptions.UnknownCommandException; import net.minecraft.commands.exceptions.UnknownCommandException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -8,12 +9,13 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal; import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherTest { public class CommandDispatcherTest {
CommandDispatcher subject; CommandDispatcher subject;
@Mock Runnable runnable; @Mock Command command;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@@ -22,16 +24,16 @@ public class CommandDispatcherTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testDuplicateCommand() throws Exception { public void testDuplicateCommand() throws Exception {
subject.register(literal("foo").executes(runnable)); subject.register(literal("foo").executes(command));
subject.register(literal("foo").executes(runnable)); subject.register(literal("foo").executes(command));
} }
@Test @Test
public void testCreateAndExecuteCommand() throws Exception { public void testCreateAndExecuteCommand() throws Exception {
subject.register(literal("foo").executes(runnable)); subject.register(literal("foo").executes(command));
subject.execute("foo"); subject.execute("foo");
verify(runnable).run(); verify(command).run(any(CommandContext.class));
} }
@Test(expected = UnknownCommandException.class) @Test(expected = UnknownCommandException.class)
@@ -1,5 +1,6 @@
package net.minecraft.commands.builder; package net.minecraft.commands.builder;
import net.minecraft.commands.Command;
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;
@@ -14,7 +15,7 @@ import static org.junit.Assert.assertThat;
public class LiteralArgumentBuilderTest { public class LiteralArgumentBuilderTest {
LiteralArgumentBuilder builder; LiteralArgumentBuilder builder;
@Mock @Mock
Runnable executor; Command command;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@@ -30,10 +31,10 @@ public class LiteralArgumentBuilderTest {
@Test @Test
public void testBuildWithExecutor() throws Exception { public void testBuildWithExecutor() throws Exception {
LiteralCommandNode node = builder.executes(executor).build(); LiteralCommandNode node = builder.executes(command).build();
assertThat(node.getLiteral(), is("foo")); assertThat(node.getLiteral(), is("foo"));
assertThat(node.getExecutor(), is(executor)); assertThat(node.getCommand(), is(command));
} }
@Test @Test
@@ -1,5 +1,6 @@
package net.minecraft.commands.builder; package net.minecraft.commands.builder;
import net.minecraft.commands.Command;
import net.minecraft.commands.arguments.CommandArgumentType; import net.minecraft.commands.arguments.CommandArgumentType;
import net.minecraft.commands.tree.ArgumentCommandNode; import net.minecraft.commands.tree.ArgumentCommandNode;
import org.junit.Before; import org.junit.Before;
@@ -15,7 +16,8 @@ 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; @Mock
Command command;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@@ -32,11 +34,11 @@ public class RequiredArgumentBuilderTest {
@Test @Test
public void testBuildWithExecutor() throws Exception { public void testBuildWithExecutor() throws Exception {
ArgumentCommandNode<Integer> node = builder.executes(executor).build(); ArgumentCommandNode<Integer> node = builder.executes(command).build();
assertThat(node.getName(), is("foo")); assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type)); assertThat(node.getType(), is(type));
assertThat(node.getExecutor(), is(executor)); assertThat(node.getCommand(), is(command));
} }
@Test @Test