Building a Command

The first step is to get a new CommandSpec builder. The builder provides methods to modify the command help messages, command arguments and the command logic. These methods can be chained.

To finally build the command, you’ll want to call the CommandSpec.Builder#build() method.

After that, you have to register the command.

Example: Building a Simple Command

import org.spongepowered.api.Sponge;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.command.spec.CommandSpec;

PluginContainer plugin = ...;

CommandSpec myCommandSpec = CommandSpec.builder()
    .description(Text.of("Hello World Command"))
    .executor(new HelloWorldCommand())

Sponge.getCommandManager().register(plugin, myCommandSpec, "helloworld", "hello", "test");

Overview of the CommandSpec builder methods




Defines the command logic (See Writing a Command Executor).

Setting the executor is required if no child commands are set.


Sets the argument specification for this command (See Argument Parsing).


Set the permission that will be checked before using this command.


A short, one-line description of this command’s purpose that will be displayed by the help system.


Sets an extended description to use in longer help listings. Will be appended to the short description.


Adds a child command to this command with its aliases (See Child Commands).


Sets the child commands of this command with their aliases (See Child Commands).


Defines how the arguments will be parsed. By default, the parser splits the command input by spaces. Quotations count as a single argument.

Example: /tpworld Notch "My World" would result in two arguments: Notch and My World.


Builds the command. After that, you have to register the command.

Writing a Command Executor

The only required component to build a simple command is the command executor class, which contains the logic of the command.

The class has to implement the CommandExecutor interface, which defines the CommandExecutor#execute(CommandSource, CommandContext) method. The method is called on command execution and has two arguments:

  • The source of the command call (e.g. the console, a command block or a player)

  • The command context object, which contains the parsed arguments (See Argument Parsing)

Example: Simple Command Executor

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;

public class HelloWorldCommand implements CommandExecutor {

    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
        src.sendMessage(Text.of("Hello World!"));
        return CommandResult.success();


You can use anonymous classes to define the command executor in the command build process (See the example in the Argument Parsing page).

Player-Only Commands

Sometimes it is necessary that only players can execute a command (e.g. a /suicide command).

Perform an instanceof check to determine the type of the CommandSource:

import org.spongepowered.api.command.source.CommandBlockSource;
import org.spongepowered.api.command.source.ConsoleSource;

if (src instanceof Player) {
    Player player = (Player) src;
    player.sendMessage(Text.of("Hello " + player.getName() + "!"));
else if(src instanceof ConsoleSource) {
    src.sendMessage(Text.of("Hello GLaDOS!"));
    // The Cake Is a Lie
else if(src instanceof CommandBlockSource) {
    src.sendMessage(Text.of("Hello Companion Cube!"));
    // <3


We recommend you to add an optional [player] argument to make the command console-friendly (e.g. /suicide [player]).

The easiest solution for this is to append a playerOrSource command element (See Argument Parsing).

Command Results

The CommandExecutor#execute() method must always return a CommandResult. In most cases it is sufficient to return CommandResult#success() if the command was successful or CommandResult#empty() if it wasn’t. In cases where more information needs to be conveyed, a CommandResult#builder() should be used. The builder provides the several various methods that accepts an integer and will set the attribute of the same name. All attributes that are not set by the builder will be empty.

Command blocks can use those values to modify scoreboard stats, which then can be used for elaborate constructions consisting of multiple command blocks. A tutorial how the data is accessed can be found here.

Example: Building a CommandResult

CommandResult result = CommandResult.builder()

This example uses a builder to create a CommandResult for a command which affected 42 entities and was successful.

Error Handling

The execute() method may also throw a CommandException, signaling that an error occurred while trying to execute the command. If such an Exception is thrown, its message will be displayed to the command source, formatted as an error. Also, the commands usage message will be displayed. An ArgumentParseException, a subtype of CommandException is automatically thrown if the commands arguments could not be parsed.