Commands can be parsed, in a very limited way.

This commit is contained in:
Nathan Adams 2014-09-18 11:48:03 +02:00
parent 2b267a26f6
commit a32243b704
11 changed files with 163 additions and 17 deletions

View file

@ -1,4 +1,7 @@
package net.minecraft.commands.arguments;
public interface CommandArgumentType {
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
public interface CommandArgumentType<T> {
T parse(String command) throws IllegalCommandArgumentException;
}

View file

@ -1,6 +1,8 @@
package net.minecraft.commands.arguments;
public class IntegerArgumentType implements CommandArgumentType {
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
public class IntegerArgumentType implements CommandArgumentType<Integer> {
private final int minimum;
private final int maximum;
@ -20,4 +22,22 @@ public class IntegerArgumentType implements CommandArgumentType {
public static IntegerArgumentType integer(int min, int max) {
return new IntegerArgumentType(min, max);
}
@Override
public Integer parse(String command) throws IllegalCommandArgumentException {
try {
int value = Integer.parseInt(command);
if (value < minimum) {
throw new IllegalCommandArgumentException();
}
if (value > maximum) {
throw new IllegalCommandArgumentException();
}
return value;
} catch (NumberFormatException ignored) {
throw new IllegalCommandArgumentException();
}
}
}

View file

@ -3,20 +3,20 @@ package net.minecraft.commands.builder;
import net.minecraft.commands.arguments.CommandArgumentType;
import net.minecraft.commands.tree.ArgumentCommandNode;
public class RequiredArgumentBuilder extends ArgumentBuilder {
public class RequiredArgumentBuilder<T> extends ArgumentBuilder {
private final String name;
private final CommandArgumentType type;
private final CommandArgumentType<T> type;
protected RequiredArgumentBuilder(String name, CommandArgumentType type) {
protected RequiredArgumentBuilder(String name, CommandArgumentType<T> type) {
this.name = name;
this.type = type;
}
public static RequiredArgumentBuilder argument(String name, CommandArgumentType type) {
return new RequiredArgumentBuilder(name, type);
public static <T> RequiredArgumentBuilder<T> argument(String name, CommandArgumentType<T> type) {
return new RequiredArgumentBuilder<T>(name, type);
}
public CommandArgumentType getType() {
public CommandArgumentType<T> getType() {
return type;
}
@ -24,8 +24,8 @@ public class RequiredArgumentBuilder extends ArgumentBuilder {
return name;
}
public ArgumentCommandNode build() {
ArgumentCommandNode result = new ArgumentCommandNode(getName(), getType());
public ArgumentCommandNode<T> build() {
ArgumentCommandNode<T> result = new ArgumentCommandNode<T>(getName(), getType());
for (ArgumentBuilder argument : getArguments()) {
result.addChild(argument.build());

View file

@ -0,0 +1,4 @@
package net.minecraft.commands.exceptions;
public class IllegalCommandArgumentException extends CommandException {
}

View file

@ -1,12 +1,13 @@
package net.minecraft.commands.tree;
import net.minecraft.commands.arguments.CommandArgumentType;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
public class ArgumentCommandNode extends CommandNode {
public class ArgumentCommandNode<T> extends CommandNode {
private final String name;
private final CommandArgumentType type;
private final CommandArgumentType<T> type;
public ArgumentCommandNode(String name, CommandArgumentType type) {
public ArgumentCommandNode(String name, CommandArgumentType<T> type) {
this.name = name;
this.type = type;
}
@ -15,7 +16,12 @@ public class ArgumentCommandNode extends CommandNode {
return name;
}
public CommandArgumentType getType() {
public CommandArgumentType<T> getType() {
return type;
}
@Override
public void parse(String command) throws IllegalCommandArgumentException {
type.parse(command);
}
}

View file

@ -1,6 +1,7 @@
package net.minecraft.commands.tree;
import com.google.common.collect.Lists;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
import java.util.List;
@ -14,4 +15,6 @@ public abstract class CommandNode {
public void addChild(CommandNode node) {
children.add(node);
}
public abstract void parse(String command) throws IllegalCommandArgumentException;
}

View file

@ -1,5 +1,7 @@
package net.minecraft.commands.tree;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
public class LiteralCommandNode extends CommandNode {
private final String literal;
private final Runnable executor;
@ -16,4 +18,11 @@ public class LiteralCommandNode extends CommandNode {
public Runnable getExecutor() {
return executor;
}
@Override
public void parse(String command) throws IllegalCommandArgumentException {
if (!command.equals(literal)) {
throw new IllegalCommandArgumentException();
}
}
}

View file

@ -0,0 +1,48 @@
package net.minecraft.commands.arguments;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
import org.junit.Before;
import org.junit.Test;
import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class IntegerArgumentTypeTest {
IntegerArgumentType type;
@Before
public void setUp() throws Exception {
type = integer(-100, 100);
}
@Test
public void testParse() throws Exception {
assertThat(type.parse("50"), is(50));
}
@Test(expected = IllegalCommandArgumentException.class)
public void testParseInvalid() throws Exception {
type.parse("fifty");
}
@Test(expected = IllegalCommandArgumentException.class)
public void testParseTooLow() throws Exception {
type.parse("-101");
}
@Test
public void testParseLowerLimit() throws Exception {
assertThat(type.parse("-100"), is(-100));
}
@Test(expected = IllegalCommandArgumentException.class)
public void testParseTooHigh() throws Exception {
type.parse("101");
}
@Test
public void testParseHigherLimit() throws Exception {
assertThat(type.parse("100"), is(100));
}
}

View file

@ -13,8 +13,8 @@ import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class RequiredArgumentBuilderTest {
@Mock CommandArgumentType type;
RequiredArgumentBuilder builder;
@Mock CommandArgumentType<Integer> type;
RequiredArgumentBuilder<Integer> builder;
@Before
public void setUp() throws Exception {
@ -23,7 +23,7 @@ public class RequiredArgumentBuilderTest {
@Test
public void testBuild() throws Exception {
ArgumentCommandNode node = builder.build();
ArgumentCommandNode<Integer> node = builder.build();
assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type));

View file

@ -0,0 +1,27 @@
package net.minecraft.commands.tree;
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.RequiredArgumentBuilder.argument;
public class ArgumentCommandNodeTest {
ArgumentCommandNode node;
@Before
public void setUp() throws Exception {
node = argument("foo", integer()).build();
}
@Test
public void testParse() throws Exception {
node.parse("123");
}
@Test(expected = IllegalCommandArgumentException.class)
public void testParseInvalid() throws Exception {
node.parse("bar");
}
}

View file

@ -0,0 +1,26 @@
package net.minecraft.commands.tree;
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
import org.junit.Before;
import org.junit.Test;
import static net.minecraft.commands.builder.CommandBuilder.command;
public class LiteralCommandNodeTest {
LiteralCommandNode node;
@Before
public void setUp() throws Exception {
node = command("foo").build();
}
@Test
public void testParse() throws Exception {
node.parse("foo");
}
@Test(expected = IllegalCommandArgumentException.class)
public void testParseInvalid() throws Exception {
node.parse("bar");
}
}