Skip to content
This repository was archived by the owner on Nov 10, 2021. It is now read-only.

SubCommands

Julian edited this page Nov 14, 2016 · 10 revisions

You have to create the subcommands classes implementing SubCommandExecutor. For example, create something like this...

public class InfoCommand implements SubCommandExecutor {
    @Override
    public boolean execute(CommandSender cs, String[] args) {
        cs.sendMessage("Hello there!");
        return true;
    }
}

When you have all the subcommands classes created, you have to create another class, which implements SubCommandsInterface, to simplify the subcommands management. For the previous example, create something like this:

public enum Arguments implements SubCommandsInterface {
    INFO(
            "info", // argument
            "myplugin info", // command usage
            "Just to say hello.", // A bit useless, I know"
            new InfoCommand() // Argument instance
    ),
    
    private final String arg, usage;
    private final Enum info;
    private final SubCommandExecutor instance;

    Arguments(String argument, String usage, Enum description, SubCommandExecutor i) {
        this.arg = argument;
        this.usage = usage;
        this.info = description;
        this.instance = i;
    }

    @Override
    public String getArgument() {
        return arg;
    }

    @Override
    public String getUsage() {
        return usage;
    }

    @Override
    public Enum getDescription() {
        return info;
    }

    @Override
    public SubCommandExecutor getInstance() {
        return instance;
    }    
}

And that's all. You just have to register the subcommands (For example, in the main class):

// First, register the command...
SubCommandsRegisterer command = new SubCommandsRegisterer("mainCommand", "mc");
// and finally, it will register all the subcommands that you have created.
command.register(Arguments.class);

Clone this wiki locally