@Arg Optional
To define an optional argument in a command, you can choose from two different ways:
- Using the
@Argannotation with theOptional<T>type. - Using the
@OptionalArgannotation 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();
}
Norbert Dejlich