Creating a Simple Command

Jul 14, 2016
Creating a Simple Command
  • Creating a Simple Command

    Create a simple command to give players some items




    Designing the Command(top)

    Firstly, you should have a name in mind already for this command. In this snippet, we will be using the example command of "/kit". However, this can be substituted with whatever command you choose to use in your own plugin.

    It is recommended and more organized to create a new class for every command. Your class must implement the interface CommandExecutor. The class file may look similar to this:
    Code (Java):
    public class CommandKit implements CommandExecutor {

        // This method is called, when somebody uses our command
        @Override
        public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
            return false;
        }

    }
    Rename the parameters and fill the method body with some code. Here is the meaning of each parameter:
    • CommandSender represents whatever is sending the command. This could be a Player, ConsoleCommandSender, or BlockCommandSender (a command block)
    • Command represents what the command being called is. This will almost always be known ahead of time.
    • Label represents the exact first word of the command (excluding arguments) that was entered by the sender
    • Args is the remainder of the command statement (excluding the label) split up by spaces and put into an array.
    Before giving our item(s) to a player for our example command, we need a player-object. If the command sender is a player, we can cast it. In this case, the cast will give us access to the player-object.
    Note: The CommandSender object passed to us in the onCommand method can sometimes be used without casting to a player first. If all you're wanting to do is send a message, checking if it's a player or not is just needlessly adding clutter to your code and making your command slightly less usable.
    Code (Java):
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                // Here we need to give items to our player
            }

            // If the player (or console) uses our command correct, we can return true
            return true;
        }
    Next, we give items to our player. In this example, we will use a diamond and twenty bricks. An item is represented by an ItemStack. So let's create a new ItemStack and the set the amount of them. Finally, we will give it to our player.

    The code might for our example command would look like this.
    Code (Java):
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;

                // Create a new ItemStack (type: diamond)
                ItemStack diamond = new ItemStack(Material.DIAMOND);

                // Create a new ItemStack (type: brick)
                ItemStack bricks = new ItemStack(Material.BRICK);

                // Set the amount of the ItemStack
                bricks.setAmount(20);

                // Give the player our items (comma-seperated list of all ItemStack)
                player.getInventory().addItem(bricks, diamond);
            }

            // If the player (or console) uses our command correct, we can return true
            return true;
        }
    Note: It's also possible to set the amount directly by creating a new ItemStack. This option is easier and shorter to use.
    Code (Java):
    ItemStack bricks = new ItemStack(Material.BRICK, 20);

    Registering the Command(top)

    Next, we register our command. To do this, go to your onEnable() method in the main class. We need to only add one line.
    Code (Java):
        @Override
        public void onEnable() {
            // Register our command "kit" (set an instance of your command class as executor)
            this.getCommand("kit").setExecutor(new CommandKit());
        }

    Add to plugin.yml(top)

    The final step is adding our command to the plugin.yml. Open it and add the lines similar to our example and test your plugin.
    Code (YAML):
    # Replace "kit" with the name of your command.
    commands
    :
      kit
    :
        description
    : Your description
        usage
    : /kit
    Some things to keep in mind:
    • The string that you use when registering the command must be the same as the one used in your plugin.yml
    • You can initialize your CommandExecutor instance ahead of time with constructor arguments such as your main plugin class, if necessary.
    • In your onCommand method, return false if you want the CommandSender to recieve the message dictating correct usage of your command (as defined in the plugin.yml). Return true to stop execution silently.
    • It is good practice to send an error when an invalid CommandSender attempts to use a command (such as when the console would use the /kit command).
    And you are now finished! We hope you learned some basics about creating commands.
  • Loading...
  • Loading...