From 2114f086cbcce0b044b123e2cab3f580cb318d9e Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Mon, 29 Sep 2014 12:18:40 +0200 Subject: [PATCH] Added a 'source' for commands that may be passed around in the context --- .../minecraft/commands/CommandDispatcher.java | 10 ++++---- .../arguments/IntegerArgumentType.java | 2 +- .../commands/context/CommandContext.java | 14 ++++++++--- .../context/CommandContextBuilder.java | 18 +++++++------ .../commands/CommandDispatcherTest.java | 25 ++++++++++--------- .../arguments/IntegerArgumentTypeTest.java | 2 +- .../commands/context/CommandContextTest.java | 20 +++++++++++---- .../tree/ArgumentCommandNodeTest.java | 4 +-- .../commands/tree/LiteralCommandNodeTest.java | 4 +-- .../commands/tree/RootCommandNodeTest.java | 2 +- 10 files changed, 60 insertions(+), 41 deletions(-) diff --git a/src/main/java/net/minecraft/commands/CommandDispatcher.java b/src/main/java/net/minecraft/commands/CommandDispatcher.java index 54d16e5..812e9ed 100644 --- a/src/main/java/net/minecraft/commands/CommandDispatcher.java +++ b/src/main/java/net/minecraft/commands/CommandDispatcher.java @@ -10,7 +10,7 @@ import net.minecraft.commands.exceptions.UnknownCommandException; import net.minecraft.commands.tree.CommandNode; import net.minecraft.commands.tree.RootCommandNode; -public class CommandDispatcher { +public class CommandDispatcher { public static final String ARGUMENT_SEPARATOR = " "; private final RootCommandNode root = new RootCommandNode(); @@ -19,17 +19,17 @@ public class CommandDispatcher { root.addChild(command.build()); } - public void execute(String command) throws CommandException { - CommandContext context = parseNodes(root, command, new CommandContextBuilder()); + public void execute(String command, T source) throws CommandException { + CommandContext context = parseNodes(root, command, new CommandContextBuilder(source)); context.getCommand().run(context); } - protected CommandContext parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException, UnknownCommandException { + protected CommandContext parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws IllegalArgumentSyntaxException, ArgumentValidationException, UnknownCommandException { IllegalArgumentSyntaxException exception = null; for (CommandNode child : node.getChildren()) { try { - CommandContextBuilder context = contextBuilder.copy(); + CommandContextBuilder context = contextBuilder.copy(); String remaining = child.parse(command, context); if (child.getCommand() != null) { context.withCommand(child.getCommand()); diff --git a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java index 720df78..aad25ca 100644 --- a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java @@ -30,7 +30,7 @@ public class IntegerArgumentType implements CommandArgumentType { return new IntegerArgumentType(min, max); } - public static int getInteger(CommandContext context, String name) { + public static int getInteger(CommandContext context, String name) { return context.getArgument(name, int.class).getResult(); } diff --git a/src/main/java/net/minecraft/commands/context/CommandContext.java b/src/main/java/net/minecraft/commands/context/CommandContext.java index 2796264..abc708d 100644 --- a/src/main/java/net/minecraft/commands/context/CommandContext.java +++ b/src/main/java/net/minecraft/commands/context/CommandContext.java @@ -5,11 +5,13 @@ import net.minecraft.commands.Command; import java.util.Map; -public class CommandContext { +public class CommandContext { + private final T source; private final Map> arguments; private final Command command; - public CommandContext(Map> arguments, Command command) { + public CommandContext(T source, Map> arguments, Command command) { + this.source = source; this.arguments = arguments; this.command = command; } @@ -18,8 +20,12 @@ public class CommandContext { return command; } + public T getSource() { + return source; + } + @SuppressWarnings("unchecked") - public ParsedArgument getArgument(String name, Class clazz) { + public ParsedArgument getArgument(String name, Class clazz) { ParsedArgument argument = arguments.get(name); if (argument == null) { @@ -27,7 +33,7 @@ public class CommandContext { } if (Primitives.wrap(clazz).isAssignableFrom(argument.getResult().getClass())) { - return (ParsedArgument) argument; + return (ParsedArgument) argument; } else { throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz); } diff --git a/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java b/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java index 9649960..3e76600 100644 --- a/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java +++ b/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java @@ -5,14 +5,16 @@ import net.minecraft.commands.Command; import java.util.Map; -public class CommandContextBuilder { +public class CommandContextBuilder { private final Map> arguments = Maps.newHashMap(); + private final T source; private Command command; - public CommandContextBuilder() { + public CommandContextBuilder(T source) { + this.source = source; } - public CommandContextBuilder withArgument(String name, ParsedArgument argument) { + public CommandContextBuilder withArgument(String name, ParsedArgument argument) { this.arguments.put(name, argument); return this; } @@ -21,19 +23,19 @@ public class CommandContextBuilder { return arguments; } - public CommandContextBuilder withCommand(Command command) { + public CommandContextBuilder withCommand(Command command) { this.command = command; return this; } - public CommandContextBuilder copy() { - CommandContextBuilder copy = new CommandContextBuilder(); + public CommandContextBuilder copy() { + CommandContextBuilder copy = new CommandContextBuilder(source); copy.command = this.command; copy.arguments.putAll(this.arguments); return copy; } - public CommandContext build() { - return new CommandContext(arguments, command); + public CommandContext build() { + return new CommandContext(source, arguments, command); } } diff --git a/src/test/java/net/minecraft/commands/CommandDispatcherTest.java b/src/test/java/net/minecraft/commands/CommandDispatcherTest.java index f933b68..9262ed1 100644 --- a/src/test/java/net/minecraft/commands/CommandDispatcherTest.java +++ b/src/test/java/net/minecraft/commands/CommandDispatcherTest.java @@ -19,19 +19,20 @@ import static org.mockito.Mockito.verify; @RunWith(MockitoJUnitRunner.class) public class CommandDispatcherTest { - CommandDispatcher subject; + CommandDispatcher subject; @Mock Command command; + @Mock Object source; @Before public void setUp() throws Exception { - subject = new CommandDispatcher(); + subject = new CommandDispatcher(); } @Test public void testCreateAndExecuteCommand() throws Exception { subject.register(literal("foo").executes(command)); - subject.execute("foo"); + subject.execute("foo", source); verify(command).run(any(CommandContext.class)); } @@ -40,8 +41,8 @@ public class CommandDispatcherTest { subject.register(literal("base").then(literal("foo")).executes(command)); subject.register(literal("base").then(literal("bar")).executes(command)); - subject.execute("base foo"); - subject.execute("base bar"); + subject.execute("base foo", source); + subject.execute("base bar", source); verify(command, times(2)).run(any(CommandContext.class)); } @@ -67,25 +68,25 @@ public class CommandDispatcherTest { ) ); - subject.execute("foo 1 one"); + subject.execute("foo 1 one", source); verify(one).run(any(CommandContext.class)); - subject.execute("foo 2 two"); + subject.execute("foo 2 two", source); verify(two).run(any(CommandContext.class)); - subject.execute("foo 3 three"); + subject.execute("foo 3 three", source); verify(three).run(any(CommandContext.class)); } @Test(expected = UnknownCommandException.class) public void testExecuteUnknownCommand() throws Exception { - subject.execute("foo"); + subject.execute("foo", source); } @Test(expected = UnknownCommandException.class) public void testExecuteUnknownSubcommand() throws Exception { subject.register(literal("foo").executes(command)); - subject.execute("foo bar"); + subject.execute("foo bar", source); } @Test @@ -100,7 +101,7 @@ public class CommandDispatcherTest { literal("c") ).executes(command)); - subject.execute("foo b"); + subject.execute("foo b", source); verify(subCommand).run(any(CommandContext.class)); } @@ -110,6 +111,6 @@ public class CommandDispatcherTest { argument("bar", integer()) ).executes(command)); - subject.execute("foo bar"); + subject.execute("foo bar", source); } } \ No newline at end of file diff --git a/src/test/java/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java b/src/test/java/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java index 8be9b6d..62e4ab0 100644 --- a/src/test/java/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java +++ b/src/test/java/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java @@ -63,7 +63,7 @@ public class IntegerArgumentTypeTest { @Test public void testGetInteger() throws Exception { - CommandContext context = new CommandContextBuilder().withArgument("foo", type.parse("100")).build(); + CommandContext context = new CommandContextBuilder(new Object()).withArgument("foo", type.parse("100")).build(); assertThat(IntegerArgumentType.getInteger(context, "foo"), is(100)); } diff --git a/src/test/java/net/minecraft/commands/context/CommandContextTest.java b/src/test/java/net/minecraft/commands/context/CommandContextTest.java index 6e56839..6f9731d 100644 --- a/src/test/java/net/minecraft/commands/context/CommandContextTest.java +++ b/src/test/java/net/minecraft/commands/context/CommandContextTest.java @@ -3,32 +3,42 @@ package net.minecraft.commands.context; import net.minecraft.commands.arguments.IntegerArgumentType; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +@RunWith(MockitoJUnitRunner.class) public class CommandContextTest { - CommandContext context; + CommandContextBuilder builder; + @Mock Object source; @Before public void setUp() throws Exception { - context = new CommandContextBuilder().build(); + builder = new CommandContextBuilder(source); } @Test(expected = IllegalArgumentException.class) public void testGetArgument_nonexistent() throws Exception { - context.getArgument("foo", Object.class); + builder.build().getArgument("foo", Object.class); } @Test(expected = IllegalArgumentException.class) public void testGetArgument_wrongType() throws Exception { - context = new CommandContextBuilder().withArgument("foo", IntegerArgumentType.integer().parse("123")).build(); + CommandContext context = builder.withArgument("foo", IntegerArgumentType.integer().parse("123")).build(); context.getArgument("foo", String.class); } @Test public void testGetArgument() throws Exception { - context = new CommandContextBuilder().withArgument("foo", IntegerArgumentType.integer().parse("123")).build(); + CommandContext context = builder.withArgument("foo", IntegerArgumentType.integer().parse("123")).build(); assertThat(context.getArgument("foo", int.class).getResult(), is(123)); } + + @Test + public void testSource() throws Exception { + assertThat(builder.build().getSource(), is(source)); + } } \ No newline at end of file diff --git a/src/test/java/net/minecraft/commands/tree/ArgumentCommandNodeTest.java b/src/test/java/net/minecraft/commands/tree/ArgumentCommandNodeTest.java index 3f7d768..66607a5 100644 --- a/src/test/java/net/minecraft/commands/tree/ArgumentCommandNodeTest.java +++ b/src/test/java/net/minecraft/commands/tree/ArgumentCommandNodeTest.java @@ -15,7 +15,7 @@ import static org.mockito.Mockito.mock; public class ArgumentCommandNodeTest extends AbstractCommandNodeTest { ArgumentCommandNode node; - CommandContextBuilder contextBuilder; + CommandContextBuilder contextBuilder; @Override protected CommandNode getCommandNode() { @@ -25,7 +25,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest { @Before public void setUp() throws Exception { node = argument("foo", integer()).build(); - contextBuilder = new CommandContextBuilder(); + contextBuilder = new CommandContextBuilder(new Object()); } @Test diff --git a/src/test/java/net/minecraft/commands/tree/LiteralCommandNodeTest.java b/src/test/java/net/minecraft/commands/tree/LiteralCommandNodeTest.java index 220a12c..ea982e8 100644 --- a/src/test/java/net/minecraft/commands/tree/LiteralCommandNodeTest.java +++ b/src/test/java/net/minecraft/commands/tree/LiteralCommandNodeTest.java @@ -14,7 +14,7 @@ import static org.mockito.Mockito.mock; public class LiteralCommandNodeTest extends AbstractCommandNodeTest { LiteralCommandNode node; - CommandContextBuilder contextBuilder; + CommandContextBuilder contextBuilder; @Override protected CommandNode getCommandNode() { @@ -24,7 +24,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest { @Before public void setUp() throws Exception { node = literal("foo").build(); - contextBuilder = new CommandContextBuilder(); + contextBuilder = new CommandContextBuilder(new Object()); } @Test diff --git a/src/test/java/net/minecraft/commands/tree/RootCommandNodeTest.java b/src/test/java/net/minecraft/commands/tree/RootCommandNodeTest.java index d3af395..713e691 100644 --- a/src/test/java/net/minecraft/commands/tree/RootCommandNodeTest.java +++ b/src/test/java/net/minecraft/commands/tree/RootCommandNodeTest.java @@ -24,7 +24,7 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest { @Test public void testParse() throws Exception { - assertThat(node.parse("foo bar baz", new CommandContextBuilder()), is("foo bar baz")); + assertThat(node.parse("foo bar baz", new CommandContextBuilder(new Object())), is("foo bar baz")); } @Test(expected = UnsupportedOperationException.class)