Parse into nested arguments
This commit is contained in:
@@ -3,5 +3,23 @@ package net.minecraft.commands.arguments;
|
|||||||
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
|
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
|
||||||
|
|
||||||
public interface CommandArgumentType<T> {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package net.minecraft.commands.arguments;
|
package net.minecraft.commands.arguments;
|
||||||
|
|
||||||
|
import com.google.common.base.Splitter;
|
||||||
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
|
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
|
||||||
|
|
||||||
public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
||||||
|
private static final Splitter SPLITTER = Splitter.on(' ').limit(2);
|
||||||
|
|
||||||
private final int minimum;
|
private final int minimum;
|
||||||
private final int maximum;
|
private final int maximum;
|
||||||
|
|
||||||
@@ -24,9 +27,11 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer parse(String command) throws IllegalCommandArgumentException {
|
public CommandArgumentParseResult<Integer> parse(String command) throws IllegalCommandArgumentException {
|
||||||
|
String raw = SPLITTER.split(command).iterator().next();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int value = Integer.parseInt(command);
|
int value = Integer.parseInt(raw);
|
||||||
|
|
||||||
if (value < minimum) {
|
if (value < minimum) {
|
||||||
throw new IllegalCommandArgumentException();
|
throw new IllegalCommandArgumentException();
|
||||||
@@ -35,7 +40,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
|||||||
throw new IllegalCommandArgumentException();
|
throw new IllegalCommandArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return new CommandArgumentParseResult<Integer>(raw, value);
|
||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
throw new IllegalCommandArgumentException();
|
throw new IllegalCommandArgumentException();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,23 @@ public class ArgumentCommandNode<T> extends CommandNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String command) throws IllegalCommandArgumentException {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,20 @@ public class LiteralCommandNode extends CommandNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String command) throws IllegalCommandArgumentException {
|
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();
|
throw new IllegalCommandArgumentException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package net.minecraft.commands;
|
|
||||||
|
|
||||||
import net.minecraft.commands.tree.CommandNode;
|
|
||||||
|
|
||||||
public class CommandNodeTest {
|
|
||||||
CommandNode node;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -18,7 +18,10 @@ public class IntegerArgumentTypeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse() throws Exception {
|
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)
|
@Test(expected = IllegalCommandArgumentException.class)
|
||||||
@@ -33,7 +36,10 @@ public class IntegerArgumentTypeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseLowerLimit() throws Exception {
|
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)
|
@Test(expected = IllegalCommandArgumentException.class)
|
||||||
@@ -43,6 +49,9 @@ public class IntegerArgumentTypeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseHigherLimit() throws Exception {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,4 +24,16 @@ public class ArgumentCommandNodeTest {
|
|||||||
public void testParseInvalid() throws Exception {
|
public void testParseInvalid() throws Exception {
|
||||||
node.parse("bar");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,9 @@ import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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.CommandBuilder.command;
|
||||||
|
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
|
||||||
|
|
||||||
public class LiteralCommandNodeTest {
|
public class LiteralCommandNodeTest {
|
||||||
LiteralCommandNode node;
|
LiteralCommandNode node;
|
||||||
@@ -23,4 +25,16 @@ public class LiteralCommandNodeTest {
|
|||||||
public void testParseInvalid() throws Exception {
|
public void testParseInvalid() throws Exception {
|
||||||
node.parse("bar");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user