Child Commands

The CommandSpec builder supports hierarchical command structures like this:

  • /mail (parent command)

    • /mail send (child command)

    • /mail read (child command)

Every child command is a separate CommandSpec and can be created in the same way a regular command is.

import org.spongepowered.api.text.Text;
import org.spongepowered.api.command.spec.CommandSpec;

// /mail read
CommandSpec readCmd = CommandSpec.builder()
    .permission("myplugin.mail.read")
    .description(Text.of("Read your inbox"))
    .executor(...)
    .build();

// /mail send
CommandSpec sendCmd = CommandSpec.builder()
    .permission("myplugin.mail.send")
    .description(Text.of("Send a mail"))
    .arguments(...)
    .executor(...)
    .build();

Instead of registering them to the command service, child commands are registered on their parent command using the CommandSpec.Builder#child(CommandCallable, String…) method. They are registered with a list of aliases. The first alias supplied is the primary one and will appear in the usage message.

import org.spongepowered.api.Sponge;

CommandSpec mailCommandSpec = CommandSpec.builder()
    .permission("myplugin.mail")
    .description(Text.of("Send and receive mails"))
    .child(readCmd, "read", "r", "inbox")
    .child(sendCmd, "send", "s", "write")
    .build();

Sponge.getCommandManager().register(plugin, mailCommandSpec, "mail", "email");

Nota

If a CommandExecutor was set for the parent command, it is used as a fallback if the arguments do not match one of the child command aliases. Setting an executor is not required.