Class CommonParameters
- java.lang.Object
-
- org.spongepowered.api.command.parameter.CommonParameters
-
public final class CommonParameters extends Object
Commonly usedparametersthat 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
ServerPlayerparameter 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
ValueParameters and, by extension,Parameter.Values 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
CommandContextand 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.Valuetype).
Given these two considerations, it generally makes sense to store one
Parameter.Valuefor a givenValueParameterandParameter.Keycombination. 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.Values with sensible key names such that plugin developers do not have to generate their own parameters and can also use these asParameter.Keys 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<Boolean>BOOLEANAParameter.Valuethat parses aBooleanwith the key name "true/false".static Parameter.Value<ServerLocation>LOCATION_ONLINE_ONLYAParameter.Valuethat parses a world and a position and stores it as aServerLocationunder the key "location".static Parameter.Value<String>MESSAGEAParameter.Valuethat parses the remainder of the string under the key "message".static Parameter.Value<ServerPlayer>PLAYERAParameter.Valuethat parses a player name or selector and stores the results under the key "player".static Parameter.Value<ServerPlayer>PLAYER_OPTIONALAParameter.Valuethat parses a player name or selector and stores the results under the key "player" if the input is valid.static Parameter.Value<Vector3d>POSITIONAParameter.Valuethat parses a position and stores it as aVector3dunder the key "position".static Parameter.Value<ServerWorld>WORLDAParameter.Valuethat parses a world id and stores it as aServerWorldunder the key "world".
-
-
-
Field Detail
-
WORLD
public static final Parameter.Value<ServerWorld> WORLD
AParameter.Valuethat parses a world id and stores it as aServerWorldunder the key "world".- See Also:
ResourceKeyedValueParameters.WORLD
-
BOOLEAN
public static final Parameter.Value<Boolean> BOOLEAN
AParameter.Valuethat parses aBooleanwith the key name "true/false".- See Also:
ResourceKeyedValueParameters.BOOLEAN
-
LOCATION_ONLINE_ONLY
public static final Parameter.Value<ServerLocation> LOCATION_ONLINE_ONLY
AParameter.Valuethat parses a world and a position and stores it as aServerLocationunder the key "location".- See Also:
ResourceKeyedValueParameters.LOCATION
-
MESSAGE
public static final Parameter.Value<String> MESSAGE
AParameter.Valuethat parses the remainder of the string under the key "message".
-
PLAYER
public static final Parameter.Value<ServerPlayer> PLAYER
AParameter.Valuethat 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.Valuethat 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.Valuethat parses a position and stores it as aVector3dunder the key "position".- See Also:
ResourceKeyedValueParameters.VECTOR3D
-
-