> 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-shi/di-yi-ge-cha-jian.md).

# 第一个插件

## 第一个插件

先做一个能正常加载的最小插件。

这个插件只做三件事：

* 服务器启动时输出日志
* 服务器关闭时输出日志
* 提供一个 `/hello` 命令

### `pom.xml`

{% code title="pom.xml" %}

```xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>hello-plugin</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <repository>
            <id>papermc-repo</id>
            <url>https://repo.papermc.io/repository/maven-public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>io.papermc.paper</groupId>
            <artifactId>paper-api</artifactId>
            <version>1.21.1-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
```

{% endcode %}

### 主类

把主类放在 `src/main/java/com/example/helloplugin/HelloPlugin.java`。

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

```java
package com.example.helloplugin;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

public final class HelloPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        getLogger().info("HelloPlugin 已启用");
    }

    @Override
    public void onDisable() {
        getLogger().info("HelloPlugin 已停用");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (command.getName().equalsIgnoreCase("hello")) {
            sender.sendMessage("Hello from plugin!");
            return true;
        }
        return false;
    }
}
```

{% endcode %}

### `plugin.yml`

把这个文件放在 `src/main/resources/plugin.yml`。

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

```yaml
name: HelloPlugin
version: 1.0.0
main: com.example.helloplugin.HelloPlugin
api-version: '1.21'
commands:
  hello:
    description: Say hello
    usage: /hello
```

{% endcode %}

### 构建和测试

1. 运行 `mvn package`
2. 找到 `target/hello-plugin-1.0.0.jar`
3. 把它放进测试服的 `plugins/` 目录
4. 启动服务器
5. 在游戏里输入 `/hello`

如果插件加载成功，你会在控制台看到启用日志。 命令也会正常返回消息。

### 这一页学到了什么

你已经完成了插件开发最小闭环：

* 能编译
* 能加载
* 能执行代码
* 能在服务器里验证结果

下一步就该学生命周期、命令和事件。


---

# 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-shi/di-yi-ge-cha-jian.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.
