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

View file

@ -21,24 +21,23 @@ public class ArgumentCommandNode<T> extends CommandNode {
}
@Override
public void parse(String command) throws IllegalCommandArgumentException {
public CommandNode parse(String command) throws IllegalCommandArgumentException {
CommandArgumentType.CommandArgumentParseResult<T> 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;
return node.parse(result);
} catch (IllegalCommandArgumentException ignored) {
}
}
throw exception;
throw new IllegalCommandArgumentException();
} else {
return this;
}
}
}

View file

@ -16,5 +16,5 @@ public abstract class CommandNode {
children.add(node);
}
public abstract void parse(String command) throws IllegalCommandArgumentException;
public abstract CommandNode parse(String command) throws IllegalCommandArgumentException;
}

View file

@ -20,7 +20,7 @@ public class LiteralCommandNode extends CommandNode {
}
@Override
public void parse(String command) throws IllegalCommandArgumentException {
public CommandNode parse(String command) throws IllegalCommandArgumentException {
if (command.startsWith(literal)) {
int start = literal.length() + 1;
@ -28,11 +28,15 @@ public class LiteralCommandNode extends CommandNode {
String result = command.substring(start);
for (CommandNode node : getChildren()) {
node.parse(result);
return;
try {
return node.parse(result);
} catch (IllegalCommandArgumentException ignored) {
}
}
throw new IllegalCommandArgumentException();
} else {
return this;
}
} else {
throw new IllegalCommandArgumentException();

View file

@ -6,6 +6,8 @@ import org.junit.Test;
import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class ArgumentCommandNodeTest {
ArgumentCommandNode node;
@ -17,7 +19,7 @@ public class ArgumentCommandNodeTest {
@Test
public void testParse() throws Exception {
node.parse("123");
assertThat((ArgumentCommandNode) node.parse("123"), is(node));
}
@Test(expected = IllegalCommandArgumentException.class)
@ -27,9 +29,11 @@ public class ArgumentCommandNodeTest {
@Test
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)

View file

@ -7,6 +7,8 @@ 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;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class LiteralCommandNodeTest {
LiteralCommandNode node;
@ -18,7 +20,7 @@ public class LiteralCommandNodeTest {
@Test
public void testParse() throws Exception {
node.parse("foo");
assertThat((LiteralCommandNode) node.parse("foo"), is(node));
}
@Test(expected = IllegalCommandArgumentException.class)
@ -28,9 +30,11 @@ public class LiteralCommandNodeTest {
@Test
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)