# SendPacket

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

**Since:** 1.8.4

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

Fires synchronously at the client's packet-send path, including for non-gameplay packets.  
A joined listener can cancel sending or replace the packet with `setPacket(...)`;  
assigning `null` drops it. A joined script listener runs on a worker thread while the
sending thread waits until the script releases the packet, so keep it short; pass `false`
to observe asynchronously without blocking or changing the packet. A buffer
from `getPacketBuffer()` is independent, so apply edits by decoding it and assigning the
result to `setPacket(...)`. Use a filterer to limit which packets invoke a listener.

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

## Constructors

### new EventSendPacket {#com.jsmacrosce.jsmacros.client.api.event.impl.EventSendPacket_constructor-Packet-}

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

Initializes the event with the client core and the supplied outgoing packet, retaining the
packet reference without sending or dispatching it. The packet must be non-null. [type](#com.jsmacrosce.jsmacros.client.api.event.impl.EventSendPacket_type)
captures its shortcut name, or its simple class name when no shortcut is registered; this
value does not change if `setPacket(...)` is later called.

**Parameters:**
- `packet`: the non-null outgoing packet to store

## Fields

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

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

The outgoing packet object. Assigning a replacement changes what is sent; assigning
`null` drops it. Buffer edits do not alter this field unless the decoded packet is
assigned here.

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

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

```js
type: String
```

The convenience name used by [PacketByteBufferHelper.getPacketNames()](../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/PacketByteBufferHelper.md#com.jsmacrosce.jsmacros.client.api.helper.PacketByteBufferHelper_getPacketNames-) and
[FiltererSendPacket](../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/event/filterer/FiltererSendPacket.md). Unlisted packet classes use their simple class name; names may
vary by game version.

## Methods

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

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

Checks whether the current packet has a selectable registered phase and standalone codec.  
Returns `false` when the current packet is `null`. This does not guarantee that
encoding its contents will succeed; bundles are unsupported.

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

**Since:** 2.1.0

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

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

Returns the outgoing packet object, or `null` when sending is suppressed.

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

**Since:** 2.2.0

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

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

Encodes the current packet into an independent buffer helper. Read and write operations change
only that buffer; to change what is sent, decode the edits and assign the packet with
`setPacket(...)`. Unsupported packets, including bundles, cause an
[IllegalArgumentException](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/IllegalArgumentException.html); [canGetPacketBuffer()](#com.jsmacrosce.jsmacros.client.api.event.impl.EventSendPacket_canGetPacketBuffer-) checks codec availability but
does not guarantee encoding succeeds.

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

**Since:** 1.8.4

### replacePacket {#com.jsmacrosce.jsmacros.client.api.event.impl.EventSendPacket_replacePacket-Object[]-}

```js
replacePacket(Object... args): void
```

Constructs a packet of the current packet's class and assigns it with
`setPacket(...)`. It first
tries an exact runtime-argument-type match, then constructors with the same argument count,  
coercing numeric arguments to parameter types.

**Parameters:**
- `args`: the arguments to pass to the packet's constructor

**Since:** 1.8.4

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

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

Replaces the outgoing packet. `null` drops it. Buffer edits do not change the packet
unless 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.EventSendPacket_toString-}

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

Returns a diagnostic string containing the event name and the type captured at construction.  
The format is `%s:{"type": "%s"}`; it is not JSON, and the type is not escaped.

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