Injecting sub commands

Since 1.0.0

This is probably one of my favourite features of the MCCommand class. With it, you can add a sub command to an existing MCCommand at any argument position. Sub commands don't use an special class, so they can also be registered as an standalone command or even have sub commands themselves, so sub commands on sub commands. Let's see how that works.

First, let's create the actual sub command, this will be the /clan info sub command.

import net.codersky.mcutils.general.MCCommand;
import net.codersky.clans.ClanPlugin;

public class ClanInfo extends MCPlugin<ClanPlugin> {

    public ClanCommand(ClanPlugin plugin) {
        // The sub command name will be "info", but yes, aliases are supported.
        super(plugin, "info");
        // Making this command player only isn't required as ClanCommand
        // will take preference and prevent the console from reaching this point.
    }

    @Override
    public boolean onCommand(CommandSender sender, String[] args) {
        // Example code to send the info of a clan to the sender.
        // The sender can be safely casted to player because of class restrictions.
        Player receiver = (Player) sender;
        // We get the clan, which may be null, but let's ignore that for now.
        Clan clan = getPlugin().getClanManager().getClan(receiver);
        clan.sendInfo(sender);
    }

    @Override
    public List<String> onTab(CommandSender sender, String[] args) {
        // We just return null as no suggestions will be made by this
        // specific command at this point to the sender.
        return null;
    }
}

Now let's inject it to the /clan command itself, the info sub command should be injected on the position 0 of /clan as we will use it as /clan info, right before the main command name.

// Let's skip directly to the MainClanCommand constructor...
public MainClanCommand(ClanPlugin plugin) {
    // Code from the previous example should go here.
    // Note that you could add multiple commands to the same positions.
    inject(0, new ClanInfo(this), ...);
}

Automatizations

One thing to note is that once the sub command is injected, MCUtils will automatically deduce that when a sender tries to tab-complete (onTab) the main command, in this case MainClanCommand, it should suggest the name and aliases of the injected commands. So if a user types on chat /clan, "info" will be suggested, so you don't have to do that manually, sub-commands that return false on their hasAccess method won't be included.

Tab suggestions are also filtered by MCUtils, so if the suggestion list is ["info", "create", "kick", "invite"] and the user types "/clan i" and then presses tab, the suggestions will only be "info" and "invite", as those are the only suggestions starting with "i".

Lastly, the most obvious one, you don't need to handle the logic of what sub command should be called when the user executes a command, this is done by MCUtils and is the whole point of the sub command system on the first place.

Last updated