Parse into nested arguments

This commit is contained in:
Nathan Adams 2014-09-18 16:36:36 +02:00
parent a32243b704
commit 8d164e4b66
8 changed files with 97 additions and 17 deletions

View file

@ -3,5 +3,23 @@ package net.minecraft.commands.arguments;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
public interface CommandArgumentType<T> {
T parse(String command) throws IllegalCommandArgumentException;
CommandArgumentParseResult<T> parse(String command) throws IllegalCommandArgumentException;
class CommandArgumentParseResult<T> {
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;
}
}
}

View file

@ -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<Integer> {
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<Integer> {
}
@Override
public Integer parse(String command) throws IllegalCommandArgumentException {
public CommandArgumentParseResult<Integer> 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<Integer> {
throw new IllegalCommandArgumentException();
}
return value;
return new CommandArgumentParseResult<Integer>(raw, value);
} catch (NumberFormatException ignored) {
throw new IllegalCommandArgumentException();
}

View file

@ -22,6 +22,23 @@ public class ArgumentCommandNode<T> extends CommandNode {
@Override
public void parse(String command) throws IllegalCommandArgumentException {
type.parse(command);
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;
}
}
throw exception;
}
}
}

View file

@ -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();
}
}

View file

@ -1,8 +0,0 @@
package net.minecraft.commands;
import net.minecraft.commands.tree.CommandNode;
public class CommandNodeTest {
CommandNode node;
}

View file

@ -18,7 +18,10 @@ public class IntegerArgumentTypeTest {
@Test
public void testParse() throws Exception {
assertThat(type.parse("50"), is(50));
CommandArgumentType.CommandArgumentParseResult<Integer> 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<Integer> 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<Integer> result = type.parse("100");
assertThat(result.getRaw(), is("100"));
assertThat(result.getResult(), is(100));
}
}

View file

@ -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");
}
}

View file

@ -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");
}
}