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.<T> T
computeIfAbsent
(@NonNull CloudKey<T> key, @NonNull Function<CloudKey<T>, T> defaultFunction) Get a value if it exists, else compute and store the value returned by the function and return it.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 extends @NonNull Object>
Tget
(@NonNull CommandArgument<C, T> keyHolder) Get a required argument from the context.<T extends @NonNull Object>
TGet a required argument from the context.<T extends @NonNull Object>
Tget
(@NonNull CloudKeyHolder<T> keyHolder) Get a required argument from the context.<T extends @NonNull Object>
TGet 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.getOptional
(@NonNull CommandArgument<C, T> keyHolder) Get a value from its key.getOptional
(@NonNull CloudKey<T> key) Get a value from its key.getOptional
(@NonNull CloudKeyHolder<T> keyHolder) Get a value from its key.getOptional
(@NonNull String key) Get a value from its key.<T> T
getOrDefault
(@NonNull CommandArgument<C, @NonNull T> argument, T defaultValue) Get a value if it exists, else return the provided default value<T> T
getOrDefault
(@NonNull CloudKey<@NonNull T> key, T defaultValue) Get a value if it exists, else return the provided default value<T> T
getOrDefault
(@NonNull String key, T defaultValue) Get a value if it exists, else return the provided default value<T> T
getOrSupplyDefault
(@NonNull CloudKey<@NonNull T> key, @NonNull Supplier<T> defaultSupplier) Get a value if it exists, else return the value supplied by the given supplier<T> T
getOrSupplyDefault
(@NonNull String key, @NonNull Supplier<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 extends @NonNull Object>
voidstore
(@NonNull CommandArgument<C, T> keyHolder, T value) Store a value in the context map.<T extends @NonNull Object>
voidStore a value in the context map.<T extends @NonNull Object>
voidstore
(@NonNull CloudKeyHolder<T> keyHolder, T value) Store a value in the context map.<T extends @NonNull Object>
voidStore 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 extends @NonNull Object> void store(@NonNull CloudKeyHolder<T> keyHolder, 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 extends @NonNull Object> @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
public <T extends @NonNull Object> @NonNull Optional<T> getOptional(@NonNull CommandArgument<C, 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
-
getOptional
@API(status=STABLE, since="1.4.0") public <T extends @NonNull Object> @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 throw 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
@API(status=STABLE, since="1.4.0") public <T extends @NonNull Object> T get(@NonNull CloudKey<T> key) Get a required argument from the context. This will throw 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 throw 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
@API(status=STABLE, since="1.4.0") public <T extends @NonNull Object> T get(@NonNull CloudKeyHolder<T> keyHolder) Get a required argument from the context. This will throw 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
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
- Value type- Parameters:
key
- Cloud keydefaultValue
- Default value- Returns:
- Argument, or supplied default value
-
getOrDefault
@API(status=STABLE, since="1.4.0") public <T> T getOrDefault(@NonNull CloudKey<@NonNull T> key, T defaultValue) Get a value if it exists, else return the provided default value- Type Parameters:
T
- Value type- Parameters:
key
- Cloud keydefaultValue
- Default value- Returns:
- Argument, or supplied default value
- Since:
- 1.4.0
-
getOrSupplyDefault
@API(status=STABLE, since="1.2.0") public <T> T getOrSupplyDefault(@NonNull String key, @NonNull Supplier<T> defaultSupplier) Get a value if it exists, else return the value supplied by the given supplier- Type Parameters:
T
- Value type- Parameters:
key
- Cloud 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> T getOrSupplyDefault(@NonNull CloudKey<@NonNull T> key, @NonNull Supplier<T> defaultSupplier) Get a value if it exists, else return the value supplied by the given supplier- Type Parameters:
T
- Value type- Parameters:
key
- Cloud keydefaultSupplier
- Supplier of default value- Returns:
- Argument, or supplied default value
- Since:
- 1.4.0
-
computeIfAbsent
@API(status=STABLE, since="1.8.0") public <T> T computeIfAbsent(@NonNull CloudKey<T> key, @NonNull Function<CloudKey<T>, T> defaultFunction) Get a value if it exists, else compute and store the value returned by the function and return it.- Type Parameters:
T
- Value type- Parameters:
key
- Cloud keydefaultFunction
- Default value function- Returns:
- present or computed value
- Since:
- 1.8.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
-