Skip to content

Custom Help Format

Marten edited this page Nov 4, 2021 · 6 revisions

One of the best features of this simple library is that it comes with its own command help. The current format can easily be changed by a developer like you. First, we need to understand how it works though.

The SimpleHelpFormatter

Each root command (the one that is at the start /test) has its own SimpleHelpFormatter. This formatter is used by its children too. This formatted can be changed when creating the class. One can simply change only the plugin name or implement a custom formatted.

If you want the children to have a different format you can do so. Simply set the formatter in the constructor of these commands. To create your own formatted please look at the section down below.

Change only the header

The SimpleHelpFormatter that comes with SimpleCommands allows you to only change the header name. You can use the following code to do so:

public class SimpleTestCommand extends SimpleCommand {

    public SimpleTestCommand() {
        super("test",  false);

        addCommand(new SubCommandFoo());

        SimpleHelpFormatter helpFormatter = (SimpleHelpFormatter) getHelpFormatter();
        helpFormatter.setHeader("Your plugin name here.");
    }
}

Custom formatter

In order to change the whole format, you need to implement your own instance of the ISimpleCommandFormatter. This formatter can then be set when creating the commands.

public class CustomHelpFormatter implements ISimpleHelpFormatter {

    public SimpleHelpFormatter() {

    }

    /**
     * Sends the help to the player.
     * @param sender The sender that should receive the help page.
     * @param subCommands The commands that are available for this sender.
     */
    @Override
    public void sendHelp(CommandSender sender, List<SimpleCommand> subCommands) {
        send(sender, "&7===============[ PLUGIN &7]===============");
        send(sender, " ");

        for(SimpleCommand cmd : subCommands) {
            if(cmd.hasDescription()) {
                send(sender, String.format("&2/&a%s &7- &e%s", cmd.getFullName(), cmd.getDescription()));
            } else {
                send(sender, String.format("&2/&a%s", cmd.getFullName()));
            }

        }

        send(sender, " ");
    }

    private void send(CommandSender sender, String message) {
        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
    }
}

And in the command (most often the root command) you need to set the formatted. The children will inherit this formatted by default although they can specify their own.

public class SimpleTestCommand extends SimpleCommand {

    public SimpleTestCommand() {
        super("test",  false);

        addCommand(new SubCommandFoo());

        setHelpFormatter(new CustomHelpFormatter());
    }
}
Clone this wiki locally