An open API service indexing awesome lists of open source software.

https://github.com/imperazim/libpacket

Packet handler library for PocketMine (Minecraft PE)
https://github.com/imperazim/libpacket

Last synced: 12 months ago
JSON representation

Packet handler library for PocketMine (Minecraft PE)

Awesome Lists containing this project

README

          

# LibPacket

LibPacket is a PHP library for PocketMine-MP that simplifies packet handling by letting you register specific handlers instead of writing long `if ($packet instanceof …)` chains. In other words, it helps “put an end to the if-elseif instanceof hell”. It provides a clean API to handle data packets per network session.

## Project Description

* **Packet Monitor:** Use this if you want to *observe* packet events without changing them. A Packet Monitor registers callbacks for `DataPacketReceiveEvent`/`DataPacketSendEvent` at the `MONITOR` priority, which by convention means your code should not alter the packet. This is useful for logging or debugging packets (e.g. dumping packet contents or tracking packet flow).
* **Packet Interceptor:** Use this to *handle and possibly modify* packets before they reach the rest of the system. Interceptors register callbacks at a lower priority (by default `NORMAL`). In an interceptor you can cancel a packet or change its data (for example, blocking certain packet types or injecting new data) before PocketMine processes it.

These components let your plugin focus on relevant packets and actions, without needing manual `instanceof` checks for each packet type.

## Installation

No special installation steps or dependencies are required. Simply include or autoload LibPacket in your plugin (for example via Composer or by placing the files in your plugin folder). LibPacket works out of the box with PocketMine-MP plugins.

## Usage

Use `LibPacket::createMonitor($plugin)` or `LibPacket::createInterceptor($plugin)` to create the respective component, then register your handlers. For example, to intercept `AvailableCommandsPacket` you could write:

```php
$interceptor = LibPacket::createInterceptor($this);
$interceptor->registerIncoming(new LibCommandInterceptor());
```

Then implement the handler class. Here’s an example of a `LibCommandInterceptor` that implements `PacketHandlerInterface` to process the `AvailableCommandsPacket`:

```php
class LibCommandInterceptor implements \imperazim\packet\handler\PacketHandlerInterface {
public function getPacketIds(): array {
// Specify which packet types this handler should receive
return [\pocketmine\network\mcpe\protocol\AvailableCommandsPacket::class];
}

public function handle($packet, $session): bool {
if ($packet instanceof \pocketmine\network\mcpe\protocol\AvailableCommandsPacket) {
// Manipulate the packet’s data here (e.g. filter out commands).
}
// Return true to allow the packet, or false to cancel it.
return true;
}
}
```

In this example, `getPacketIds()` tells LibPacket which packet classes to listen for. The `handle()` method will be called for each matching packet: if you return `false`, the packet event is canceled (preventing PocketMine from processing it), and if you return `true`, PocketMine will continue as normal. This makes it easy to modify or block packets on the fly (for instance, to remove commands or change packet flags).