Parse into nested arguments
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user