Skip to content

Hey there! If you like my work, you can support me by sponsoring me on GitHub. Thanks! ❤

Sponsor me
on GitHub

@Arg Optional

To define an optional argument in a command, you can choose from two different ways:

  1. Using the @Arg annotation with the Optional<T> type.
  2. Using the @OptionalArg annotation with a check for null.

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();
}

Made by LiteDevelopers and Contributors with ❤️