Contents
Class CachedPacket<T extends ToClientPacket>
java.lang.Object
com.hypixel.hytale.protocol.CachedPacket<T>
- All Implemented Interfaces:
Packet, ToClientPacket, AutoCloseable
public final class CachedPacket<T extends ToClientPacket>
extends Object
implements ToClientPacket, AutoCloseable
A pre-serialized packet for efficient broadcast to multiple recipients.
Caches the serialized bytes once, then writes them directly for each send.
Implements AutoCloseable for use with try-with-resources to prevent leaks:
try (var cached = CachedPacket.cache(myPacket)) {
for (var player : players) {
player.getPacketHandler().write(cached);
}
} // automatically released
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends ToClientPacket>
CachedPacket<T> cache(T packet) Create a cached version of the given packet.voidclose()Release the underlying buffer.intCompute the exact number of bytes needed to serialize this packet.intReturns the channel this packet should be sent on.intgetId()Returns the unique packet ID.intserialize(MemorySegment buffer, int offset) Writes this packet atoffsetand returns the number of bytes written, which callers rely on being exactlyPacket.computeSize().
-
Method Details
-
cache
Create a cached version of the given packet. The packet is serialized immediately and the bytes are retained. -
getId
-
getChannel
Description copied from interface:PacketReturns the channel this packet should be sent on.- Specified by:
getChannelin interfacePacket
-
serialize
Description copied from interface:PacketWrites this packet atoffsetand returns the number of bytes written, which callers rely on being exactlyPacket.computeSize(). The framed write path reserves and length-prefixes on that figure before serializing, so a shorter or longer write corrupts the frame rather than failing. -
computeSize
public int computeSize()Description copied from interface:PacketCompute the exact number of bytes needed to serialize this packet.- Specified by:
computeSizein interfacePacket
-
getPacketType
-
getCachedSize
public int getCachedSize() -
close
public void close()Release the underlying buffer. Call when the cached packet is no longer needed. Can also use try-with-resources for automatic cleanup.- Specified by:
closein interfaceAutoCloseable
-