Getting Started - LilBroCodes/commander GitHub Wiki

Getting Started with Commander

This guide will help you get started with the Commander library for your Bukkit/Spigot/Paper Minecraft plugin.

Installation

Gradle

Add the following to your build.gradle file:

repositories {
    // Your other repositories
    maven { url 'https://jitpack.io' }
}

dependencies {
    // Your other dependencies
    implementation 'com.github.LilBroCodes:Commander:latest-version'
}

Maven

Add the following to your pom.xml file:

<repositories>
    <!-- Your other repositories -->
    <repository>
        <id>jitpack</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <!-- Your other dependencies -->
    <dependency>
        <groupId>com.github.LilBroCodes</groupId>
        <artifactId>Commander</artifactId>
        <version>latest-version</version>
    </dependency>
</dependencies>

Basic Setup

1. Register your command in plugin.yml

First, make sure to register your command in your plugin's plugin.yml file:

name: YourPlugin
version: 1.0.0
main: com.example.yourplugin.YourPlugin
api-version: 1.13

commands:
  yourcommand:
    description: Your command description
    usage: /yourcommand
    aliases: [yc, ycmd]

2. Create a simple command

Here's a basic example of how to create and register a command using Commander:

import org.bukkit.plugin.java.JavaPlugin;
import org.lilbrocodes.commander.api.CommanderCommand;
import org.lilbrocodes.commander.api.executor.CommandGroupNode;
import org.lilbrocodes.commander.api.executor.CommandActionNode;
import org.lilbrocodes.commander.api.argument.TypedParameter;
import org.lilbrocodes.commander.api.argument.ParameterType;

import java.util.List;

public class YourPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        // Create the root command node
        CommandGroupNode rootNode = new CommandGroupNode(
            "yourcommand",
            "Your command description",
            "YourPlugin"
        );
        
        // Create a subcommand with parameters
        List<TypedParameter> parameters = List.of(
            new TypedParameter("player", ParameterType.STRING),
            new TypedParameter("amount", ParameterType.INT)
        );
        
        CommandActionNode giveSubcommand = new CommandActionNode(
            "give",
            "Give items to a player",
            "YourPlugin",
            parameters,
            (sender, args) -> {
                String playerName = (String) args.get(0);
                int amount = (int) args.get(1);
                
                sender.sendMessage("Giving " + amount + " items to " + playerName);
                // Your command logic here
            }
        );
        
        // Add the subcommand to the root node
        rootNode.addChild(giveSubcommand);
        
        // Register the command
        CommanderCommand command = new CommanderCommand(rootNode, true);
        command.register(this, "yourcommand");
    }
}

Understanding the Example

Let's break down what's happening in the example above:

  1. We create a CommandGroupNode as the root of our command tree. This represents the base command /yourcommand.

  2. We define a subcommand give as a CommandActionNode with two parameters:

    • A player parameter of type STRING
    • An amount parameter of type INT
  3. We provide an executor lambda that receives the parsed arguments and performs the command logic.

  4. We add the subcommand to the root node.

  5. Finally, we create a CommanderCommand with our command tree and register it with the plugin.

With this setup, users can now use the command /yourcommand give playername 5 and the parameters will be automatically parsed and validated.

Automatic Help Command

Commander automatically generates a help command for your command tree. Users can access it by typing:

/yourcommand help

This will display a list of all available subcommands with their descriptions.

For a more detailed view of the command structure, users can type:

/yourcommand help tree

Next Steps

Now that you have a basic understanding of how to set up Commander, check out the following pages to learn more:

⚠️ **GitHub.com Fallback** ⚠️