From 20357723dfa677c1876a6274d3c1051dfbd2bf38 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Wed, 24 Sep 2014 13:49:00 +0200 Subject: [PATCH] Added command context and the ability to query arguments from it --- .../arguments/IntegerArgumentType.java | 5 +++ .../commands/context/CommandContext.java | 29 ++++++++++++++++ .../context/CommandContextBuilder.java | 22 ++++++++++++ .../commands/context/package-info.java | 4 +++ .../arguments/IntegerArgumentTypeTest.java | 9 +++++ .../commands/context/CommandContextTest.java | 34 +++++++++++++++++++ 6 files changed, 103 insertions(+) create mode 100644 src/main/java/net/minecraft/commands/context/CommandContext.java create mode 100644 src/main/java/net/minecraft/commands/context/CommandContextBuilder.java create mode 100644 src/main/java/net/minecraft/commands/context/package-info.java create mode 100644 src/main/test/net/minecraft/commands/context/CommandContextTest.java diff --git a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java index a11f353..76e75b2 100644 --- a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java @@ -1,6 +1,7 @@ package net.minecraft.commands.arguments; import com.google.common.base.Splitter; +import net.minecraft.commands.context.CommandContext; import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; @@ -27,6 +28,10 @@ public class IntegerArgumentType implements CommandArgumentType { return new IntegerArgumentType(min, max); } + public static int getInteger(CommandContext context, String name) { + return context.getArgument(name, int.class).getResult(); + } + @Override public CommandArgumentParseResult parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException { String raw = SPLITTER.split(command).iterator().next(); diff --git a/src/main/java/net/minecraft/commands/context/CommandContext.java b/src/main/java/net/minecraft/commands/context/CommandContext.java new file mode 100644 index 0000000..b09474f --- /dev/null +++ b/src/main/java/net/minecraft/commands/context/CommandContext.java @@ -0,0 +1,29 @@ +package net.minecraft.commands.context; + +import com.google.common.primitives.Primitives; +import net.minecraft.commands.arguments.CommandArgumentType; + +import java.util.Map; + +public class CommandContext { + private final Map> arguments; + + public CommandContext(Map> arguments) { + this.arguments = arguments; + } + + @SuppressWarnings("unchecked") + public CommandArgumentType.CommandArgumentParseResult getArgument(String name, Class clazz) { + CommandArgumentType.CommandArgumentParseResult argument = arguments.get(name); + + if (argument == null) { + throw new IllegalArgumentException("No such argument '" + name + "' exists on this command"); + } + + if (Primitives.wrap(clazz).isAssignableFrom(argument.getResult().getClass())) { + return (CommandArgumentType.CommandArgumentParseResult) 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 new file mode 100644 index 0000000..a614380 --- /dev/null +++ b/src/main/java/net/minecraft/commands/context/CommandContextBuilder.java @@ -0,0 +1,22 @@ +package net.minecraft.commands.context; + +import com.google.common.collect.Maps; +import net.minecraft.commands.arguments.CommandArgumentType; + +import java.util.Map; + +public class CommandContextBuilder { + private final Map> arguments = Maps.newHashMap(); + + public CommandContextBuilder() { + } + + public CommandContextBuilder withArgument(String name, CommandArgumentType.CommandArgumentParseResult argument) { + this.arguments.put(name, argument); + return this; + } + + public CommandContext build() { + return new CommandContext(arguments); + } +} diff --git a/src/main/java/net/minecraft/commands/context/package-info.java b/src/main/java/net/minecraft/commands/context/package-info.java new file mode 100644 index 0000000..5397ef1 --- /dev/null +++ b/src/main/java/net/minecraft/commands/context/package-info.java @@ -0,0 +1,4 @@ +@ParametersAreNonnullByDefault +package net.minecraft.commands.context; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java b/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java index 77914fe..d3f24fc 100644 --- a/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java +++ b/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java @@ -1,5 +1,7 @@ package net.minecraft.commands.arguments; +import net.minecraft.commands.context.CommandContext; +import net.minecraft.commands.context.CommandContextBuilder; import net.minecraft.commands.exceptions.ArgumentValidationException; import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException; import org.junit.Before; @@ -55,4 +57,11 @@ public class IntegerArgumentTypeTest { assertThat(result.getRaw(), is("100")); assertThat(result.getResult(), is(100)); } + + @Test + public void testGetInteger() throws Exception { + CommandContext context = new CommandContextBuilder().withArgument("foo", type.parse("100")).build(); + + assertThat(IntegerArgumentType.getInteger(context, "foo"), is(100)); + } } \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/context/CommandContextTest.java b/src/main/test/net/minecraft/commands/context/CommandContextTest.java new file mode 100644 index 0000000..6e56839 --- /dev/null +++ b/src/main/test/net/minecraft/commands/context/CommandContextTest.java @@ -0,0 +1,34 @@ +package net.minecraft.commands.context; + +import net.minecraft.commands.arguments.IntegerArgumentType; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class CommandContextTest { + CommandContext context; + + @Before + public void setUp() throws Exception { + context = new CommandContextBuilder().build(); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetArgument_nonexistent() throws Exception { + context.getArgument("foo", Object.class); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetArgument_wrongType() throws Exception { + context = new CommandContextBuilder().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(); + assertThat(context.getArgument("foo", int.class).getResult(), is(123)); + } +} \ No newline at end of file