Create a subcommand

Since version 3.0 it is possible to create your own subcommands using the plugin API.

Summary

Admin subcommand

To add your own admin subcommand, you need to create a class that extends AdminCommandBasearrow-up-right. This class requires three methods to be overide: getName()arrow-up-right, getPermission()arrow-up-right and execute(CommandSender sender, String[] args)arrow-up-right. Here is an example of implementation:

public class MyAdminCommand extends AdminCommandBase {

    @Override
    public String getName() {
        return "hello";
    }

    @Override
    public String getPermission() {
        return "odailyquests.admin.hello";
    }

    @Override
    public void execute(CommandSender sender, String[] args) {
        if (args.length == 2 && args[1] != null) {
            final Player target = getTargetPlayer(sender, args[1]);
            if (target == null) return;

            sendHelloMessage(sender, target);
        } else {
            help(sender);
        }
    }

    private void sendHelloMessage(CommandSender sender, Player target) {
        target.sendMessage("Hello from " + sender.getName() + "!");
        sender.sendMessage("You greeted " + target.getName() + ".");
    }
}

In this case, the command will be /dqa hello <target>. It will require the odailyquests.admin.hello permission. You don't need to check that the user has permission, ODailyQuests takes care of that for you! It will use the value returned by the getPermission() method.

The AdminCommandBasearrow-up-right class itself inherits from a AdminMessagesarrow-up-right class, which contains utility methods to avoid duplication of code, which you can also use. They all return messages present in the messages.yml file.

The AdminCommandBasearrow-up-right class also provides two methods for retrieving a player and parse a quest identifier, getTargetPlayer(...)arrow-up-right and parseQuestIndex(...)arrow-up-right.

Player subcommand

To add your own player subcommand, you need to create a class that extends PlayerCommandBasearrow-up-right. This class requires three methods to be overide: getName()arrow-up-right, getPermission()arrow-up-right and execute(Player player, String[] args)arrow-up-right. Here is an example of implementation:

In this case, the command will be /dq hello. It will require the odailyquests.hello permission. You don't need to check that the user has permission, ODailyQuests takes care of that for you! It will use the value returned by the getPermission() method.

The PlayerCommandBasearrow-up-right class itself inherits from a PlayerMessagesarrow-up-right class, which contains utility methods to avoid duplication of code, which you can also use. They all return messages present in the messages.yml file.

Register your command

You can register your command in the onEnable() method of your main class, by retrieving the AdminCommandRegistryarrow-up-right or PlayerCommandRegistryarrow-up-right, dependeing of the type of command.

Tab completion

Depending on how you use it, you'll probably want to add auto completion to your commands. You can do this by overriding the onTabComplete(CommandSender sender, String[] args) arrow-up-rightmethod, which is included in both AdminCommandBasearrow-up-right and PlayerCommandBasearrow-up-right. By default, this method returns an empty list.

Note that if you want to return the players' nicknames, you'll have to return null instead of an empty list. This is a specific Bukkit behaviour.

Here is an example of tab completion for our MyAdminCommand class.

Last updated