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

SubCommands

Julian edited this page Aug 26, 2017 · 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,
            (pl, args) -> pl.getName().equalsIgnoreCase("tester") // Custom condition,
            "i" // Aliases
    ),
    INFO2(
            "info2", // argument
            "myplugin info2", // command usage
            "Just to say hello 2.", // A bit useless, I know"
            new InfoCommand() // Argument instance
    );
    
    private final String arg, usage;
    private final Enum info;
    private final SubCommandExecutor instance;
    private final Condition condition;
    private final String[] aliases;

    Arguments(String argument, String usage, Enum desc, SubCommandExecutor instance) {
        this(argument, usage, desc, instance, null, new String[0]);
    }

    Arguments(String argument, String usage, Enum desc, SubCommandExecutor instance, 
            String... aliases) {
        this(argument, usage, desc, instance, null, aliases);
    }

    Arguments(String argument, String usage, Enum desc, SubCommandExecutor instance, 
            Condition condition) {
        this(argument, usage, desc, instance, condition, new String[0]);
    }

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

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

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

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

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

    @Override
    public CustomCondition getCustomCondition() {
        return condition;
    }

    @Override
    public String[] getAliases() {
        return aliases;
    }
}

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