✨ Allow command context to be retrieved using argument instances
This commit is contained in:
parent
2067eac600
commit
b564ecf60d
1 changed files with 45 additions and 1 deletions
|
|
@ -115,6 +115,24 @@ public final class CommandContext<C> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a value from its key. Will return {@link Optional#empty()}
|
||||||
|
* if no value is stored with the given key
|
||||||
|
*
|
||||||
|
* @param argument Argument
|
||||||
|
* @param <T> Value type
|
||||||
|
* @return Value
|
||||||
|
*/
|
||||||
|
public <T> @NonNull Optional<T> getOptional(final @NonNull CommandArgument<C, T> argument) {
|
||||||
|
final Object value = this.internalStorage.get(argument.getName());
|
||||||
|
if (value != null) {
|
||||||
|
@SuppressWarnings("ALL") final T castedValue = (T) value;
|
||||||
|
return Optional.of(castedValue);
|
||||||
|
} else {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a stored value from the context
|
* Remove a stored value from the context
|
||||||
*
|
*
|
||||||
|
|
@ -142,6 +160,32 @@ public final class CommandContext<C> {
|
||||||
return (T) value;
|
return (T) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a required argument from the context. This will thrown an exception
|
||||||
|
* if there's no value associated with the given argument
|
||||||
|
*
|
||||||
|
* @param argument The argument
|
||||||
|
* @param <T> Argument type
|
||||||
|
* @return Stored value
|
||||||
|
* @throws NullPointerException If no such value is stored
|
||||||
|
*/
|
||||||
|
public <T> @NonNull T get(final @NonNull CommandArgument<C, T> argument) {
|
||||||
|
return this.get(argument.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a value if it exists, else return the provided default value
|
||||||
|
*
|
||||||
|
* @param argument Argument
|
||||||
|
* @param defaultValue Default value
|
||||||
|
* @param <T> Argument type
|
||||||
|
* @return Stored value, or supplied default value
|
||||||
|
*/
|
||||||
|
public <T> @Nullable T getOrDefault(final @NonNull CommandArgument<C, T> argument,
|
||||||
|
final @Nullable T defaultValue) {
|
||||||
|
return this.<T>getOptional(argument.getName()).orElse(defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a value if it exists, else return the provided default value
|
* Get a value if it exists, else return the provided default value
|
||||||
*
|
*
|
||||||
|
|
@ -246,7 +290,7 @@ public final class CommandContext<C> {
|
||||||
/**
|
/**
|
||||||
* Set the end time
|
* Set the end time
|
||||||
*
|
*
|
||||||
* @param end End time (in nanoseconds)
|
* @param end End time (in nanoseconds)
|
||||||
* @param success Whether or not the argument was parsed successfully
|
* @param success Whether or not the argument was parsed successfully
|
||||||
*/
|
*/
|
||||||
public void setEnd(final long end, final boolean success) {
|
public void setEnd(final long end, final boolean success) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue