Added command context and the ability to query arguments from it
This commit is contained in:
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;
|
|
@ -1,5 +1,7 @@
|
|||
package net.minecraft.commands.arguments;
|
||||
|
||||
import net.minecraft.commands.context.CommandContext;
|
||||
import net.minecraft.commands.context.CommandContextBuilder;
|
||||
import net.minecraft.commands.exceptions.ArgumentValidationException;
|
||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||
import org.junit.Before;
|
||||
|
@ -55,4 +57,11 @@ public class IntegerArgumentTypeTest {
|
|||
assertThat(result.getRaw(), is("100"));
|
||||
assertThat(result.getResult(), is(100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInteger() throws Exception {
|
||||
CommandContext context = new CommandContextBuilder().withArgument("foo", type.parse("100")).build();
|
||||
|
||||
assertThat(IntegerArgumentType.getInteger(context, "foo"), is(100));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package net.minecraft.commands.context;
|
||||
|
||||
import net.minecraft.commands.arguments.IntegerArgumentType;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class CommandContextTest {
|
||||
CommandContext context;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
context = new CommandContextBuilder().build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetArgument_nonexistent() throws Exception {
|
||||
context.getArgument("foo", Object.class);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetArgument_wrongType() throws Exception {
|
||||
context = new CommandContextBuilder().withArgument("foo", IntegerArgumentType.integer().parse("123")).build();
|
||||
context.getArgument("foo", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArgument() throws Exception {
|
||||
context = new CommandContextBuilder().withArgument("foo", IntegerArgumentType.integer().parse("123")).build();
|
||||
assertThat(context.getArgument("foo", int.class).getResult(), is(123));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue