> For the complete documentation index, see [llms.txt](https://facuu16.gitbook.io/mcjs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://facuu16.gitbook.io/mcjs/listeners.md).

# Listeners

Listeners, also known as "**event listeners**", are an essential part of script development. In the context of Minecraft and other game development environments, listeners are used to detect and respond to specific events that occur in the game. Here's an example:

```javascript
import("org.bukkit.event.player.PlayerJoinEvent");

function build(build) {
    build.registerListener(PlayerJoinEvent, function (event) {
        var player = event.getPlayer();

        API.message(player, "Welcome to the server!");
    });
}
```

**Import Statement:** The `import("org.bukkit.event.player.PlayerJoinEvent");` statement is used to import the "**PlayerJoinEvent**" class from the "**org.bukkit.event.player**" package. This event class is triggered when a player joins the server.

**Registering a Listener:** In the `build` function, you use `build.registerListener(PlayerJoinEvent, function (event) {...});` to register a listener for the "**PlayerJoinEvent**". This means that your script will listen for this specific event and execute the provided function when the event occurs.

**Listener Function:** The `(event) {...}` function is the listener function that will be executed when a player joins the server. It receives the event object as its parameter, allowing you to access event information. In this case, it retrieves the player who joined using **event.getPlayer()** and stores it in the **player** variable.

**Using the Listener:** Within the listener function, you can use the **player** variable to interact with the player who joined. In this example, you use **API.message(player, "Welcome to the server!");** to send a welcome message to the joining player using the **API.message** method.


---

# 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:

```
GET https://facuu16.gitbook.io/mcjs/listeners.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
