When parsing nodes, return the deepest node we went into

This commit is contained in:
Nathan Adams
2014-09-22 15:20:57 +02:00
parent 8d164e4b66
commit 397ab17e64
5 changed files with 28 additions and 17 deletions
@@ -21,24 +21,23 @@ public class ArgumentCommandNode<T> extends CommandNode {
} }
@Override @Override
public void parse(String command) throws IllegalCommandArgumentException { public CommandNode parse(String command) throws IllegalCommandArgumentException {
CommandArgumentType.CommandArgumentParseResult<T> parsed = type.parse(command); CommandArgumentType.CommandArgumentParseResult<T> parsed = type.parse(command);
int start = parsed.getRaw().length() + 1; int start = parsed.getRaw().length() + 1;
if (start < command.length()) { if (start < command.length()) {
String result = command.substring(start); String result = command.substring(start);
IllegalCommandArgumentException exception = new IllegalCommandArgumentException();
for (CommandNode node : getChildren()) { for (CommandNode node : getChildren()) {
try { try {
node.parse(result); return node.parse(result);
return; } catch (IllegalCommandArgumentException ignored) {
} catch (IllegalCommandArgumentException ex) {
exception = ex;
} }
} }
throw exception; throw new IllegalCommandArgumentException();
} else {
return this;
} }
} }
} }
@@ -16,5 +16,5 @@ public abstract class CommandNode {
children.add(node); children.add(node);
} }
public abstract void parse(String command) throws IllegalCommandArgumentException; public abstract CommandNode parse(String command) throws IllegalCommandArgumentException;
} }
@@ -20,7 +20,7 @@ public class LiteralCommandNode extends CommandNode {
} }
@Override @Override
public void parse(String command) throws IllegalCommandArgumentException { public CommandNode parse(String command) throws IllegalCommandArgumentException {
if (command.startsWith(literal)) { if (command.startsWith(literal)) {
int start = literal.length() + 1; int start = literal.length() + 1;
@@ -28,11 +28,15 @@ public class LiteralCommandNode extends CommandNode {
String result = command.substring(start); String result = command.substring(start);
for (CommandNode node : getChildren()) { for (CommandNode node : getChildren()) {
node.parse(result); try {
return; return node.parse(result);
} catch (IllegalCommandArgumentException ignored) {
}
} }
throw new IllegalCommandArgumentException(); throw new IllegalCommandArgumentException();
} else {
return this;
} }
} else { } else {
throw new IllegalCommandArgumentException(); throw new IllegalCommandArgumentException();
@@ -6,6 +6,8 @@ import org.junit.Test;
import static net.minecraft.commands.arguments.IntegerArgumentType.integer; import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class ArgumentCommandNodeTest { public class ArgumentCommandNodeTest {
ArgumentCommandNode node; ArgumentCommandNode node;
@@ -17,7 +19,7 @@ public class ArgumentCommandNodeTest {
@Test @Test
public void testParse() throws Exception { public void testParse() throws Exception {
node.parse("123"); assertThat((ArgumentCommandNode) node.parse("123"), is(node));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalCommandArgumentException.class)
@@ -27,9 +29,11 @@ public class ArgumentCommandNodeTest {
@Test @Test
public void testParseChild() throws Exception { public void testParseChild() throws Exception {
node.addChild(argument("bar", integer()).build()); CommandNode child = argument("bar", integer()).build();
node.parse("123 123"); node.addChild(child);
assertThat(node.parse("123 123"), is(child));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalCommandArgumentException.class)
@@ -7,6 +7,8 @@ import org.junit.Test;
import static net.minecraft.commands.arguments.IntegerArgumentType.integer; import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
import static net.minecraft.commands.builder.CommandBuilder.command; import static net.minecraft.commands.builder.CommandBuilder.command;
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument; import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class LiteralCommandNodeTest { public class LiteralCommandNodeTest {
LiteralCommandNode node; LiteralCommandNode node;
@@ -18,7 +20,7 @@ public class LiteralCommandNodeTest {
@Test @Test
public void testParse() throws Exception { public void testParse() throws Exception {
node.parse("foo"); assertThat((LiteralCommandNode) node.parse("foo"), is(node));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalCommandArgumentException.class)
@@ -28,9 +30,11 @@ public class LiteralCommandNodeTest {
@Test @Test
public void testParseChild() throws Exception { public void testParseChild() throws Exception {
node.addChild(argument("bar", integer()).build()); CommandNode child = argument("bar", integer()).build();
node.parse("foo 123"); node.addChild(child);
assertThat(node.parse("foo 123"), is(child));
} }
@Test(expected = IllegalCommandArgumentException.class) @Test(expected = IllegalCommandArgumentException.class)