> 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/shi-jian-jian-ting.md).

# 事件监听

## 事件监听

事件系统是插件开发的核心。

玩家加入、方块放置、实体死亡、背包点击，都会触发事件。

### 一个最小监听器

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

```java
package com.example.myplugin;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class JoinListener implements Listener {
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        event.getPlayer().sendMessage("欢迎来到服务器。");
    }
}
```

{% endcode %}

### 注册监听器

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

```java
@Override
public void onEnable() {
    getServer().getPluginManager().registerEvents(new JoinListener(), this);
}
```

{% endcode %}

### 事件对象里有什么

事件对象会带上上下文数据。

比如 `PlayerJoinEvent` 里有玩家对象。 有些事件还允许你取消行为。

### 常见写法

你会经常写这几步：

1. 创建一个实现 `Listener` 的类
2. 写一个带 `@EventHandler` 的方法
3. 接收对应事件对象
4. 在 `onEnable()` 里注册

### 常见问题

#### 监听器没有触发

先检查是否真的注册了监听器。 再确认方法上有 `@EventHandler`。

#### 事件被别的插件改掉了

多插件环境里很常见。 你需要学会检查事件是否已取消，或调整事件优先级。

#### 逻辑太重

不要在高频事件里做昂贵操作。

比如玩家移动事件会触发很多次。 这种场景要格外注意性能。

{% hint style="info" %}
刚开始先从 `PlayerJoinEvent`、`BlockBreakEvent`、`PlayerInteractEvent` 这类事件入手。 它们最容易理解，也最常用。
{% 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/shi-jian-jian-ting.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.
