diff --git a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java index 282074c..36f4ea2 100644 --- a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java @@ -1,4 +1,7 @@ package net.minecraft.commands.arguments; -public interface CommandArgumentType { +import net.minecraft.commands.exceptions.IllegalCommandArgumentException; + +public interface CommandArgumentType { + T parse(String command) throws IllegalCommandArgumentException; } diff --git a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java index 72bad36..50674b8 100644 --- a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java @@ -1,6 +1,8 @@ package net.minecraft.commands.arguments; -public class IntegerArgumentType implements CommandArgumentType { +import net.minecraft.commands.exceptions.IllegalCommandArgumentException; + +public class IntegerArgumentType implements CommandArgumentType { private final int minimum; private final int maximum; @@ -20,4 +22,22 @@ public class IntegerArgumentType implements CommandArgumentType { public static IntegerArgumentType integer(int min, int max) { return new IntegerArgumentType(min, max); } + + @Override + public Integer parse(String command) throws IllegalCommandArgumentException { + try { + int value = Integer.parseInt(command); + + if (value < minimum) { + throw new IllegalCommandArgumentException(); + } + if (value > maximum) { + throw new IllegalCommandArgumentException(); + } + + return value; + } catch (NumberFormatException ignored) { + throw new IllegalCommandArgumentException(); + } + } } diff --git a/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java b/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java index 2d99607..6392f46 100644 --- a/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java +++ b/src/main/java/net/minecraft/commands/builder/RequiredArgumentBuilder.java @@ -3,20 +3,20 @@ package net.minecraft.commands.builder; import net.minecraft.commands.arguments.CommandArgumentType; import net.minecraft.commands.tree.ArgumentCommandNode; -public class RequiredArgumentBuilder extends ArgumentBuilder { +public class RequiredArgumentBuilder extends ArgumentBuilder { private final String name; - private final CommandArgumentType type; + private final CommandArgumentType type; - protected RequiredArgumentBuilder(String name, CommandArgumentType type) { + protected RequiredArgumentBuilder(String name, CommandArgumentType type) { this.name = name; this.type = type; } - public static RequiredArgumentBuilder argument(String name, CommandArgumentType type) { - return new RequiredArgumentBuilder(name, type); + public static RequiredArgumentBuilder argument(String name, CommandArgumentType type) { + return new RequiredArgumentBuilder(name, type); } - public CommandArgumentType getType() { + public CommandArgumentType getType() { return type; } @@ -24,8 +24,8 @@ public class RequiredArgumentBuilder extends ArgumentBuilder { return name; } - public ArgumentCommandNode build() { - ArgumentCommandNode result = new ArgumentCommandNode(getName(), getType()); + public ArgumentCommandNode build() { + ArgumentCommandNode result = new ArgumentCommandNode(getName(), getType()); for (ArgumentBuilder argument : getArguments()) { result.addChild(argument.build()); diff --git a/src/main/java/net/minecraft/commands/exceptions/IllegalCommandArgumentException.java b/src/main/java/net/minecraft/commands/exceptions/IllegalCommandArgumentException.java new file mode 100644 index 0000000..5562a10 --- /dev/null +++ b/src/main/java/net/minecraft/commands/exceptions/IllegalCommandArgumentException.java @@ -0,0 +1,4 @@ +package net.minecraft.commands.exceptions; + +public class IllegalCommandArgumentException extends CommandException { +} diff --git a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java index f278cd4..603cef6 100644 --- a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java @@ -1,12 +1,13 @@ package net.minecraft.commands.tree; import net.minecraft.commands.arguments.CommandArgumentType; +import net.minecraft.commands.exceptions.IllegalCommandArgumentException; -public class ArgumentCommandNode extends CommandNode { +public class ArgumentCommandNode extends CommandNode { private final String name; - private final CommandArgumentType type; + private final CommandArgumentType type; - public ArgumentCommandNode(String name, CommandArgumentType type) { + public ArgumentCommandNode(String name, CommandArgumentType type) { this.name = name; this.type = type; } @@ -15,7 +16,12 @@ public class ArgumentCommandNode extends CommandNode { return name; } - public CommandArgumentType getType() { + public CommandArgumentType getType() { return type; } + + @Override + public void parse(String command) throws IllegalCommandArgumentException { + type.parse(command); + } } diff --git a/src/main/java/net/minecraft/commands/tree/CommandNode.java b/src/main/java/net/minecraft/commands/tree/CommandNode.java index fc8dd10..0555975 100644 --- a/src/main/java/net/minecraft/commands/tree/CommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/CommandNode.java @@ -1,6 +1,7 @@ package net.minecraft.commands.tree; import com.google.common.collect.Lists; +import net.minecraft.commands.exceptions.IllegalCommandArgumentException; import java.util.List; @@ -14,4 +15,6 @@ public abstract class CommandNode { public void addChild(CommandNode node) { children.add(node); } + + public abstract void parse(String command) throws IllegalCommandArgumentException; } diff --git a/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java b/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java index 85329a9..1190233 100644 --- a/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java @@ -1,5 +1,7 @@ package net.minecraft.commands.tree; +import net.minecraft.commands.exceptions.IllegalCommandArgumentException; + public class LiteralCommandNode extends CommandNode { private final String literal; private final Runnable executor; @@ -16,4 +18,11 @@ public class LiteralCommandNode extends CommandNode { public Runnable getExecutor() { return executor; } + + @Override + public void parse(String command) throws IllegalCommandArgumentException { + if (!command.equals(literal)) { + throw new IllegalCommandArgumentException(); + } + } } diff --git a/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java b/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java new file mode 100644 index 0000000..e85277c --- /dev/null +++ b/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java @@ -0,0 +1,48 @@ +package net.minecraft.commands.arguments; + +import net.minecraft.commands.exceptions.IllegalCommandArgumentException; +import org.junit.Before; +import org.junit.Test; + +import static net.minecraft.commands.arguments.IntegerArgumentType.integer; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class IntegerArgumentTypeTest { + IntegerArgumentType type; + + @Before + public void setUp() throws Exception { + type = integer(-100, 100); + } + + @Test + public void testParse() throws Exception { + assertThat(type.parse("50"), is(50)); + } + + @Test(expected = IllegalCommandArgumentException.class) + public void testParseInvalid() throws Exception { + type.parse("fifty"); + } + + @Test(expected = IllegalCommandArgumentException.class) + public void testParseTooLow() throws Exception { + type.parse("-101"); + } + + @Test + public void testParseLowerLimit() throws Exception { + assertThat(type.parse("-100"), is(-100)); + } + + @Test(expected = IllegalCommandArgumentException.class) + public void testParseTooHigh() throws Exception { + type.parse("101"); + } + + @Test + public void testParseHigherLimit() throws Exception { + assertThat(type.parse("100"), is(100)); + } +} \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/builder/RequiredArgumentBuilderTest.java b/src/main/test/net/minecraft/commands/builder/RequiredArgumentBuilderTest.java index 56e6a80..5c5e1b6 100644 --- a/src/main/test/net/minecraft/commands/builder/RequiredArgumentBuilderTest.java +++ b/src/main/test/net/minecraft/commands/builder/RequiredArgumentBuilderTest.java @@ -13,8 +13,8 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; public class RequiredArgumentBuilderTest { - @Mock CommandArgumentType type; - RequiredArgumentBuilder builder; + @Mock CommandArgumentType type; + RequiredArgumentBuilder builder; @Before public void setUp() throws Exception { @@ -23,7 +23,7 @@ public class RequiredArgumentBuilderTest { @Test public void testBuild() throws Exception { - ArgumentCommandNode node = builder.build(); + ArgumentCommandNode node = builder.build(); assertThat(node.getName(), is("foo")); assertThat(node.getType(), is(type)); diff --git a/src/main/test/net/minecraft/commands/tree/ArgumentCommandNodeTest.java b/src/main/test/net/minecraft/commands/tree/ArgumentCommandNodeTest.java new file mode 100644 index 0000000..86f09fd --- /dev/null +++ b/src/main/test/net/minecraft/commands/tree/ArgumentCommandNodeTest.java @@ -0,0 +1,27 @@ +package net.minecraft.commands.tree; + +import net.minecraft.commands.exceptions.IllegalCommandArgumentException; +import org.junit.Before; +import org.junit.Test; + +import static net.minecraft.commands.arguments.IntegerArgumentType.integer; +import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; + +public class ArgumentCommandNodeTest { + ArgumentCommandNode node; + + @Before + public void setUp() throws Exception { + node = argument("foo", integer()).build(); + } + + @Test + public void testParse() throws Exception { + node.parse("123"); + } + + @Test(expected = IllegalCommandArgumentException.class) + public void testParseInvalid() throws Exception { + node.parse("bar"); + } +} \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/tree/LiteralCommandNodeTest.java b/src/main/test/net/minecraft/commands/tree/LiteralCommandNodeTest.java new file mode 100644 index 0000000..98324d7 --- /dev/null +++ b/src/main/test/net/minecraft/commands/tree/LiteralCommandNodeTest.java @@ -0,0 +1,26 @@ +package net.minecraft.commands.tree; + +import net.minecraft.commands.exceptions.IllegalCommandArgumentException; +import org.junit.Before; +import org.junit.Test; + +import static net.minecraft.commands.builder.CommandBuilder.command; + +public class LiteralCommandNodeTest { + LiteralCommandNode node; + + @Before + public void setUp() throws Exception { + node = command("foo").build(); + } + + @Test + public void testParse() throws Exception { + node.parse("foo"); + } + + @Test(expected = IllegalCommandArgumentException.class) + public void testParseInvalid() throws Exception { + node.parse("bar"); + } +} \ No newline at end of file