Allow specifying defaults for getArgument
This commit is contained in:
parent
a6c4baa1b6
commit
00f7681485
2 changed files with 70 additions and 2 deletions
|
@ -10,6 +10,7 @@ import com.mojang.brigadier.tree.CommandNode;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class CommandContext<S> {
|
||||
|
||||
|
@ -77,6 +78,17 @@ public class CommandContext<S> {
|
|||
return source;
|
||||
}
|
||||
|
||||
public boolean hasArgument(final String name) {
|
||||
return arguments.containsKey(name);
|
||||
}
|
||||
|
||||
public <V> boolean hasArgumentOfType(final String name, final Class<V> clazz) {
|
||||
final ParsedArgument<S, ?> argument = arguments.get(name);
|
||||
|
||||
return argument != null &&
|
||||
PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(argument.getResult().getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> V getArgument(final String name, final Class<V> clazz) {
|
||||
final ParsedArgument<S, ?> argument = arguments.get(name);
|
||||
|
@ -93,6 +105,38 @@ public class CommandContext<S> {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> V getArgumentOrDefault(final String name, final Class<V> clazz, final V defaultValue) {
|
||||
final ParsedArgument<S, ?> argument = arguments.get(name);
|
||||
|
||||
if (argument == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
final Object result = argument.getResult();
|
||||
if (PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(result.getClass())) {
|
||||
return (V) result;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> V getArgumentOrCompute(final String name, final Class<V> clazz, final Supplier<V> defaultSupplier) {
|
||||
final ParsedArgument<S, ?> argument = arguments.get(name);
|
||||
|
||||
if (argument == null) {
|
||||
return defaultSupplier.get();
|
||||
}
|
||||
|
||||
final Object result = argument.getResult();
|
||||
if (PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(result.getClass())) {
|
||||
return (V) result;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
|
|
|
@ -7,7 +7,6 @@ import com.google.common.testing.EqualsTester;
|
|||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
import com.mojang.brigadier.tree.RootCommandNode;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -45,12 +44,37 @@ public class CommandContextTest {
|
|||
context.getArgument("foo", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasArgument() throws Exception {
|
||||
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
|
||||
assertThat(context.hasArgument("foo"), is(true));
|
||||
assertThat(context.hasArgument("bar"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasArgumentOfType() throws Exception {
|
||||
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
|
||||
assertThat(context.hasArgumentOfType("foo", Integer.class), is(true));
|
||||
assertThat(context.hasArgumentOfType("foo", String.class), is(false));
|
||||
assertThat(context.hasArgumentOfType("bar", Object.class), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArgument() throws Exception {
|
||||
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
|
||||
assertThat(context.getArgument("foo", int.class), is(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArgumentOrDefault() throws Exception {
|
||||
assertThat(builder.build("").getArgumentOrDefault("foo", String.class, "bar"), is("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArgumentOrCompute() throws Exception {
|
||||
assertThat(builder.build("").getArgumentOrCompute("foo", String.class, () -> "bar"), is("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSource() throws Exception {
|
||||
assertThat(builder.build("").getSource(), is(source));
|
||||
|
|
Loading…
Reference in a new issue