Moved CommandArgumentParseResult to its own class
This commit is contained in:
parent
20357723df
commit
81ed5f0521
7 changed files with 37 additions and 34 deletions
|
@ -1,26 +1,9 @@
|
||||||
package net.minecraft.commands.arguments;
|
package net.minecraft.commands.arguments;
|
||||||
|
|
||||||
|
import net.minecraft.commands.context.ParsedArgument;
|
||||||
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
||||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||||
|
|
||||||
public interface CommandArgumentType<T> {
|
public interface CommandArgumentType<T> {
|
||||||
CommandArgumentParseResult<T> parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException;
|
ParsedArgument<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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package net.minecraft.commands.arguments;
|
||||||
|
|
||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
import net.minecraft.commands.context.CommandContext;
|
import net.minecraft.commands.context.CommandContext;
|
||||||
|
import net.minecraft.commands.context.ParsedArgument;
|
||||||
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
||||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||||
|
|
||||||
|
@ -33,7 +34,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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();
|
String raw = SPLITTER.split(command).iterator().next();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -46,7 +47,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
||||||
throw new ArgumentValidationException();
|
throw new ArgumentValidationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CommandArgumentParseResult<Integer>(raw, value);
|
return new ParsedArgument<Integer>(raw, value);
|
||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
throw new IllegalArgumentSyntaxException();
|
throw new IllegalArgumentSyntaxException();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,26 @@
|
||||||
package net.minecraft.commands.context;
|
package net.minecraft.commands.context;
|
||||||
|
|
||||||
import com.google.common.primitives.Primitives;
|
import com.google.common.primitives.Primitives;
|
||||||
import net.minecraft.commands.arguments.CommandArgumentType;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class CommandContext {
|
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;
|
this.arguments = arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> CommandArgumentType.CommandArgumentParseResult<T> getArgument(String name, Class<T> clazz) {
|
public <T> ParsedArgument<T> getArgument(String name, Class<T> clazz) {
|
||||||
CommandArgumentType.CommandArgumentParseResult<?> argument = arguments.get(name);
|
ParsedArgument<?> argument = arguments.get(name);
|
||||||
|
|
||||||
if (argument == null) {
|
if (argument == null) {
|
||||||
throw new IllegalArgumentException("No such argument '" + name + "' exists on this command");
|
throw new IllegalArgumentException("No such argument '" + name + "' exists on this command");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Primitives.wrap(clazz).isAssignableFrom(argument.getResult().getClass())) {
|
if (Primitives.wrap(clazz).isAssignableFrom(argument.getResult().getClass())) {
|
||||||
return (CommandArgumentType.CommandArgumentParseResult<T>) argument;
|
return (ParsedArgument<T>) argument;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz);
|
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + argument.getResult().getClass().getSimpleName() + ", not " + clazz);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
package net.minecraft.commands.context;
|
package net.minecraft.commands.context;
|
||||||
|
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import net.minecraft.commands.arguments.CommandArgumentType;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class CommandContextBuilder {
|
public class CommandContextBuilder {
|
||||||
private final Map<String, CommandArgumentType.CommandArgumentParseResult<?>> arguments = Maps.newHashMap();
|
private final Map<String, ParsedArgument<?>> arguments = Maps.newHashMap();
|
||||||
|
|
||||||
public CommandContextBuilder() {
|
public CommandContextBuilder() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandContextBuilder withArgument(String name, CommandArgumentType.CommandArgumentParseResult<?> argument) {
|
public CommandContextBuilder withArgument(String name, ParsedArgument<?> argument) {
|
||||||
this.arguments.put(name, argument);
|
this.arguments.put(name, argument);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
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.context.ParsedArgument;
|
||||||
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
||||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||||
|
|
||||||
|
@ -24,7 +25,7 @@ public class ArgumentCommandNode<T> extends CommandNode {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandNode parse(String command) throws IllegalArgumentSyntaxException, ArgumentValidationException {
|
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;
|
int start = parsed.getRaw().length() + 1;
|
||||||
|
|
||||||
if (start < command.length()) {
|
if (start < command.length()) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package net.minecraft.commands.arguments;
|
||||||
|
|
||||||
import net.minecraft.commands.context.CommandContext;
|
import net.minecraft.commands.context.CommandContext;
|
||||||
import net.minecraft.commands.context.CommandContextBuilder;
|
import net.minecraft.commands.context.CommandContextBuilder;
|
||||||
|
import net.minecraft.commands.context.ParsedArgument;
|
||||||
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
||||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -21,7 +22,7 @@ public class IntegerArgumentTypeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse() throws Exception {
|
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.getRaw(), is("50"));
|
||||||
assertThat(result.getResult(), is(50));
|
assertThat(result.getResult(), is(50));
|
||||||
|
@ -39,7 +40,7 @@ public class IntegerArgumentTypeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseLowerLimit() throws Exception {
|
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.getRaw(), is("-100"));
|
||||||
assertThat(result.getResult(), is(-100));
|
assertThat(result.getResult(), is(-100));
|
||||||
|
@ -52,7 +53,7 @@ public class IntegerArgumentTypeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseHigherLimit() throws Exception {
|
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.getRaw(), is("100"));
|
||||||
assertThat(result.getResult(), is(100));
|
assertThat(result.getResult(), is(100));
|
||||||
|
|
Loading…
Reference in a new issue