Class CommonParameters
- java.lang.Object
-
- org.spongepowered.api.command.parameter.CommonParameters
-
public final class CommonParameters extends java.lang.Object
Commonly usedparameters
that can be reused in multiple commands.The intent behind these parameters is to reduce the amount of boilerplate used when creating commands and retrieving elements. A general workflow for defining a command with a
ServerPlayer
parameter may look like this:final Parameter.Value<ServerPlayer> parameter = Parameter.player().setKey("player").build(); final Command.Parameterized builder = Command.builder() .parameter(parameter) .executor(context -> { context.sendMessage(Component.text(context.requireOne(parameter).getName())); return CommandResult.success(); }).build(); // registration happens here.
While this is a totally valid approach, there are two particular considerations that can be made:
- Sponge provided
ValueParameter
s and, by extension,Parameter.Value
s are stateless and so can be reused by multiple commands, or indeed, multiple times within the same command; and - Due to the type safety provided by the
CommandContext
and the requirement of retrieving parsed arguments by theParameter.Key
(or the actualParameter.Value
, which implements this key, plugin developers will either need to keep a reference to the parameter value, its key, or rebuild the key as required (paying close attention to theParameter.Value
type).
Given these two considerations, it generally makes sense to store one
Parameter.Value
for a givenValueParameter
andParameter.Key
combination. Further, it is clear that plugins will tend to use similar, if not the same, parameter/key combinations.The values held by this class are commonly used
Parameter.Value
s with sensible key names such that plugin developers do not have to generate their own parameters and can also use these asParameter.Key
s when obtaining results from theCommandContext
. The example above would then become:final Command.Parameterized builder = Command.builder() .parameter(CommonParameters.PLAYER) .executor(context -> { context.sendMessage(Component.text(context.requireOne(CommonParameters.PLAYER).getName())); return CommandResult.success(); }).build(); // registration happens here.
reducing object creation and slightly reducing repetition amongst commands that use the same parameters.
- Sponge provided
-
-
Field Summary
Fields Modifier and Type Field Description static Parameter.Value<java.lang.Boolean>
BOOLEAN
AParameter.Value
that parses aBoolean
with the key name "true/false".static Parameter.Value<ServerLocation>
LOCATION_ONLINE_ONLY
AParameter.Value
that parses a world and a position and stores it as aServerLocation
under the key "location".static Parameter.Value<java.lang.String>
MESSAGE
AParameter.Value
that parses the remainder of the string under the key "message".static Parameter.Value<ServerPlayer>
PLAYER
AParameter.Value
that parses a player name or selector and stores the results under the key "player".static Parameter.Value<ServerPlayer>
PLAYER_OPTIONAL
AParameter.Value
that parses a player name or selector and stores the results under the key "player" if the input is valid.static Parameter.Value<Vector3d>
POSITION
AParameter.Value
that parses a position and stores it as aVector3d
under the key "position".static Parameter.Value<ServerWorld>
WORLD
AParameter.Value
that parses a world id and stores it as aServerWorld
under the key "world".
-
-
-
Field Detail
-
WORLD
public static final Parameter.Value<ServerWorld> WORLD
AParameter.Value
that parses a world id and stores it as aServerWorld
under the key "world".- See Also:
ResourceKeyedValueParameters.WORLD
-
BOOLEAN
public static final Parameter.Value<java.lang.Boolean> BOOLEAN
AParameter.Value
that parses aBoolean
with the key name "true/false".- See Also:
ResourceKeyedValueParameters.BOOLEAN
-
LOCATION_ONLINE_ONLY
public static final Parameter.Value<ServerLocation> LOCATION_ONLINE_ONLY
AParameter.Value
that parses a world and a position and stores it as aServerLocation
under the key "location".- See Also:
ResourceKeyedValueParameters.LOCATION
-
MESSAGE
public static final Parameter.Value<java.lang.String> MESSAGE
AParameter.Value
that parses the remainder of the string under the key "message".
-
PLAYER
public static final Parameter.Value<ServerPlayer> PLAYER
AParameter.Value
that parses a player name or selector and stores the results under the key "player".- See Also:
ResourceKeyedValueParameters.PLAYER
-
PLAYER_OPTIONAL
public static final Parameter.Value<ServerPlayer> PLAYER_OPTIONAL
AParameter.Value
that parses a player name or selector and stores the results under the key "player" if the input is valid. Otherwise, this parameter is skipped.- See Also:
ResourceKeyedValueParameters.PLAYER
-
POSITION
public static final Parameter.Value<Vector3d> POSITION
AParameter.Value
that parses a position and stores it as aVector3d
under the key "position".- See Also:
ResourceKeyedValueParameters.VECTOR3D
-
-