# 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. Cancelling drops the packet;  
 assigning `null` to [packet](#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_packet) has the same effect. 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 to [packet](#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_packet); 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 [packet](#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_packet) is later replaced.

**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.

### 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; this does not
 validate packet contents. Bundles are unsupported.

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

**Since:** 2.1.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 to
 [packet](#com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket_packet). 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

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