> 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/cha-jian-sheng-ming-zhou-qi.md).

# 插件生命周期

## 插件生命周期

每个 Bukkit / Spigot / Paper 插件都有自己的生命周期。

最常用的是这三个方法：

* `onLoad()`
* `onEnable()`
* `onDisable()`

### `onLoad()`

这个阶段发生得很早。

适合做轻量初始化。 不要在这里注册大部分运行逻辑。

### `onEnable()`

这是最重要的入口。

通常你会在这里做这些事：

* 注册事件监听器
* 注册命令执行器
* 加载配置文件
* 启动定时任务
* 建立外部连接

### `onDisable()`

服务器关闭或插件停用时会调用。

这里应该做清理工作：

* 保存未落盘的数据
* 停止任务
* 关闭文件或数据库连接
* 输出停用日志

### 常见写法

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

```java
@Override
public void onEnable() {
    saveDefaultConfig();
    getLogger().info("插件已启用");
}

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

{% endcode %}

### 常见错误

#### 在构造函数里做太多事

主类构造阶段太早。 很多 Bukkit API 这时还不安全。

#### 在 `onEnable()` 里写很重的阻塞逻辑

比如长时间读文件、网络请求或数据库初始化。

这会拖慢服务器启动。 有些任务应该异步处理。

#### 在 `onDisable()` 里不保存数据

这会让玩家数据、缓存或配置修改直接丢失。

{% hint style="info" %}
先把主逻辑放进 `onEnable()`。 等你对插件结构更熟后，再拆分为服务类和管理器类。
{% endhint %}


---

# 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/cha-jian-sheng-ming-zhou-qi.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.
