Creating a basic command and registering it

Dec 3, 2018
Creating a basic command and registering it
  • Creating a basic command:

    Create a class with some name and extend it with ACFCommand
    Code (Java):
    public class HelloCommand extends ACFCommand {
    Make one of the implemented 'super' method and the 'execute' method.

    Code (Java):
        public HelloCommand() {
            super("hello");
        }

        @Override
        public boolean execute(CommandSender sender, String s, String[] args) {
            return false;
        }
    Into the public <className>() method you could set everything you want (permission, permission message, aliases, usage message, description)

    Code (Text):
        public HelloCommand() {
            super("hello");
            setDescription("A hello command!");
            setUsage("hello");
            setAliases(Arrays.asList("hi", "hilol"));
        }
    Then into the execute method you do everything that the command should do, for this example it will send to the sender a message:

    Code (Java):
        @Override
        public boolean execute(CommandSender sender, String s, String[] args) {
            sender.sendMessage("Hello, world!");
            return true;
        }
    The full class now for me look like this:

    Code (Java):
    public class HelloCommand extends ACFCommand {

        public HelloCommand() {
            super("hello");
            setDescription("A hello command!");
            setUsage("hello");
            setAliases(Arrays.asList("hi", "hilol"));
        }

        @Override
        public boolean execute(CommandSender sender, String s, String[] args) {
            sender.sendMessage("Hello, world!");
            return true;
        }
    }
    Registering a command:

    There are various of methods, non-deprecated register methods require the plugin to be hooked (if your plugin is not hooked see this page on how to hook it).

    Here's how to:

    1st you should get the ACFCommandHandler class by doing this line into your code where you register commands:
    Code (Java):
            ACFCommandHandler handler = IvanACF.getRegisterHandler();
    Then to register a command and your plugin is properly hooked you do:
    Code (Java):
            handler.registerCommand(this, new HelloCommand());
    while HelloCommand is your command.

    Registering a command without your plugin hooked:
    Code (Java):
            handler.registerCommandNoHook(this, new HelloCommand());
    while HelloCommand is your command.

    If you have various of commands and you want cleaner register, you could do this:
    Code (Java):
            ACFCommandHandler handler = IvanACF.getRegisterHandler();
            ArrayList<ACFCommand> commands = new ArrayList<>();
            commands.add(new HelloCommand());
            commands.add(new InfoCommand());
            handler.registerCommands(this, commands);
    while HelloCommand and InfoCommand are your commands.
    Be aware that handler.registerCommands require the plugin to be hooked.

    And that's it. You can use your commands the same way as you registered them into your plugin.yml. Be aware that if you want "yourpluginname:yourcommand" to work you plugin should be hooked.
    Here's a pic on 1.13:
    [​IMG]
    [​IMG]
  • Loading...
  • Loading...