Skip to content

Overwritting handler methods

Maarten Magits edited this page May 23, 2021 · 1 revision

Recapitulating

The handler has a lot of options that can be configured using the configuration methods as seen in Configuring the options. If you require more customization, you can start looking at overwriting some of the MmCommandHandler methods.

noArguments()

This method is called whenever the base command the handler is linked to gets executed. /base In this case, there are no subcommands to be matched. By default, this method just returns true. The return value of the method determines whether or not the help menu should be printed. It is suggested that you overwrite this method to display your plugin information or other important information. In that case, you probably want to return false. Printing the help menu is suggested when you treat executing just the base command as an error.

noSuchCommand()

This method is called whenever the first argument after the base command cannot be matched with the name or alias of a MmCommand registered to the handler. The return value of the method determines whether or not the help menu should be printed. The method allows you to display a fitting error message that fits the style of your plugin. The attempted command is that first string that did not get matched. All other arguments are discarded. /base <failedToMatch> <discardedArguments>

noValidType()

This method is called whenever the executor of a command does not match the type filter specified in the MmCommandSignature of the first subcommand. The return value specifies if the per-command help menu should be printed. The signature will contain the information about the first subcommand.

noPermission()

This method is called whenever the executor of a command does not have the permission node specified in the MmCommandSignature of the first subcommand. The return value specifies if the per-command help menu should be printed. The signature will contain the information about the first subcommand.

Help methods

- ! Note that these will have severe consequences ! -

You can overwrite the help methods but unexpected behavior may occur. Make sure you cannot accomplish your goal by changing options.

printHelp()

This method is called whenever the help menu should be printed. The default implementation works recursively checking the subcommands of each MmCommand with the getSubCommands() method and adding an entry into the help menu whenever the subcommand has

  • a description that is not an empty string
  • no subcommands

Pagination should be zero-based.

printPerCommandHelp()

This method is called whenever the help menu for a specific command should be printed. The default implementation will first print a key-value pair list of the properties in the MmCommandSignature followed by a recursive list of all subcommands adding entries following the same constraints as printHelp().

Example

(anonymous class)

MmCommandHandler handler = new MmCommandHandler() {
    @Override
    protected boolean noArguments(@NotNull CommandSender sender) {
        sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
            String.format(
                "&5Running &d%s &5v&d%s\n&5Made by &d%s&5.",
                getDescription().getName(),
                getDescription().getVersion(),
                getDescription().getAuthors()
            ))
        );
        return true;
    }

    @Override
    protected boolean noSuchCommand(@NotNull CommandSender sender, @NotNull String attemptedCommand) {
        sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
            String.format(
                "&7[&c&lERROR&7] > Command &4%s &7does not exist",
                attemptedCommand
            ))
        );
        return true;
    }

    @Override
    protected boolean noValidType(@NotNull CommandSender sender, @NotNull MmCommandSignature signature) {
        sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
            String.format(
                "&7[&c&lERROR&7] > You are not allowed to run &4%s&7? It is of type &4%s&7.",
                signature.name(),
                signature.type()
            ))
        );
        return false;
    }

    @Override
    protected boolean noPermission(@NotNull CommandSender sender, @NotNull MmCommandSignature signature) {
        sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
           String.format(
               "&7[&c&lERROR&7] > You are not allowed to run &4%s&7? Missing permission node &4%s&7.",
               signature.name(),
               signature.permission()
           ))
        );
        return false;
    }
};

No example provided of overwriting the help functions as it is discouraged

Clone this wiki locally