Skip to content

Configuring the options

Maarten Magits edited this page May 22, 2021 · 6 revisions

Recapitulating

If you followed the getting started tutorial, you should currently have something like this inside of your main class:

public class MyPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        Bukkit.getPluginCommand().setExecutor(new MmCommandHandler());
    }
}

Options

Before adding any subcommands, you may want to modify any options on the handler. You can do so by calling the option methods on the MmCommandHandler instance (preferably before setting the executor). An example of how to do this would be:

public class MyPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        MmCommandHandler handler = new MmCommandHandler(); // Getting the instance
        handler.useHelp();                                 // Setting the option
        Bukkit.getPluginCommand().setExecutor(handler);    // Registering the handler
    }
}

All option methods return the instance of the handler which makes it possible to chain the options.

useHelp()

Calling this option will enable the help subcommand. Performing the command /base test (remember that base is the plugincommand as registered in plugin.yml) will print a help menu to the player containing information about all commands registered to this handler. If an extra argument is given /base test <command>, then the help menu will be filtered to only show the information about that command and its subcommands.

generateTabCompletions()

Calling this option will configure the handler to generate TabCompletions for every subcommand on every level. These completions will be appended to the completions determined by the subcommand itself.

runLastAllowed()

Calling this option will allow partial running of commands. Let's say the following commands are registered to the handler: /base say <args> and /base say bold <args> (bold is a subcommand of say). Let's assume a player executes /base say bold Hello World but they do not have access to the bold subcommand. If partial running is allowed, /base say <bold Hello World> will be called (the subcommand bold will be converted to a normal argument). If partial running is not enabled, a permission error will be printed.

Help menu

The help menu is structured in a header/value structure.

help menu

You can determine what goes into the header part by calling helpHeader(@NotNull String header) on the handler. Color codes are supported by using the alternate chat code & and multiple lines can be set by splitting them with the newline character \n. The header has one placeholder %page% which houses the current page the player is on. The value part of the menu holds a certain amount of help entries. The number of help entries on one page can be set by calling amountOfCommandsPerHelpPage(int amount). Each help entry follows a specific format:

commandPrefix /<commandLabel> <subCommandNames> cmdArgsSpacer <arguments> argsDescSpacer <description>

The <> properties get dynamically filled in for each entry. The other properties can be set through the settings. In order of appearance in the format: helpCommandPrefix(@NotNull String commandPrefix), helpArgumentSpacer(@NotNull String argumentSpacer), helpDescriptionSpacer(@NotNull String descriptionSpacer)

Per command help menu

Whenever a player types the command /base help <subcommand>, the per command help menu will be shown. This is a non paginated menu that contains all help entries related to the provided subcommand.

The properties of the subcommand (name, aliases, type, permission and description) will be printed in between the help header and the help entries. helpPropertyPrefix(@NotNull String helpPropertyPrefix) and helpPropertyValueSpacer(@NotNull String helpPropertyValueSpacer) allow you to change how these properties are formatted.

Example

After configuring the options for the handler, your main class may look something like this:

public class MyPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        MmCommandHandler handler = new MmCommandHandler();
        handler.useHelp()
                .generateTabCompletions()
                .runLastAllowed()
                .helpHeader("&7This is the help &dheader&7!\n&7And this is the second line of it")
                .helpCommandPrefix(" &8")
                .helpArgumentSpacer(" &e")
                .helpDescriptionSpacer(" &7>> &a")
                .helpPropertyPrefix("&5")
                .helpPropertyValueSpacer("&7: &d");
        Bukkit.getPluginCommand().setExecutor(handler);
    }
}

Additional notes

  • None of the options are mandatory. They all have default settings.
  • The useHelp() option does not influence the help menu, it only determines whether or not the subcommand help is added to the handler to display this menu.
  • When using multiple lines, make sure to repeat the color code after the new line character. So &7Hello\nworld becomes &7Hello\n&7world

Clone this wiki locally