# RecvPacket

`com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket`

**Since:** 1.8.4

**Cancellable:** Yes. Call `event.cancel()` in a handler before releasing the event lock.

Fires for an incoming packet before it is handled by the game. A joined listener can cancel or
 replace the packet with `setPacket(...)`; assigning `null` drops it. A joined script
 listener runs on a worker thread while the network thread waits until the script releases the
 packet, so keep it short; pass `false` to observe asynchronously without blocking or
 changing the packet. Bundled packets are reported as a
 single packet, not as separate events for their contents. Use a filterer to restrict which
 packets invoke a listener. Packet-buffer edits affect handling only
 after the decoded packet is assigned with `setPacket(...)`; check
 [canGetPacketBuffer()](#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_canGetPacketBuffer-) because not every packet has a standalone codec.  
 
 ```js
const filterer = JsMacros.createEventFilterer("RecvPacket").setType("HealthUpdateS2CPacket");  
const listener = JsMacros.on("RecvPacket", filterer, JavaWrapper.methodToJava(event => {
  if (!event.canGetPacketBuffer()) return;  
  const buffer = event.getPacketBuffer();  
  // This packet's first field is the health value.  
  Chat.log(`the server says my health is ${buffer.readFloat()}`);  
  // Reading advances the buffer; reset before reading or decoding it again.  
  buffer.reset();  
}));  
// Later, remove the listener:  
JsMacros.off(listener);  
 ```

**Filterer:** [FiltererRecvPacket](../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/event/filterer/FiltererRecvPacket.md). Create it with `JsMacros.createEventFilterer("RecvPacket")` and pass it to the filterer overload of `JsMacros.on`.

## Constructors

### new EventRecvPacket {#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_constructor-Packet-}

```js
new EventRecvPacket(Packet<?> packet)
```

Initializes the event with the client core and captures the supplied packet and its type.  
 The packet is stored by reference; it is not cloned, and this constructor does not dispatch,  
 handle, or send it. The captured type uses a registered shortcut name when available, otherwise
 the packet's simple class name, and remains unchanged if `setPacket(...)` is later called.

**Parameters:**
- `packet`: the non-null incoming or outgoing packet to store and name

## Fields

### packet {#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_packet}

```js
packet: Packet<?>
```

The incoming packet object. Replacing it changes the packet handled by the game; assigning
 `null` drops it. Buffer edits do not affect this field until the decoded packet is
 assigned here.

**Deprecated:** Removal in 3.0.0: Use [getPacket()](#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_getPacket-) and [setPacket(Packet)](#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_setPacket-Packet-) instead.

### type {#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_type}

```js
type: String
```

The packet's convenience name, matching names in
 [PacketByteBufferHelper.getPacketNames()](../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/PacketByteBufferHelper.md#com.jsmacrosce.jsmacros.client.api.helper.PacketByteBufferHelper_getPacketNames-) and accepted by [FiltererRecvPacket](../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/event/filterer/FiltererRecvPacket.md).  
 Unlisted packet classes fall back to their simple class name; names can vary by game version.

## Methods

### canGetPacketBuffer {#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_canGetPacketBuffer-}

```js
canGetPacketBuffer(): boolean
```

Checks whether the current packet has a registered phase and standalone codec. Returns
 `false` when the current packet is `null`. This does not validate packet contents;  
 bundles are unsupported.

**Returns:** whether a standalone packet buffer is supported

**Since:** 2.1.0

### getPacket {#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_getPacket-}

```js
getPacket(): Packet<?>
```

Returns the incoming packet object, or `null` when handling is suppressed.

**Returns:** the current packet, or `null`

**Since:** 2.2.0

### getPacketBuffer {#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_getPacketBuffer-}

```js
getPacketBuffer(): PacketByteBufferHelper
```

Returns a helper containing the packet encoded in its wire format. Reads and writes advance
 its independent buffer; edits affect handling only after decoding and assigning the result
 with `setPacket(...)`. Call [PacketByteBufferHelper.reset()](../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/PacketByteBufferHelper.md#com.jsmacrosce.jsmacros.client.api.helper.PacketByteBufferHelper_reset-) to restore its original contents
 and reader position. Check [canGetPacketBuffer()](#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_canGetPacketBuffer-) first; bundles and other unsupported
 packets have no standalone codec.  
 
 ```ts
const filterer = (JsMacros.createEventFilterer("RecvPacket") as FiltererRecvPacket)
  .setType("HealthUpdateS2CPacket");  
JsMacros.on("RecvPacket", filterer, JavaWrapper.methodToJava(function (event: EventRecvPacket) {
  if (!event.canGetPacketBuffer()) return;  
  // HealthUpdateS2CPacket writes a float health, then the food level and saturation
  const buffer = event.getPacketBuffer();  
  const health = buffer.readFloat();  
  Chat.log(`health is ${health}`);  
  buffer.reset();  
}));  
 ```

**Returns:** a helper for reading or editing the packet's encoded data

**Since:** 1.8.4

### setPacket {#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_setPacket-Packet-}

```js
setPacket(Packet<?> packet): void
```

Replaces the incoming packet. `null` drops it. Buffer edits do not affect the packet
 until the decoded result is assigned here.

**Parameters:**
- `packet`: replacement packet, or `null` to drop it

**Since:** 2.2.0

### toString {#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_toString-}

```js
toString(): String
```

Returns a diagnostic string containing the event name and the type captured at construction.  
 Its `eventName:{"type": "type"}` shape is not JSON, and the captured type is not escaped.

**Returns:** a diagnostic string with the event name and captured packet type
