Allow specifying defaults for getArgument

This commit is contained in:
bigfoot547 2022-11-08 22:45:34 -06:00
parent a6c4baa1b6
commit 00f7681485
No known key found for this signature in database
GPG key ID: B6231CB24795E060
2 changed files with 70 additions and 2 deletions

View file

@ -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;

View file

@ -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));
@ -82,4 +106,4 @@ public class CommandContextTest {
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withNode(otherNode, StringRange.between(0, 3)).withNode(node, StringRange.between(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withNode(otherNode, StringRange.between(0, 3)).withNode(node, StringRange.between(4, 6)).build("123 456"))
.testEquals();
}
}
}