diff --git a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java index 36f4ea2..7ba56c3 100644 --- a/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/CommandArgumentType.java @@ -3,5 +3,23 @@ package net.minecraft.commands.arguments; import net.minecraft.commands.exceptions.IllegalCommandArgumentException; public interface CommandArgumentType { - T parse(String command) throws IllegalCommandArgumentException; + CommandArgumentParseResult parse(String command) throws IllegalCommandArgumentException; + + class CommandArgumentParseResult { + private final String raw; + private final T result; + + public CommandArgumentParseResult(String raw, T result) { + this.raw = raw; + this.result = result; + } + + public String getRaw() { + return raw; + } + + public T getResult() { + return result; + } + } } diff --git a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java index 50674b8..8f4164e 100644 --- a/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java +++ b/src/main/java/net/minecraft/commands/arguments/IntegerArgumentType.java @@ -1,8 +1,11 @@ package net.minecraft.commands.arguments; +import com.google.common.base.Splitter; import net.minecraft.commands.exceptions.IllegalCommandArgumentException; public class IntegerArgumentType implements CommandArgumentType { + private static final Splitter SPLITTER = Splitter.on(' ').limit(2); + private final int minimum; private final int maximum; @@ -24,9 +27,11 @@ public class IntegerArgumentType implements CommandArgumentType { } @Override - public Integer parse(String command) throws IllegalCommandArgumentException { + public CommandArgumentParseResult parse(String command) throws IllegalCommandArgumentException { + String raw = SPLITTER.split(command).iterator().next(); + try { - int value = Integer.parseInt(command); + int value = Integer.parseInt(raw); if (value < minimum) { throw new IllegalCommandArgumentException(); @@ -35,7 +40,7 @@ public class IntegerArgumentType implements CommandArgumentType { throw new IllegalCommandArgumentException(); } - return value; + return new CommandArgumentParseResult(raw, value); } catch (NumberFormatException ignored) { throw new IllegalCommandArgumentException(); } diff --git a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java index 603cef6..e054ffa 100644 --- a/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/ArgumentCommandNode.java @@ -22,6 +22,23 @@ public class ArgumentCommandNode extends CommandNode { @Override public void parse(String command) throws IllegalCommandArgumentException { - type.parse(command); + CommandArgumentType.CommandArgumentParseResult parsed = type.parse(command); + int start = parsed.getRaw().length() + 1; + + if (start < command.length()) { + String result = command.substring(start); + IllegalCommandArgumentException exception = new IllegalCommandArgumentException(); + + for (CommandNode node : getChildren()) { + try { + node.parse(result); + return; + } catch (IllegalCommandArgumentException ex) { + exception = ex; + } + } + + throw exception; + } } } diff --git a/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java b/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java index 1190233..b1bb87a 100644 --- a/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java +++ b/src/main/java/net/minecraft/commands/tree/LiteralCommandNode.java @@ -21,7 +21,20 @@ public class LiteralCommandNode extends CommandNode { @Override public void parse(String command) throws IllegalCommandArgumentException { - if (!command.equals(literal)) { + if (command.startsWith(literal)) { + int start = literal.length() + 1; + + if (start < command.length()) { + String result = command.substring(start); + + for (CommandNode node : getChildren()) { + node.parse(result); + return; + } + + throw new IllegalCommandArgumentException(); + } + } else { throw new IllegalCommandArgumentException(); } } diff --git a/src/main/test/net/minecraft/commands/CommandNodeTest.java b/src/main/test/net/minecraft/commands/CommandNodeTest.java deleted file mode 100644 index 20cfaa8..0000000 --- a/src/main/test/net/minecraft/commands/CommandNodeTest.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.minecraft.commands; - -import net.minecraft.commands.tree.CommandNode; - -public class CommandNodeTest { - CommandNode node; - -} \ 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 e85277c..befc371 100644 --- a/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java +++ b/src/main/test/net/minecraft/commands/arguments/IntegerArgumentTypeTest.java @@ -18,7 +18,10 @@ public class IntegerArgumentTypeTest { @Test public void testParse() throws Exception { - assertThat(type.parse("50"), is(50)); + CommandArgumentType.CommandArgumentParseResult result = type.parse("50"); + + assertThat(result.getRaw(), is("50")); + assertThat(result.getResult(), is(50)); } @Test(expected = IllegalCommandArgumentException.class) @@ -33,7 +36,10 @@ public class IntegerArgumentTypeTest { @Test public void testParseLowerLimit() throws Exception { - assertThat(type.parse("-100"), is(-100)); + CommandArgumentType.CommandArgumentParseResult result = type.parse("-100"); + + assertThat(result.getRaw(), is("-100")); + assertThat(result.getResult(), is(-100)); } @Test(expected = IllegalCommandArgumentException.class) @@ -43,6 +49,9 @@ public class IntegerArgumentTypeTest { @Test public void testParseHigherLimit() throws Exception { - assertThat(type.parse("100"), is(100)); + CommandArgumentType.CommandArgumentParseResult result = type.parse("100"); + + assertThat(result.getRaw(), is("100")); + assertThat(result.getResult(), is(100)); } } \ No newline at end of file diff --git a/src/main/test/net/minecraft/commands/tree/ArgumentCommandNodeTest.java b/src/main/test/net/minecraft/commands/tree/ArgumentCommandNodeTest.java index 86f09fd..63b46d8 100644 --- a/src/main/test/net/minecraft/commands/tree/ArgumentCommandNodeTest.java +++ b/src/main/test/net/minecraft/commands/tree/ArgumentCommandNodeTest.java @@ -24,4 +24,16 @@ public class ArgumentCommandNodeTest { public void testParseInvalid() throws Exception { node.parse("bar"); } + + @Test + public void testParseChild() throws Exception { + node.addChild(argument("bar", integer()).build()); + + node.parse("123 123"); + } + + @Test(expected = IllegalCommandArgumentException.class) + public void testParseNoChildren() throws Exception { + node.parse("123 123"); + } } \ 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 index 98324d7..9132d46 100644 --- a/src/main/test/net/minecraft/commands/tree/LiteralCommandNodeTest.java +++ b/src/main/test/net/minecraft/commands/tree/LiteralCommandNodeTest.java @@ -4,7 +4,9 @@ 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.CommandBuilder.command; +import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; public class LiteralCommandNodeTest { LiteralCommandNode node; @@ -23,4 +25,16 @@ public class LiteralCommandNodeTest { public void testParseInvalid() throws Exception { node.parse("bar"); } + + @Test + public void testParseChild() throws Exception { + node.addChild(argument("bar", integer()).build()); + + node.parse("foo 123"); + } + + @Test(expected = IllegalCommandArgumentException.class) + public void testParseNoChildren() throws Exception { + node.parse("foo 123"); + } } \ No newline at end of file