# Packet Events

## SendPacketEvent

SendPacketEvent is (as the name suggests) an event that is fired every time the client sends a packet.

This event also gets the packet that is going to be sent, as a `Packet<?>`. You can use code like the code below to do things with it.

{% hint style="warning" %}
This event is cancellable, meaning you can stop the packet from getting into the games code. Be careful with what you cancel!
{% endhint %}

```java
@Subscribe
public void onPacketSend(SendPacketEvent event) {
    // check if the packet is the right type
    if(event.packet instanceof S0FPacketSpawnMob) {
        // cast the packet to the right type
        S0FPacketSpawnMob packet = (S0FPacketSpawnMob) event.packet;
        // do something with it
        System.out.println("Entity attempted to be spawned with ID: " + packet.getEntityID());
        // cancel the event afterwards, rendering this packet useless.
        event.isCancelled = true;
    }
}
```

## ReceivePacketEvent

ReceivePacketEvent is (as the name suggests) an event that is fired every time the client receives a packet.

This event also gets the packet that has been received, as a `Packet<?>`. You can use code like the code below to do things with it.

{% hint style="warning" %}
This event is Cancel-able, meaning you can stop the packet from getting into the games code. Be careful with what you cancel!
{% endhint %}

```java
@Subscribe
public void onPacketReceive(ReceivePacketEvent event) {
    // check if the packet is the right type
    if(event.packet instanceof S0FPacketSpawnMob) {
        // cast the packet to the right type
        S0FPacketSpawnMob packet = (S0FPacketSpawnMob) event.packet;
        // do something with it
        System.out.println("Received data from server about an entity spawned with ID: " + packet.getEntityID());
    }
}
```


---

# Agent Instructions: 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://docs.polyfrost.org/oneconfig/events/available-events/packet-events.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.
