Interface CommandContext
- All Superinterfaces:
Contextual
,Subject
,SubjectProxy
- All Known Subinterfaces:
CommandContext.Builder
The
CommandContext
contains the parsed arguments for a command, and
any other information that might be important when executing a command.
For information about the cause of the command, the CommandCause
is available (see cause()
. Some popular tasks that operate
on the CommandCause
are also directly available on this context,
namely permission checks (via SubjectProxy
) and sending a message to
the CommandCause
's Audience
(via sendMessage(net.kyori.adventure.text.Component)
).
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
A builder for creating this context. -
Method Summary
Modifier and TypeMethodDescription<T> Collection
<? extends T> all
(Parameter.Key<T> key) Gets all values for the given argument.<T> Collection
<? extends T> all
(Parameter.Value<T> parameter) Gets all values for the given argument.cause()
Gets theCommandCause
associated with this context.Gets theCommand.Parameterized
that is being executed, if it exists.int
flagInvocationCount
(String flagKey) Returns how many times a given flag was invoked for this command.int
flagInvocationCount
(Flag flag) Returns how many times a givenFlag
was invoked for this command.boolean
hasAny
(Parameter.Key<?> key) Returns whether this context has any value for the given argument key.boolean
hasAny
(Parameter.Value<?> parameter) Returns whether this context has any value for the given argument key.boolean
Gets if a flag with given alias was specified at least once.boolean
Gets if the given flag was specified once.<T> Optional
<T> one
(Parameter.Key<T> key) Gets the value for the given key if the key has only one value.<T> Optional
<T> one
(Parameter.Value<T> parameter) Gets the value for the given key if the key has only one value.<T> T
requireOne
(Parameter.Key<T> key) Gets the value for the given key if the key has only one value, otherwise, throws aNoSuchElementException
.<T> T
requireOne
(Parameter.Value<T> parameter) Gets the value for the given key if the key has only one value, otherwise, throws aNoSuchElementException
.void
sendMessage
(Identified source, Component message) Sends a message viaCommandCause.audience()
void
sendMessage
(Identity source, Component message) Sends a message viaCommandCause.audience()
void
sendMessage
(Component message) Sends a system message viaCommandCause.audience()
Methods inherited from interface org.spongepowered.api.service.permission.SubjectProxy
associatedObject, asSubjectReference, containingCollection, contextCause, friendlyIdentifier, hasPermission, hasPermission, hasPermission, identifier, isChildOf, isChildOf, isChildOf, isSubjectDataPersisted, option, option, option, parents, parents, parents, permissionValue, permissionValue, permissionValue, subject, subjectData, transientSubjectData
-
Method Details
-
executedCommand
Optional<Command.Parameterized> executedCommand()Gets theCommand.Parameterized
that is being executed, if it exists.- Returns:
- The
Command.Parameterized
.
-
cause
CommandCause cause()Gets theCommandCause
associated with this context.- Returns:
- The
CommandCause
-
hasFlag
Gets if a flag with given alias was specified at least once.If the flag has multiple aliases, (for example,
-f
and--flag
, passingf
orflag
to this method will return the same result, regardless of the alias specified by the user.- Parameters:
flagAlias
- The flag's alias (without a prefixed dash)- Returns:
- If the flag was specified
-
hasFlag
Gets if the given flag was specified once.- Parameters:
flag
- TheFlag
- Returns:
- If the flag was specified
-
flagInvocationCount
Returns how many times a given flag was invoked for this command.If the flag has multiple aliases, (for example,
-f
and--flag
, passingf
orflag
to this method will return the same result, regardless of the alias specified by the user.- Parameters:
flagKey
- The flag's alias (without a prefixed dash)- Returns:
- The number of times the flag was specified
-
flagInvocationCount
Returns how many times a givenFlag
was invoked for this command.- Parameters:
flag
- TheFlag
- Returns:
- The number of times the flag was specified
-
hasAny
Returns whether this context has any value for the given argument key.- Parameters:
parameter
- theParameter
associated with the argument- Returns:
- whether there are any values present
-
hasAny
Returns whether this context has any value for the given argument key.- Parameters:
key
- The key to look up- Returns:
- whether there are any values present
-
one
Gets the value for the given key if the key has only one value.- Type Parameters:
T
- the expected type of the argument- Parameters:
parameter
- theParameter
associated with the argument- Returns:
- the argument
- Throws:
IllegalArgumentException
- if more than one value for the key was found
-
one
Gets the value for the given key if the key has only one value.- Type Parameters:
T
- the expected type of the argument- Parameters:
key
- the key to get- Returns:
- the argument
- Throws:
IllegalArgumentException
- if more than one value for the key was found
-
requireOne
<T> T requireOne(Parameter.Value<T> parameter) throws NoSuchElementException, IllegalArgumentException Gets the value for the given key if the key has only one value, otherwise, throws aNoSuchElementException
.- Type Parameters:
T
- the expected type of the argument- Parameters:
parameter
- theParameter
associated with the argument- Returns:
- the argument
- Throws:
NoSuchElementException
- if no value for the key was foundIllegalArgumentException
- if more than one value for the key was found
-
requireOne
Gets the value for the given key if the key has only one value, otherwise, throws aNoSuchElementException
.- Type Parameters:
T
- the expected type of the argument- Parameters:
key
- the key to get- Returns:
- the argument
- Throws:
NoSuchElementException
- if no value for the key was foundIllegalArgumentException
- if more than one value for the key was found
-
all
Gets all values for the given argument. May return an empty list if no values are present.- Type Parameters:
T
- the expected type of the argument- Parameters:
parameter
- theParameter
associated with the argument- Returns:
- the argument
-
all
Gets all values for the given argument. May return an empty list if no values are present.- Type Parameters:
T
- the type of value to get- Parameters:
key
- The key to get values for- Returns:
- the collection of all values
-
sendMessage
Sends a system message viaCommandCause.audience()
- Parameters:
message
- The message to send- See Also:
-
sendMessage
Sends a message viaCommandCause.audience()
- Parameters:
source
- TheIdentified
that is the sender of the messagemessage
- The message to send- See Also:
-
sendMessage
Sends a message viaCommandCause.audience()
- Parameters:
source
- TheIdentity
of the sender of the messagemessage
- The message to send- See Also:
-