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
@@ -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;
}
@@ -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();
}
}
}
@@ -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());
@@ -0,0 +1,4 @@
package net.minecraft.commands.exceptions;
public class IllegalCommandArgumentException extends CommandException {
}
@@ -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);
}
}
@@ -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;
}
@@ -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();
}
}
}