> For the complete documentation index, see [llms.txt](https://doc.stellarvan.cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.stellarvan.cn/readme/kai-fa-ji-chu/ming-ling-yu-quan-xian.md).

# 命令与权限

## 命令与权限

命令是大多数插件的第一批交互入口。

你通常要做两件事：

* 在 `plugin.yml` 里声明命令
* 在 Java 代码里处理命令逻辑

### 在 `plugin.yml` 里声明命令

{% code title="plugin.yml" %}

```yaml
commands:
  heal:
    description: Heal yourself
    usage: /heal
    permission: myplugin.heal

permissions:
  myplugin.heal:
    description: Allows the player to use /heal
    default: true
```

{% endcode %}

### 在代码里处理命令

{% code title="HealCommand.java" %}

```java
package com.example.myplugin;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class HealCommand implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("只有玩家可以使用这个命令。");
            return true;
        }

        if (!player.hasPermission("myplugin.heal")) {
            player.sendMessage("你没有权限使用这个命令。");
            return true;
        }

        player.setHealth(player.getMaxHealth());
        player.sendMessage("你已被治疗。");
        return true;
    }
}
```

{% endcode %}

### 注册命令执行器

{% code title="MyPlugin.java" %}

```java
@Override
public void onEnable() {
    getCommand("heal").setExecutor(new HealCommand());
}
```

{% endcode %}

### 返回值是什么意思

`onCommand()` 返回 `true`，表示命令已处理。

返回 `false`，服务器会尝试发送 `usage` 提示。 这适合参数错误场景。

### 常见问题

#### `getCommand("heal")` 返回 `null`

最常见原因是你没在 `plugin.yml` 里声明这个命令。

#### 权限始终无效

先确认权限节点名称完全一致。 再确认玩家是否真的拥有对应权限。

#### 控制台也能执行命令

`CommandSender` 不一定是玩家。 处理玩家专属逻辑前先判断类型。


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://doc.stellarvan.cn/readme/kai-fa-ji-chu/ming-ling-yu-quan-xian.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
