Skip to content

Commands

Asintoto edited this page Jul 19, 2024 · 7 revisions

Basic Command

Here you will find how to simply create Commands using Basic


You can create a custom command as following:

public class YourCommand extends BasicCommand {

    public YourCommand () {
        super("command_name"); // Specify here the name of the command (Ex. if you want to create a /hello command, type "hello")
    }

    @Override
    public void onCommand() {

        // Command execution logic
    }

    @Override
    public List<String> onTabComplete() {
        
       // Tab Completion logic
    }
}

There are some attributes you can use:

  • sender: The command sender (Player or Console)
  • args: the arguments of the commands

There are some methods you can use:

  • isPlayer(): return "true" if the sender is a player
  • getPlayer(): return the sender casted to a player (if the sender is actually a player)
  • printUsage(): print the command usage (You can specify the command usage using setUsage(String))
  • getLabel(): return the command name (or label)
  • sendMessage(String): send a message to the sender
  • sendMessage(Player, String): send a message to a given player
  • getUsage(): return the command usage string
  • setUsage(String): set the command usage

Note: You can return NO_COMPLETION to return and empty completion when tab compliting, or you can return PLAYER_LIST to return the player list

Annotated Command

Here you will find how to simply create a one-parameter command


You can create this type of custom command as following:

public class YourCommand extends AnnotatedCommand {

    public YourCommand () {
        super("command_name"); // Specify here the name of the command (Ex. if you want to create a /hello command, type "hello")
    }

    @Parameter(value = "your_parameter_2", requiredPlayer = true|false)
    public void onParam1() {
        sendMessage("&bParameter 1")
    }

    @Parameter(value = "your_parameter_2", requiredPlayer = true|false)
    @RequiredPermission(permission = "yourplugin.command.test.param2")
    public void onParam2() {
        sendMessage("&bParameter 2")
    }
}

Simply specify the parameter you want in the @Parameter annotation and if the sender must be a player to execute that subcommand.

You can also specify a permission for that parameter by adding the @RequiredPermission(permission = "your_permission") annotation.

If you want to change the default no permission message, simple override the method printNoPermission(String permission)

Basic will handle tab completion and usage message generation (this can be modified with setUsage(String)).

The method corrisponding to the given parameter will be automatically executed without the need of using onCommand().

You can change the not-a-player message by using setNotAPlayerMessage(String)

Important Steps:

Make sure to register the command in you plugin.yml file. Also register the command in your onPluginEnable() or onEnable() method by using the built-in method registerCommand(new YourCommand()). This method can also be used to register Non-Basic commands (the one you create implementing CommandExecutor)

registerCommand(new BasicTestCommand());  // Register a Basic based command
registerCommand("external", new ExternalCommand(this));   // Register a Non Basic based command (the command name is required in this case)

Note: You can also use the @AutoRegister annotation to automatically register the command (you still need to declare it in you plugin.yml)

@AutoRegister
public class MyCommand extends BasicCommand {
   ...
}

Note: This only works for BasicCommand and AnnotatedCommand, Spigot's CommandExecutor will NOT work

Note: Make sure that your command class has ONLY the No Args Constructor (ex. public MyCommand () {} )

Clone this wiki locally