Package cloud.commandframework.context
Class CommandContext<C>
java.lang.Object
cloud.commandframework.context.CommandContext<C>
- Type Parameters:
C
- Command sender type
Command context used to assist in the parsing of commands
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Used to track performance metrics related to command parsing. -
Constructor Summary
ConstructorsConstructorDescriptionCommandContext
(boolean suggestions, @NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry) Deprecated.Provide a command manager instead of a caption registryCommandContext
(boolean suggestions, @NonNull C commandSender, @NonNull CommandManager<C> commandManager) Create a new command context instanceCommandContext
(@NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry) Deprecated.Provide a command manager instead of a caption registryCommandContext
(@NonNull C commandSender, @NonNull CommandManager<C> commandManager) Create a new command context instance -
Method Summary
Modifier and TypeMethodDescriptionasMap()
Get the current state of this command context as a map of String to context value.boolean
Check if the context has a value stored for a keyboolean
Check if the context has a value stored for a key@NonNull CommandContext.ArgumentTiming
createTiming
(@NonNull CommandArgument<C, ?> argument) Create an argument timing for a specific argument@NonNull FlagContext
flags()
Get the associatedFlagContext
instance@NonNull String
formatMessage
(@NonNull Caption caption, @NonNull CaptionVariable... variables) Format a caption<T> @NonNull T
get
(@NonNull CommandArgument<C, T> keyHolder) Get a required argument from the context.<T> @NonNull T
Get a required argument from the context.<T> @NonNull T
get
(@NonNull CloudKeyHolder<T> keyHolder) Get a required argument from the context.<T> @NonNull T
Get a required argument from the context.@NonNull Map<CommandArgument<@NonNull C,
@NonNull ?>, CommandContext.ArgumentTiming> Get an immutable view of the argument timings map@Nullable CommandArgument<C,
?> Get the argument that is currently being parsed for this command context.<T> @NonNull Optional<T>
getOptional
(@NonNull CommandArgument<C, T> keyHolder) Get a value from its key.<T> @NonNull Optional<T>
getOptional
(@NonNull CloudKey<T> key) Get a value from its key.<T> @NonNull Optional<T>
getOptional
(@NonNull CloudKeyHolder<T> keyHolder) Get a value from its key.<T> @NonNull Optional<T>
getOptional
(@NonNull String key) Get a value from its key.<T> @Nullable T
getOrDefault
(@NonNull CommandArgument<C, T> argument, @Nullable T defaultValue) Get a value if it exists, else return the provided default value<T> @Nullable T
getOrDefault
(@NonNull CloudKey<T> key, @Nullable T defaultValue) Get a value if it exists, else return the provided default value<T> @Nullable T
getOrDefault
(@NonNull String key, @Nullable T defaultValue) Get a value if it exists, else return the provided default value<T> @Nullable T
getOrSupplyDefault
(@NonNull CloudKey<T> key, @NonNull Supplier<@Nullable T> defaultSupplier) Get a value if it exists, else return the value supplied by the given supplier<T> @Nullable T
getOrSupplyDefault
(@NonNull String key, @NonNull Supplier<@Nullable T> defaultSupplier) Get a value if it exists, else return the value supplied by the given supplier@NonNull LinkedList<@NonNull String>
Get the raw input.@NonNull String
Get the raw input as a joined string@NonNull C
Get the sender that executed the commandboolean
hasPermission
(@NonNull CommandPermission permission) Check whether the sender that executed the command has a permission.boolean
hasPermission
(@NonNull String permission) Check whether the sender that executed the command has a permission.<@NonNull T>
@NonNull Optional<T>Attempt to retrieve a value that has been registered to the associated command manager'sParameterInjectorRegistry
boolean
Check if this context was created for tab completion purposesvoid
Remove a stored value from the contextvoid
Remove a stored value from the context<T> void
Store or remove a value in the context map.<T> void
Store or remove a value in the context map.void
setCurrentArgument
(@Nullable CommandArgument<C, ?> argument) Set the argument that is currently being parsed for this command context.<T> void
store
(@NonNull CommandArgument<C, T> keyHolder, @NonNull T value) Store a value in the context map.<T> void
Store a value in the context map.<T> void
store
(@NonNull CloudKeyHolder<T> keyHolder, @NonNull T value) Store a value in the context map.<T> void
Store a value in the context map.
-
Constructor Details
-
CommandContext
@Deprecated @API(status=DEPRECATED, since="1.3.0") public CommandContext(@NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry) Deprecated.Provide a command manager instead of a caption registryCreate a new command context instance- Parameters:
commandSender
- Sender of the commandcaptionRegistry
- Caption registry
-
CommandContext
@API(status=STABLE, since="1.3.0") public CommandContext(@NonNull C commandSender, @NonNull CommandManager<C> commandManager) Create a new command context instance- Parameters:
commandSender
- Sender of the commandcommandManager
- Command manager- Since:
- 1.3.0
-
CommandContext
@Deprecated @API(status=DEPRECATED, since="1.3.0") public CommandContext(boolean suggestions, @NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry) Deprecated.Provide a command manager instead of a caption registryCreate a new command context instance- Parameters:
suggestions
- Whether the context is created for command suggestionscommandSender
- Sender of the commandcaptionRegistry
- Caption registry
-
CommandContext
@API(status=STABLE, since="1.3.0") public CommandContext(boolean suggestions, @NonNull C commandSender, @NonNull CommandManager<C> commandManager) Create a new command context instance- Parameters:
suggestions
- Whether the context is created for command suggestionscommandSender
- Sender of the commandcommandManager
- Command manager- Since:
- 1.3.0
-
-
Method Details
-
formatMessage
public @NonNull String formatMessage(@NonNull Caption caption, @NonNull CaptionVariable... variables) Format a caption- Parameters:
caption
- Caption keyvariables
- Replacements- Returns:
- Formatted message
-
getSender
Get the sender that executed the command- Returns:
- Command sender
-
hasPermission
@API(status=STABLE, since="1.6.0") public boolean hasPermission(@NonNull CommandPermission permission) Check whether the sender that executed the command has a permission.- Parameters:
permission
- The permission- Returns:
- Command sender
- Since:
- 1.6.0
-
hasPermission
Check whether the sender that executed the command has a permission.- Parameters:
permission
- The permission- Returns:
- Command sender
- Since:
- 1.6.0
-
isSuggestions
public boolean isSuggestions()Check if this context was created for tab completion purposes- Returns:
true
if this context is requesting suggestions, elsefalse
-
store
Store a value in the context map. This will overwrite any existing value stored with the same key- Type Parameters:
T
- Value type- Parameters:
key
- Keyvalue
- Value
-
store
Store a value in the context map. This will overwrite any existing value stored with the same key- Type Parameters:
T
- Value type- Parameters:
key
- Keyvalue
- Value
-
store
Store a value in the context map. This will overwrite any existing value stored with the same key- Type Parameters:
T
- Value type- Parameters:
keyHolder
- Holder of the identifying keyvalue
- Value
-
store
@API(status=STABLE, since="1.4.0") public <T> void store(@NonNull CloudKeyHolder<T> keyHolder, @NonNull T value) Store a value in the context map. This will overwrite any existing value stored with the same key- Type Parameters:
T
- Value type- Parameters:
keyHolder
- Holder of the identifying keyvalue
- Value- Since:
- 1.4.0
-
set
Store or remove a value in the context map. This will overwrite any existing value stored with the same key.If the provided value is
null
, any current value stored for the provided key will be removed.- Type Parameters:
T
- Value type- Parameters:
key
- Keyvalue
- Value- Since:
- 1.3.0
-
set
Store or remove a value in the context map. This will overwrite any existing value stored with the same key.If the provided value is
null
, any current value stored for the provided key will be removed.- Type Parameters:
T
- Value type- Parameters:
key
- Keyvalue
- Value- Since:
- 1.4.0
-
contains
Check if the context has a value stored for a key- Parameters:
key
- Key- Returns:
- Whether the context has a value for the provided key
- Since:
- 1.3.0
-
contains
Check if the context has a value stored for a key- Parameters:
key
- Key- Returns:
- Whether the context has a value for the provided key
- Since:
- 1.4.0
-
asMap
Get the current state of this command context as a map of String to context value.- Returns:
- An immutable copy of this command context as a map
- Since:
- 1.3.0
-
getOptional
Get a value from its key. Will returnOptional.empty()
if no value is stored with the given key- Type Parameters:
T
- Value type- Parameters:
key
- Key- Returns:
- Value
-
getOptional
@API(status=STABLE, since="1.4.0") public <T> @NonNull Optional<T> getOptional(@NonNull CloudKey<T> key) Get a value from its key. Will returnOptional.empty()
if no value is stored with the given key- Type Parameters:
T
- Value type- Parameters:
key
- Key- Returns:
- Value
- Since:
- 1.4.0
-
getOptional
Get a value from its key. Will returnOptional.empty()
if no value is stored with the given key- Type Parameters:
T
- Value type- Parameters:
keyHolder
- Holder of the key- Returns:
- Value
-
getOptional
@API(status=STABLE, since="1.4.0") public <T> @NonNull Optional<T> getOptional(@NonNull CloudKeyHolder<T> keyHolder) Get a value from its key. Will returnOptional.empty()
if no value is stored with the given key- Type Parameters:
T
- Value type- Parameters:
keyHolder
- Holder of the key- Returns:
- Value
- Since:
- 1.4.0
-
remove
Remove a stored value from the context- Parameters:
key
- Key to remove
-
remove
Remove a stored value from the context- Parameters:
key
- Key to remove- Since:
- 1.4.0
-
get
Get a required argument from the context. This will thrown an exception if there's no value associated with the given key- Type Parameters:
T
- Argument type- Parameters:
key
- Argument key- Returns:
- Argument
- Throws:
NullPointerException
- If no such argument is stored
-
get
Get a required argument from the context. This will thrown an exception if there's no value associated with the given key- Type Parameters:
T
- Argument type- Parameters:
key
- Argument key- Returns:
- Argument
- Throws:
NullPointerException
- If no such argument is stored- Since:
- 1.4.0
-
get
Get a required argument from the context. This will thrown an exception if there's no value associated with the given argument- Type Parameters:
T
- Argument type- Parameters:
keyHolder
- Holder of the identifying key- Returns:
- Stored value
- Throws:
NullPointerException
- If no such value is stored
-
get
Get a required argument from the context. This will thrown an exception if there's no value associated with the given argument- Type Parameters:
T
- Argument type- Parameters:
keyHolder
- Holder of the identifying key- Returns:
- Stored value
- Throws:
NullPointerException
- If no such value is stored- Since:
- 1.4.0
-
getOrDefault
public <T> @Nullable T getOrDefault(@NonNull CommandArgument<C, T> argument, @Nullable T defaultValue) Get a value if it exists, else return the provided default value- Type Parameters:
T
- Argument type- Parameters:
argument
- ArgumentdefaultValue
- Default value- Returns:
- Stored value, or supplied default value
-
getOrDefault
Get a value if it exists, else return the provided default value- Type Parameters:
T
- Argument type- Parameters:
key
- Argument keydefaultValue
- Default value- Returns:
- Argument, or supplied default value
-
getOrDefault
@API(status=STABLE, since="1.4.0") public <T> @Nullable T getOrDefault(@NonNull CloudKey<T> key, @Nullable T defaultValue) Get a value if it exists, else return the provided default value- Type Parameters:
T
- Argument type- Parameters:
key
- Argument keydefaultValue
- Default value- Returns:
- Argument, or supplied default value
- Since:
- 1.4.0
-
getOrSupplyDefault
@API(status=STABLE, since="1.2.0") public <T> @Nullable T getOrSupplyDefault(@NonNull String key, @NonNull Supplier<@Nullable T> defaultSupplier) Get a value if it exists, else return the value supplied by the given supplier- Type Parameters:
T
- Argument type- Parameters:
key
- Argument keydefaultSupplier
- Supplier of default value- Returns:
- Argument, or supplied default value
- Since:
- 1.2.0
-
getOrSupplyDefault
@API(status=STABLE, since="1.4.0") public <T> @Nullable T getOrSupplyDefault(@NonNull CloudKey<T> key, @NonNull Supplier<@Nullable T> defaultSupplier) Get a value if it exists, else return the value supplied by the given supplier- Type Parameters:
T
- Argument type- Parameters:
key
- Argument keydefaultSupplier
- Supplier of default value- Returns:
- Argument, or supplied default value
- Since:
- 1.4.0
-
getRawInput
Get the raw input.- Returns:
- Raw input in token form
-
getRawInputJoined
Get the raw input as a joined string- Returns:
getRawInput()
joined with" "
as the delimiter- Since:
- 1.1.0
-
createTiming
Create an argument timing for a specific argument- Parameters:
argument
- Argument- Returns:
- Created timing instance
-
getArgumentTimings
public @NonNull Map<CommandArgument<@NonNull C,@NonNull ?>, getArgumentTimings()CommandContext.ArgumentTiming> Get an immutable view of the argument timings map- Returns:
- Argument timings
-
flags
Get the associatedFlagContext
instance- Returns:
- Flag context
-
getCurrentArgument
Get the argument that is currently being parsed for this command context. This value will be updated whenever the context is used to provide new suggestions or parse a new command argument- Returns:
- Currently parsing
CommandArgument
ornull
- Since:
- 1.2.0
-
setCurrentArgument
@API(status=STABLE, since="1.2.0") public void setCurrentArgument(@Nullable CommandArgument<C, ?> argument) Set the argument that is currently being parsed for this command context. This value should be updated whenever the context is used to provide new suggestions or parse a new command argument- Parameters:
argument
- Currently parsingCommandArgument
ornull
- Since:
- 1.2.0
-
inject
@API(status=STABLE, since="1.3.0") public <@NonNull T> @NonNull Optional<T> inject(@NonNull Class<@NonNull T> clazz) Attempt to retrieve a value that has been registered to the associated command manager'sParameterInjectorRegistry
- Type Parameters:
T
- Type to inject- Parameters:
clazz
- Class of type to inject- Returns:
- Optional that may contain the created value
- Since:
- 1.3.0
-