How to use Optional<Text>

I recommend you to use the new commands API. The example in the docs is outdated.

It works like this:

CommandSpec mailCommand = CommandSpec
.builder()
.setDescription(Texts.of("Send and receive mails"))
.setExtendedDescription(Texts.of("Mails will only be visible on this server"))
.setExecutor(new MyCommandExecutor()) // <-- command logic
.setArguments(GenericArguments.seq( // <-- command arguments
    GenericArguments.player(Texts.of("player"), game),
    GenericArguments.remainingJoinedStrings(Texts.of("msg"))))
.build();

// Register the mail command
game.getCommandDispatcher().register(this, mailCommand, "mail");

The nice thing is that the arguments are already parsed:

public class MyCommandExecutor implements CommandExecutor {
    @Override
    public CommandResult execute(CommandSource src, CommandContext args)
            throws CommandException {
        Player recipient = (Player) args.getOne("player").get();
        String mailContent = (String) args.getOne("msg").get();
        ...
    }
}
3 Likes