@Arg Optional
To define an optional argument in a command, you can choose from two different ways:
- Using the
@Arg
annotation with theOptional<T>
type. - Using the
@OptionalArg
annotation with a check fornull
.
Example
Add the Optional<T>
type to the parameter to define an optional argument:
java
@Command(name = "day")
public class DayCommand {
@Execute
void day(@Context Player sender, @Arg Optional<World> world) {
World selectedWorld = world.orElse(sender.getWorld());
selectedWorld.setTime(1000);
}
}
java
@Command(name = "day")
public class DayCommand {
@Execute
void day(@Context Player sender, @OptionalArg World world) {
if (world == null) {
world = sender.getWorld();
}
world.setTime(1000);
}
}
In this example we handle the optional argument World
, when the argument is not provided, we set the World
to the sender's world:
java
World selectedWorld = world.orElse(sender.getWorld());
java
if (world == null) {
world = sender.getWorld();
}