> For the complete documentation index, see [llms.txt](https://docs.aethercube.fr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aethercube.fr/developers/plugins/commands-and-permissions.md). Agents can also search and read this documentation through its [MCP server](https://docs.aethercube.fr/~gitbook/mcp).

# Commands and permissions

How to give your plugin commands, read their arguments and decide who may run them. Start with the short example below and use the tables as a reference later.

## Registering a command

1. Declare the command in `plugin.yml` with its description, usage, aliases and permission. See [plugin.yml](https://docs.aethercube.fr/developers/plugins/plugin-yml.md).
2. Register a handler in `onEnable`:

```java
registerCommand("where", this::where);
```

The handler is a `Function<CommandContext, CommandResult>`. `registerCommand` throws `IllegalArgumentException` when `plugin.yml` does not declare the name.

```java
private CommandResult where(CommandContext c) {
    Player target = c.has(0) ? c.onlinePlayer(0) : c.player().orElse(null);
    if (target == null || target.entity() == null) {
        return CommandResult.error("Name a player who is in the world.");
    }
    Vector3 at = target.entity().position();
    return CommandResult.ok(String.format("%s is at %.1f %.1f %.1f", target.name(), at.x(), at.y(), at.z()));
}
```

The command runs on the simulation thread. The server checks the permission from `plugin.yml` before it calls the handler.

When two owners declare the same command name, the second one is reachable as `owner:name`, for example `skyglass:drifter`.

## `CommandContext`

| Method                               | Meaning                                                                                                                 |
| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- |
| `sender()`                           | The `CommandSender`.                                                                                                    |
| `label()`                            | The name or alias the sender typed.                                                                                     |
| `args()`                             | The arguments after the label, as a list of strings.                                                                    |
| `server()`                           | The `Server`.                                                                                                           |
| `size()`                             | Number of arguments.                                                                                                    |
| `has(i)`                             | Whether argument `i` exists.                                                                                            |
| `player()`                           | `Optional<Player>`: present when the sender is a player.                                                                |
| `string(i)`, `rest(i)`               | One argument, or the rest of the line from `i`.                                                                         |
| `integer(i)`, `integer(i, min, max)` | An integer, optionally range-checked.                                                                                   |
| `decimal(i)`                         | A decimal number.                                                                                                       |
| `bool(i)`                            | A boolean: `true`, `yes` or `on`, and `false`, `no` or `off`.                                                           |
| `uuid(i)`                            | A UUID.                                                                                                                 |
| `enumValue(i, type)`                 | A constant of an enum.                                                                                                  |
| `onlinePlayer(i)`                    | An online player by name.                                                                                               |
| `world(i)`                           | The name of the world, which must be the default world.                                                                 |
| `position(i)`                        | A position from arguments `i`, `i+1` and `i+2`. `~` and `~<offset>` (for example `~5`) are relative to a player sender. |
| `item(i)`                            | An item definition by key.                                                                                              |
| `entity(i)`                          | An entity by id.                                                                                                        |

The parsing methods throw `CommandSyntaxException` for a missing or invalid argument. Do not catch it: the dispatcher answers the sender with the command's usage, which saves you writing the same error handling in every command.

## `CommandResult`

| Factory                        | Effect                                   |
| ------------------------------ | ---------------------------------------- |
| `CommandResult.ok()`           | Success with no message.                 |
| `CommandResult.ok(message)`    | Success. The sender sees the message.    |
| `CommandResult.error(message)` | Failure with a message.                  |
| `CommandResult.usage()`        | Failure that prints the command's usage. |

## `CommandSender`

| Method                | Meaning                              |
| --------------------- | ------------------------------------ |
| `name()`              | The player's name, or the console's. |
| `hasPermission(node)` | Whether the sender holds the node.   |
| `sendMessage(text)`   | Send a line of text.                 |
| `isConsole()`         | `true` for the console.              |

A `Player` is a `CommandSender`.

## Permissions

Declare nodes and defaults in `plugin.yml`. Check them in code with `sender.hasPermission(node)`.

From code you can also use `server.permissions()`:

| Method                                              | Effect                                                                                   |
| --------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| `has(subject, node)`                                | Whether the subject holds the node.                                                      |
| `isOperator(subject)`, `setOperator(subject, flag)` | Read or change the operator flag.                                                        |
| `registerDefault(node, value)`                      | Declare a default at run time. `PermissionDefault` is `TRUE`, `FALSE`, `OP` or `NOT_OP`. |
| `groups()`, `group(id)`                             | Read the groups.                                                                         |
| `defineGroup(id, name, priority, prefix, suffix)`   | Create or update a group.                                                                |
| `deleteGroup(id)`                                   | Remove a group.                                                                          |
| `setGroupPermission(group, node, value)`            | Give, refuse or (with `null`) clear a node.                                              |
| `setGroupParent(group, parent, inherit)`            | Set inheritance.                                                                         |
| `setPlayerPermission(subject, node, value)`         | Same, for one player.                                                                    |
| `setPlayerGroup(subject, group, member)`            | Change membership.                                                                       |
| `playerGroups(subject)`                             | The groups of a player.                                                                  |

The resolution order is in [Operators and permissions](https://docs.aethercube.fr/developers/running-a-server/operators-and-permissions.md).

## Commands outside `plugin.yml`

`server.commands()` is the command registry. A plugin that needs a command it cannot declare statically can implement `Command` (`getName`, `getDescription`, `getUsage`, `getPermission`, `getAliases`, `execute`, and optionally `suggest(context)` for tab completion) and call `register(owner, command)`. The registry also offers `find`, `commands`, `count(owner)`, `unregisterAll(owner)`, `dispatch(sender, line)` and `suggest(sender, partial)`. The server removes everything registered with your plugin as the owner when the plugin is disabled.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.aethercube.fr/developers/plugins/commands-and-permissions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
