Commands can be parsed, in a very limited way.
This commit is contained in:
parent
2b267a26f6
commit
a32243b704
11 changed files with 163 additions and 17 deletions
|
@ -1,4 +1,7 @@
|
||||||
package net.minecraft.commands.arguments;
|
package net.minecraft.commands.arguments;
|
||||||
|
|
||||||
public interface CommandArgumentType {
|
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
|
||||||
|
|
||||||
|
public interface CommandArgumentType<T> {
|
||||||
|
T parse(String command) throws IllegalCommandArgumentException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package net.minecraft.commands.arguments;
|
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 minimum;
|
||||||
private final int maximum;
|
private final int maximum;
|
||||||
|
|
||||||
|
@ -20,4 +22,22 @@ public class IntegerArgumentType implements CommandArgumentType {
|
||||||
public static IntegerArgumentType integer(int min, int max) {
|
public static IntegerArgumentType integer(int min, int max) {
|
||||||
return new IntegerArgumentType(min, 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,20 +3,20 @@ package net.minecraft.commands.builder;
|
||||||
import net.minecraft.commands.arguments.CommandArgumentType;
|
import net.minecraft.commands.arguments.CommandArgumentType;
|
||||||
import net.minecraft.commands.tree.ArgumentCommandNode;
|
import net.minecraft.commands.tree.ArgumentCommandNode;
|
||||||
|
|
||||||
public class RequiredArgumentBuilder extends ArgumentBuilder {
|
public class RequiredArgumentBuilder<T> extends ArgumentBuilder {
|
||||||
private final String name;
|
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.name = name;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RequiredArgumentBuilder argument(String name, CommandArgumentType type) {
|
public static <T> RequiredArgumentBuilder<T> argument(String name, CommandArgumentType<T> type) {
|
||||||
return new RequiredArgumentBuilder(name, type);
|
return new RequiredArgumentBuilder<T>(name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandArgumentType getType() {
|
public CommandArgumentType<T> getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,8 +24,8 @@ public class RequiredArgumentBuilder extends ArgumentBuilder {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArgumentCommandNode build() {
|
public ArgumentCommandNode<T> build() {
|
||||||
ArgumentCommandNode result = new ArgumentCommandNode(getName(), getType());
|
ArgumentCommandNode<T> result = new ArgumentCommandNode<T>(getName(), getType());
|
||||||
|
|
||||||
for (ArgumentBuilder argument : getArguments()) {
|
for (ArgumentBuilder argument : getArguments()) {
|
||||||
result.addChild(argument.build());
|
result.addChild(argument.build());
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
package net.minecraft.commands.exceptions;
|
||||||
|
|
||||||
|
public class IllegalCommandArgumentException extends CommandException {
|
||||||
|
}
|
|
@ -1,12 +1,13 @@
|
||||||
package net.minecraft.commands.tree;
|
package net.minecraft.commands.tree;
|
||||||
|
|
||||||
import net.minecraft.commands.arguments.CommandArgumentType;
|
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 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.name = name;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +16,12 @@ public class ArgumentCommandNode extends CommandNode {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandArgumentType getType() {
|
public CommandArgumentType<T> getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void parse(String command) throws IllegalCommandArgumentException {
|
||||||
|
type.parse(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package net.minecraft.commands.tree;
|
package net.minecraft.commands.tree;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -14,4 +15,6 @@ public abstract class CommandNode {
|
||||||
public void addChild(CommandNode node) {
|
public void addChild(CommandNode node) {
|
||||||
children.add(node);
|
children.add(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract void parse(String command) throws IllegalCommandArgumentException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package net.minecraft.commands.tree;
|
package net.minecraft.commands.tree;
|
||||||
|
|
||||||
|
import net.minecraft.commands.exceptions.IllegalCommandArgumentException;
|
||||||
|
|
||||||
public class LiteralCommandNode extends CommandNode {
|
public class LiteralCommandNode extends CommandNode {
|
||||||
private final String literal;
|
private final String literal;
|
||||||
private final Runnable executor;
|
private final Runnable executor;
|
||||||
|
@ -16,4 +18,11 @@ public class LiteralCommandNode extends CommandNode {
|
||||||
public Runnable getExecutor() {
|
public Runnable getExecutor() {
|
||||||
return executor;
|
return executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void parse(String command) throws IllegalCommandArgumentException {
|
||||||
|
if (!command.equals(literal)) {
|
||||||
|
throw new IllegalCommandArgumentException();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,8 +13,8 @@ import static org.hamcrest.Matchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
public class RequiredArgumentBuilderTest {
|
public class RequiredArgumentBuilderTest {
|
||||||
@Mock CommandArgumentType type;
|
@Mock CommandArgumentType<Integer> type;
|
||||||
RequiredArgumentBuilder builder;
|
RequiredArgumentBuilder<Integer> builder;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
@ -23,7 +23,7 @@ public class RequiredArgumentBuilderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBuild() throws Exception {
|
public void testBuild() throws Exception {
|
||||||
ArgumentCommandNode node = builder.build();
|
ArgumentCommandNode<Integer> node = builder.build();
|
||||||
|
|
||||||
assertThat(node.getName(), is("foo"));
|
assertThat(node.getName(), is("foo"));
|
||||||
assertThat(node.getType(), is(type));
|
assertThat(node.getType(), is(type));
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue