RecvPacket
com.jsmacrosce.jsmacros.client.api.event.impl.EventRecvPacket
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.
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
setPacket(...) is later called.Parameters:
packet: the non-null incoming or outgoing packet to store and name
Fields
packet
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
Methods
canGetPacketBuffer
false when the current packet is null. This does not validate packet contents; bundles are unsupported.getPacket
getPacketBuffer
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();
}));