diff --git a/src/main/java/com/mojang/brigadier/context/CommandContext.java b/src/main/java/com/mojang/brigadier/context/CommandContext.java index bc5bb4a..6c019ca 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContext.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContext.java @@ -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 { @@ -77,6 +78,17 @@ public class CommandContext { return source; } + public boolean hasArgument(final String name) { + return arguments.containsKey(name); + } + + public boolean hasArgumentOfType(final String name, final Class clazz) { + final ParsedArgument argument = arguments.get(name); + + return argument != null && + PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(argument.getResult().getClass()); + } + @SuppressWarnings("unchecked") public V getArgument(final String name, final Class clazz) { final ParsedArgument argument = arguments.get(name); @@ -93,6 +105,38 @@ public class CommandContext { } } + @SuppressWarnings("unchecked") + public V getArgumentOrDefault(final String name, final Class clazz, final V defaultValue) { + final ParsedArgument 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 getArgumentOrCompute(final String name, final Class clazz, final Supplier defaultSupplier) { + final ParsedArgument 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; diff --git a/src/test/java/com/mojang/brigadier/context/CommandContextTest.java b/src/test/java/com/mojang/brigadier/context/CommandContextTest.java index 95d58a0..10aa95a 100644 --- a/src/test/java/com/mojang/brigadier/context/CommandContextTest.java +++ b/src/test/java/com/mojang/brigadier/context/CommandContextTest.java @@ -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 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 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 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(); } -} \ No newline at end of file +}