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. Cancelling drops the packet;
assigning null to 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; 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 packet is later replaced.

Parameters:

  • packet: the non-null incoming or outgoing packet to store and name

Fields ​

packet ​

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 ​

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

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 to packet. 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

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