Command Alias

Feb 2, 2018
Command Alias
  • Creating Aliases - Server Owners
    If you as a server owner wish to add an alias to a command provided by one of your plugins, you may use the commands.yml file in the root of your servers folder.

    For documentation on how to use this file, see https://bukkit.gamepedia.com/Commands.yml

    Creating Aliases - Plugin Developers
    During plugin development, you may decide that you want multiple player commands to point to the same code. There are many reasons for doing this, although the most common is eliminating the hassle of writing a long command and letting the user enter a shorter version.

    You can do this very easily in Bukkit using aliases in your plugin.yml.
    Here is an example of a plugin.yml of your current plugin.yml with a command defined:

    Code (Text):

    name: ExamplePlugin
    main: YourPlugin.ExamplePlugin
    version: 1.0
    author: YourName
    commands:
      examplecommand:
        description: Example
        usage: /<command>
     
    In order to make aliases for a command, add an aliases tag to the node of your command child. Here's an example of an aliases node:

    Code (Text):

    name: ExamplePlugin
    main: YourPlugin.ExamplePlugin
    version: 1.0
    author: YourName
    commands:
      examplecommand:
        description: Example
        usage: /<command>
        aliases: [alias1, alias2]
     
    If you were to write /alias1, the plugin will act as if you wrote /examplecommand.

    NOTE: Minecraft's tab completer also lists aliases, so don't put too many aliases if you don't want to flood the tab completer.

    Check what alias a player used
    While developing your plugin, you may want slightly different behaviours for each alias. For example, you might need to log which alias a player used to the console.

    Let's look at an onCommand() method in our example CommandExecutor.
    Code (Text):

    public class ExampleCommand implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            sender.sendMessage("Yay! You ran /examplecommand!");
            return true;
        }
    }
     
    We want to focus on are the variables command (of type Command) and label (of type String).

    If you know how to use onCommand(), you may recall that command is the command called by the player, and command.getName() returns the name of the command (registered in the plugin.yml). The important thing to remember is that command.getName() returns the same thing - the command name - no matter what alias the player used. command the player has used.

    However, label is different. The variable label returns whatever the player has written to execute the command. If a player wrote /alias1, label would be "alias1," if a player wrote /removemanhoodandfeedittothegoats label would be "removemanhoodandfeedittothegoats." Therefore, label is extremely useful when determining aliases.

    So, if we want our example command to say which alias the player used to run the command, we would do something like this:
    Code (Text):

    public class ExampleCommand implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            sender.sendMessage("Yay! You ran examplecommand!");
            if (!(command.getName().equalsIgnoreCase(label))) {
                sender.sendMessage("You also ran examplecommand with alias \"" + label + "!\"");
            }
            return true;
        }
    }
     
    In game, when we type /alias1 and press enter, we will get these messages in chat:
    Code (Text):

    Yay! You ran examplecommand!
    You also ran examplecommand with alias "alias1!"
     
    Any alias defined in the plugin.yml would also work in the above example.

    To see how to create a plugin.yml, please see our wiki page here.
  • Loading...
  • Loading...