Creating a Simple Command

Discussion in 'Wiki Discussion' started by jflory7, May 22, 2015.

  1. jflory7

    Retired Benefactor

    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
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            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):
        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 for our example command would look like this.
    Code (Java):
        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

                // 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):
        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.
    : Your description
    : /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.
    #1 jflory7, May 22, 2015
    Last edited: May 27, 2015
    • Like Like x 3
    • Winner Winner x 3
    • Informative Informative x 2
    • Agree Agree x 1
  2. jflory7

    Retired Benefactor

    This page was originally made by @Janhektor and @LPkkjHD, so all credit goes to them for this article – I only made the discussion thread. :)

    • Like Like x 2
    • Friendly Friendly x 1
  3. Cool.
    I'll show how to create a command using the AT method.
  4. jflory7

    Retired Benefactor

    I just wiki-ified the page to make it easier to read.
  5. Thanks for this post dude.
  6. Well that's a new word 'wiki-ified' :p
  7. Dead-i

    Wiki Team

    I can't see any errors on that screenshot. If you're referring to the warning at "player", that is probably just Eclipse telling you that you have set "player" but never actually used it. The warning will vanish once you do something with the player (for example, give the player an item).
  8. Still getting an error on itemstack
  9. Good demo i hope to see more people learning java Nice
  10. Added a note on the plugin.yml part. Personally didn't realise that YAML didn't accept tab spacing.
  11. Don't bump 6 month old threads.
  12. nice tutorial.
  13. Thanks, nice tutorial.
  14. I am sure this will help pepole starting with plugins cause at my days no one did tutorials :D
  15. For somebody who isnt' very good at Java (me), this is a good "tutorial". It kinda helped me to understand more of the way the server would read the code.

Share This Page