How to use Optional<Text>

Creating a command and I cannot get the description and help text working.

private final Optional<Text> desc = Optional.of(Texts.of("Some description here."));

This results in:
Type mismatch: cannot convert from Optional<Text.Literal> to Optional

Any ideas?

2 Likes

I think you are using the wrong import of Optional. The following one should be used:
com.google.common.base.Optional

1 Like

@Cybermaxke that is what I am importing. :confused:

You’ll have to explicitly cast it to Text. There was an example of this in the cookbook:

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

Then update the ones in the docs :stuck_out_tongue_winking_eye: