Skip to content

RecvPacket ​

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

Since1.8.4Cancellable

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() 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. Create it with JsMacros.createEventFilterer("RecvPacket") and pass it to the filterer overload of JsMacros.on.

Constructors ​

new EventRecvPacket ​

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 ​

packet: Packet<?>deprecated#
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() and setPacket(Packet) instead.

type ​

type: Stringread-only#
The packet's convenience name, matching names in PacketByteBufferHelper.getPacketNames() and accepted by FiltererRecvPacket. Unlisted packet classes fall back to their simple class name; names can vary by game version.

Methods ​

canGetPacketBuffer ​

canGetPacketBuffer(): booleanSince2.1.0#
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

getPacket ​

getPacket(): Packet<?>Since2.2.0#
Returns the incoming packet object, or null when handling is suppressed.
Returns: the current packet, or null

getPacketBuffer ​

getPacketBuffer(): PacketByteBufferHelperSince1.8.4#
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() to restore its original contents and reader position. Check canGetPacketBuffer() first; bundles and other unsupported packets have no standalone codec.
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

setPacket ​

setPacket(Packet<?> packet): voidSince2.2.0#
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

toString ​

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