Moved CommandArgumentParseResult to its own class

This commit is contained in:
Nathan Adams 2014-09-24 13:53:23 +02:00
parent 20357723df
commit 81ed5f0521
7 changed files with 37 additions and 34 deletions

View file

@ -1,26 +1,9 @@
package net.minecraft.commands.arguments;
import net.minecraft.commands.context.ParsedArgument;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
public interface CommandArgumentType<T> {
CommandArgumentParseResult<T> parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException;
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;
}
}
ParsedArgument<T> parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException;
}

View file

@ -2,6 +2,7 @@ package net.minecraft.commands.arguments;
import com.google.common.base.Splitter;
import net.minecraft.commands.context.CommandContext;
import net.minecraft.commands.context.ParsedArgument;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
@ -33,7 +34,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
}
@Override
public CommandArgumentParseResult<Integer> parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
public ParsedArgument<Integer> parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
String raw = SPLITTER.split(command).iterator().next();
try {
@ -46,7 +47,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
throw new ArgumentValidationException();
}
return new CommandArgumentParseResult<Integer>(raw, value);
return new ParsedArgument<Integer>(raw, value);
} catch (NumberFormatException ignored) {
throw new IllegalArgumentSyntaxException();
}

View file

@ -1,27 +1,26 @@
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;
private final Map<String, ParsedArgument<?>> arguments;
public CommandContext(Map<String, CommandArgumentType.CommandArgumentParseResult<?>> arguments) {
public CommandContext(Map<String, ParsedArgument<?>> arguments) {
this.arguments = arguments;
}
@SuppressWarnings("unchecked")
public <T> CommandArgumentType.CommandArgumentParseResult<T> getArgument(String name, Class<T> clazz) {
CommandArgumentType.CommandArgumentParseResult<?> argument = arguments.get(name);
public <T> ParsedArgument<T> getArgument(String name, Class<T> clazz) {
ParsedArgument<?> 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;
return (ParsedArgument<T>) argument;
} else {
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz);
}

View file

@ -1,17 +1,16 @@
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();
private final Map<String, ParsedArgument<?>> arguments = Maps.newHashMap();
public CommandContextBuilder() {
}
public CommandContextBuilder withArgument(String name, CommandArgumentType.CommandArgumentParseResult<?> argument) {
public CommandContextBuilder withArgument(String name, ParsedArgument<?> argument) {
this.arguments.put(name, argument);
return this;
}

View file

@ -0,0 +1,19 @@
package net.minecraft.commands.context;
public class ParsedArgument<T> {
private final String raw;
private final T result;
public ParsedArgument(String raw, T result) {
this.raw = raw;
this.result = result;
}
public String getRaw() {
return raw;
}
public T getResult() {
return result;
}
}

View file

@ -1,6 +1,7 @@
package net.minecraft.commands.tree;
import net.minecraft.commands.arguments.CommandArgumentType;
import net.minecraft.commands.context.ParsedArgument;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
@ -24,7 +25,7 @@ public class ArgumentCommandNode<T> extends CommandNode {
@Override
public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
CommandArgumentType.CommandArgumentParseResult<T> parsed = type.parse(command);
ParsedArgument<T> parsed = type.parse(command);
int start = parsed.getRaw().length() + 1;
if (start < command.length()) {

View file

@ -2,6 +2,7 @@ package net.minecraft.commands.arguments;
import net.minecraft.commands.context.CommandContext;
import net.minecraft.commands.context.CommandContextBuilder;
import net.minecraft.commands.context.ParsedArgument;
import net.minecraft.commands.exceptions.ArgumentValidationException;
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
import org.junit.Before;
@ -21,7 +22,7 @@ public class IntegerArgumentTypeTest {
@Test
public void testParse() throws Exception {
CommandArgumentType.CommandArgumentParseResult<Integer> result = type.parse("50");
ParsedArgument<Integer> result = type.parse("50");
assertThat(result.getRaw(), is("50"));
assertThat(result.getResult(), is(50));
@ -39,7 +40,7 @@ public class IntegerArgumentTypeTest {
@Test
public void testParseLowerLimit() throws Exception {
CommandArgumentType.CommandArgumentParseResult<Integer> result = type.parse("-100");
ParsedArgument<Integer> result = type.parse("-100");
assertThat(result.getRaw(), is("-100"));
assertThat(result.getResult(), is(-100));
@ -52,7 +53,7 @@ public class IntegerArgumentTypeTest {
@Test
public void testParseHigherLimit() throws Exception {
CommandArgumentType.CommandArgumentParseResult<Integer> result = type.parse("100");
ParsedArgument<Integer> result = type.parse("100");
assertThat(result.getRaw(), is("100"));
assertThat(result.getResult(), is(100));