Class CommonParameters
parameters
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 given ValueParameter
and
Parameter.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 as Parameter.Key
s when
obtaining results from the CommandContext
. 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.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Parameter.Value
<Boolean> AParameter.Value
that parses aBoolean
with the key name "true/false".static final Parameter.Value
<ServerLocation> AParameter.Value
that parses a world and a position and stores it as aServerLocation
under the key "location".static final Parameter.Value
<String> AParameter.Value
that parses the remainder of the string under the key "message".static final Parameter.Value
<ServerPlayer> AParameter.Value
that parses a player name or selector and stores the results under the key "player".static final Parameter.Value
<ServerPlayer> AParameter.Value
that parses a player name or selector and stores the results under the key "player" if the input is valid.static final Parameter.Value
<Vector3d> AParameter.Value
that parses a position and stores it as aVector3d
under the key "position".static final Parameter.Value
<ServerWorld> AParameter.Value
that parses a world id and stores it as aServerWorld
under the key "world". -
Method Summary
-
Field Details
-
WORLD
AParameter.Value
that parses a world id and stores it as aServerWorld
under the key "world".- See Also:
-
BOOLEAN
AParameter.Value
that parses aBoolean
with the key name "true/false".- See Also:
-
LOCATION_ONLINE_ONLY
AParameter.Value
that parses a world and a position and stores it as aServerLocation
under the key "location".- See Also:
-
MESSAGE
AParameter.Value
that parses the remainder of the string under the key "message".- See Also:
-
PLAYER
AParameter.Value
that parses a player name or selector and stores the results under the key "player".- See Also:
-
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:
-
POSITION
AParameter.Value
that parses a position and stores it as aVector3d
under the key "position".- See Also:
-