> For the complete documentation index, see [llms.txt](https://openjs.wiki.gd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openjs.wiki.gd/openjs-components/interacting-with-external-plugins/protocollib-integration.md).

# ProtocolLib integration

[**ProtocolLib**](https://www.spigotmc.org/resources/1997/) is a powerful Minecraft plugin that allows other plugins to **intercept, read, and modify packets** sent between the **server and client**.

While it's technically possible to access ProtocolLib directly via raw Javascript in scripts, this is **not recommended**. Unofficial use results in:

* Memory leaks
* Script instability
* Limitations due to Java-JS translation

OpenJS provides **official support** for ProtocolLib, exposing a clean and stable API.

***

{% hint style="warning" %}

### Enabling ProtocolLib Support

At the **very top of your script**, include the following [feature flag](/openjs-components/fflags.md)

```javascript
//!ProtocolLib
```

**Without this flag, the ProtocolLib API is not accessible in the script.**
{% endhint %}

***

### API Overview

#### `ProtocolLib.registerListener(priority, handler, packetTypes)`

Registers a new packet listener.

* `priority`: `"LOW"`, `"NORMAL"`, `"HIGH"`, or `"MONITOR"`
* `handler`: JavaScript object containing `onSend(event)` and/or `onReceive(event)`
* `packetTypes`: Array of packet names, e.g., `"Play.Server.BLOCK_CHANGE"`

**Returns:** a `listener` object

```js
listener = ProtocolLib.registerListener("NORMAL", {
  onSend(event) {
    const packet = event.getPacket();
    // handle packet here
  }
}, ["Play.Server.BLOCK_CHANGE"]);
```

{% hint style="info" %}
**Additional info on `packetTypes:`**&#x20;

The `packetType` is specified relative to the `com.comphenix.protocol.PacketType` class. For example, `Play.Server.BLOCK_CHANGE` corresponds to `com.comphenix.protocol.PacketType.Play.Server.BLOCK_CHANGE`. All provided packet types follow this structure and are based on their original class path.
{% endhint %}

***

#### `ProtocolLib.unregisterListener(listener)`

Unregisters a previously registered packet listener.

```js
ProtocolLib.unregisterListener(listener);
```

***

### Packet Event Object

The `event` passed into your handler (`onSend` / `onReceive`) provides access to:

| Method            | Description                                           |
| ----------------- | ----------------------------------------------------- |
| `getPacket()`     | Returns the actual ProtocolLib packet                 |
| `getPacketType()` | Returns packet type as enum (e.g. `Play.Server.CHAT`) |

You can use:

* `read(index)` / `write(index, value)` to access fields
* Packet-specific methods like `getBlockData()` or `getBlockDataArrays()`

***

### Example Script: Block Spoofing

This script replaces all outgoing non-air blocks with a spoofed `OAK_LOG` block:

```js
//!import org.bukkit.Material
//!import com.comphenix.protocol.wrappers.WrappedBlockData
//!ProtocolLib

const Material = org.bukkit.Material;
const createDataMethod = getMethod(WrappedBlockData, "createData", ["org.bukkit.Material"]);

let SpoofedBlock = createDataMethod.invoke(null, [Material.OAK_LOG]);

const handler = {
  onSend(event) {
    const packet = event.getPacket();
    const type = event.getPacketType().name();

    if (type === "BLOCK_CHANGE") {
      const blockData = packet.getBlockData().read(0);
      if (!blockData.getType().equals(Material.AIR)) {
        packet.getBlockData().write(0, SpoofedBlock);
      }
    }

    if (type === "MULTI_BLOCK_CHANGE") {
      let blocks = packet.getBlockDataArrays().read(0);
      for (let i = 0; i < blocks.length; i++) {
        if (!blocks[i].getType().equals(Material.AIR)) {
          blocks[i] = SpoofedBlock;
        }
      }
      packet.getBlockDataArrays().write(0, blocks);
    }
  }
};

const listener = ProtocolLib.registerListener("NORMAL", handler, [
  "Play.Server.BLOCK_CHANGE",
  "Play.Server.MULTI_BLOCK_CHANGE"
]);
```

***

### Best Practices

* Unregister unused listeners with `ProtocolLib.unregisterListener(...)`
* Avoid modifying packets in `"MONITOR"` priority (read-only phase)
* Use `getMethod()` and `invoke()` carefully for wrapper class access

***

### Related Links

* [Feature Flags Reference](/openjs-components/fflags.md) – Enable/disable APIs via script headers
* [Event Handling](/openjs-components/events/events.md) – Learn how to listen to Bukkit and custom events


---

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://openjs.wiki.gd/openjs-components/interacting-with-external-plugins/protocollib-integration.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
