Added command context and the ability to query arguments from it

This commit is contained in:
Nathan Adams
2014-09-24 13:49:00 +02:00
parent d13587a4df
commit 20357723df
6 changed files with 103 additions and 0 deletions
@@ -1,6 +1,7 @@
package net.minecraft.commands.arguments;
import com.google.common.base.Splitter;
import net.minecraft.commands.context.CommandContext;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
@@ -27,6 +28,10 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
return new IntegerArgumentType(min, max);
}
public static int getInteger(CommandContext context, String name) {
return context.getArgument(name, int.class).getResult();
}
@Override
public CommandArgumentParseResult<Integer> parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
String raw = SPLITTER.split(command).iterator().next();
@@ -0,0 +1,29 @@
package net.minecraft.commands.context;
import com.google.common.primitives.Primitives;
import net.minecraft.commands.arguments.CommandArgumentType;
import java.util.Map;
public class CommandContext {
private final Map<String, CommandArgumentType.CommandArgumentParseResult<?>> arguments;
public CommandContext(Map<String, CommandArgumentType.CommandArgumentParseResult<?>> arguments) {
this.arguments = arguments;
}
@SuppressWarnings("unchecked")
public <T> CommandArgumentType.CommandArgumentParseResult<T> getArgument(String name, Class<T> clazz) {
CommandArgumentType.CommandArgumentParseResult<?> argument = arguments.get(name);
if (argument == null) {
throw new IllegalArgumentException("No such argument '" + name + "' exists on this command");
}
if (Primitives.wrap(clazz).isAssignableFrom(argument.getResult().getClass())) {
return (CommandArgumentType.CommandArgumentParseResult<T>) argument;
} else {
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz);
}
}
}
@@ -0,0 +1,22 @@
package net.minecraft.commands.context;
import com.google.common.collect.Maps;
import net.minecraft.commands.arguments.CommandArgumentType;
import java.util.Map;
public class CommandContextBuilder {
private final Map<String, CommandArgumentType.CommandArgumentParseResult<?>> arguments = Maps.newHashMap();
public CommandContextBuilder() {
}
public CommandContextBuilder withArgument(String name, CommandArgumentType.CommandArgumentParseResult<?> argument) {
this.arguments.put(name, argument);
return this;
}
public CommandContext build() {
return new CommandContext(arguments);
}
}
@@ -0,0 +1,4 @@
@ParametersAreNonnullByDefault
package net.minecraft.commands.context;
import javax.annotation.ParametersAreNonnullByDefault;