🎨 Change (improve) command context semantics
This commit is contained in:
parent
ee59066733
commit
9d47a7c82d
8 changed files with 33 additions and 30 deletions
|
|
@ -411,7 +411,7 @@ public abstract class CommandManager<C> {
|
|||
.through(new TypeToken<CommandPreprocessor<C>>() {
|
||||
})
|
||||
.getResult();
|
||||
return context.<String>get(AcceptingCommandPreprocessor.PROCESSED_INDICATOR_KEY).orElse("").isEmpty()
|
||||
return context.<String>getOptional(AcceptingCommandPreprocessor.PROCESSED_INDICATOR_KEY).orElse("").isEmpty()
|
||||
? State.REJECTED
|
||||
: State.ACCEPTED;
|
||||
}
|
||||
|
|
@ -430,7 +430,7 @@ public abstract class CommandManager<C> {
|
|||
.through(new TypeToken<CommandPostprocessor<C>>() {
|
||||
})
|
||||
.getResult();
|
||||
return context.<String>get(AcceptingCommandPostprocessor.PROCESSED_INDICATOR_KEY).orElse("").isEmpty()
|
||||
return context.<String>getOptional(AcceptingCommandPostprocessor.PROCESSED_INDICATOR_KEY).orElse("").isEmpty()
|
||||
? State.REJECTED
|
||||
: State.ACCEPTED;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,7 +83,8 @@ public final class CommandContext<C> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Store a value in the context map
|
||||
* Store a value in the context map. This will overwrite any existing
|
||||
* value stored with the same key
|
||||
*
|
||||
* @param key Key
|
||||
* @param value Value
|
||||
|
|
@ -94,13 +95,14 @@ public final class CommandContext<C> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get a value from its key
|
||||
* Get a value from its key. Will return {@link Optional#empty()}
|
||||
* if no value is stored with the given key
|
||||
*
|
||||
* @param key Key
|
||||
* @param <T> Value type
|
||||
* @return Value
|
||||
*/
|
||||
public <T> @NonNull Optional<T> get(@NonNull final String key) {
|
||||
public <T> @NonNull Optional<T> getOptional(@NonNull final String key) {
|
||||
final Object value = this.internalStorage.get(key);
|
||||
if (value != null) {
|
||||
@SuppressWarnings("ALL") final T castedValue = (T) value;
|
||||
|
|
@ -111,7 +113,8 @@ public final class CommandContext<C> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get a required argument from the context
|
||||
* Get a required argument from the context. This will thrown an exception
|
||||
* if there's no value associated with the given key
|
||||
*
|
||||
* @param key Argument key
|
||||
* @param <T> Argument type
|
||||
|
|
@ -119,7 +122,7 @@ public final class CommandContext<C> {
|
|||
* @throws NullPointerException If no such argument is stored
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> @NonNull T getRequired(@NonNull final String key) {
|
||||
public <T> @NonNull T get(@NonNull final String key) {
|
||||
final Object value = this.internalStorage.get(key);
|
||||
if (value == null) {
|
||||
throw new NullPointerException("No such object stored in the context: " + key);
|
||||
|
|
@ -137,7 +140,7 @@ public final class CommandContext<C> {
|
|||
*/
|
||||
public <T> @NonNull T getOrDefault(@NonNull final String key,
|
||||
@NonNull final T defaultValue) {
|
||||
return this.<T>get(key).orElse(defaultValue);
|
||||
return this.<T>getOptional(key).orElse(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -43,10 +43,10 @@ public class CommandPreProcessorTest {
|
|||
.argument(EnumArgument.of(SampleEnum.class, "enum"))
|
||||
.handler(
|
||||
commandContext -> System.out.printf("enum = %s | integer = %d\n",
|
||||
commandContext.<SampleEnum>get(
|
||||
commandContext.<SampleEnum>getOptional(
|
||||
"enum").orElse(
|
||||
SampleEnum.VALUE1),
|
||||
commandContext.<Integer>get(
|
||||
commandContext.<Integer>getOptional(
|
||||
"int").orElseThrow(
|
||||
() -> new NullPointerException(
|
||||
"int"))))
|
||||
|
|
@ -57,7 +57,7 @@ public class CommandPreProcessorTest {
|
|||
@Test
|
||||
void testPreprocessing() {
|
||||
Assertions.assertEquals(10, manager.executeCommand(new TestCommandSender(), "10 test value1")
|
||||
.join().getCommandContext().<Integer>get("int").orElse(0));
|
||||
.join().getCommandContext().<Integer>getOptional("int").orElse(0));
|
||||
manager.executeCommand(new TestCommandSender(), "aa test value1").join();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ class CommandTreeTest {
|
|||
Pair.of(Integer.class, Integer.class))
|
||||
.simple())
|
||||
.handler(c -> {
|
||||
final Pair<Integer, Integer> pair = c.getRequired("pos");
|
||||
final Pair<Integer, Integer> pair = c.get("pos");
|
||||
System.out.printf("X: %d | Y: %d\n", pair.getFirst(), pair.getSecond());
|
||||
})
|
||||
.build());
|
||||
|
|
@ -101,7 +101,7 @@ class CommandTreeTest {
|
|||
pair -> new Vector2(pair.getFirst(), pair.getSecond()))
|
||||
)
|
||||
.handler(c -> {
|
||||
final Vector2 vector2 = c.getRequired("vec");
|
||||
final Vector2 vector2 = c.get("vec");
|
||||
System.out.printf("X: %f | Y: %f\n", vector2.getX(), vector2.getY());
|
||||
})
|
||||
.build());
|
||||
|
|
@ -155,7 +155,7 @@ class CommandTreeTest {
|
|||
manager.commandBuilder("default")
|
||||
.argument(manager.argumentBuilder(Integer.class, "int").build())
|
||||
.handler(context -> {
|
||||
final int number = context.getRequired("int");
|
||||
final int number = context.get("int");
|
||||
System.out.printf("Supplied number is: %d\n", number);
|
||||
})
|
||||
.build()
|
||||
|
|
|
|||
|
|
@ -42,8 +42,8 @@ class StringArgumentTest {
|
|||
.argument(StringArgument.of("message1", StringArgument.StringMode.QUOTED))
|
||||
.argument(StringArgument.of("message2"))
|
||||
.handler(c -> {
|
||||
final String message1 = c.getRequired("message1");
|
||||
final String message2 = c.getRequired("message2");
|
||||
final String message1 = c.get("message1");
|
||||
final String message2 = c.get("message2");
|
||||
storage[0] = message1;
|
||||
storage[1] = message2;
|
||||
})
|
||||
|
|
@ -51,14 +51,14 @@ class StringArgumentTest {
|
|||
manager.command(manager.commandBuilder("single")
|
||||
.argument(StringArgument.of("message"))
|
||||
.handler(c -> {
|
||||
final String message = c.getRequired("message");
|
||||
final String message = c.get("message");
|
||||
storage[0] = message;
|
||||
})
|
||||
.build());
|
||||
manager.command(manager.commandBuilder("greedy")
|
||||
.argument(StringArgument.of("message", StringArgument.StringMode.GREEDY))
|
||||
.handler(c -> {
|
||||
final String message = c.getRequired("message");
|
||||
final String message = c.get("message");
|
||||
storage[0] = message;
|
||||
})
|
||||
.build());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue