Getting Started - LilBroCodes/commander GitHub Wiki
This guide will help you get started with the Commander library for your Bukkit/Spigot/Paper Minecraft plugin.
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'
}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>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]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");
}
}Let's break down what's happening in the example above:
-
We create a
CommandGroupNodeas the root of our command tree. This represents the base command/yourcommand. -
We define a subcommand
giveas aCommandActionNodewith two parameters:- A
playerparameter of typeSTRING - An
amountparameter of typeINT
- A
-
We provide an executor lambda that receives the parsed arguments and performs the command logic.
-
We add the subcommand to the root node.
-
Finally, we create a
CommanderCommandwith 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.
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
Now that you have a basic understanding of how to set up Commander, check out the following pages to learn more:
- Core Concepts - Understand the fundamental concepts of Commander
- Command Nodes - Learn about the different types of command nodes
- Parameter Types - Explore the parameter types and validation