CachedPacket
Package: com.hypixel.hytale.protocol
public final class CachedPacket<T extends ToClientPacket> implements ToClientPacket, AutoCloseableWraps a ToClientPacket by pre-serializing its bytes into a Netty ByteBuf for efficient repeated transmission. This avoids re-serializing the same packet data when sending to multiple clients. Uses Netty reference counting for memory management — the cached buffer must be released via close() when no longer needed.
Factory Method
Section titled “Factory Method”public static <T extends ToClientPacket> CachedPacket<T> cache(@Nonnull T packet)Creates a new CachedPacket by serializing the given packet into a buffer. Throws IllegalArgumentException if the input is already a CachedPacket (nesting is not allowed).
Methods
Section titled “Methods”@Overridepublic int getId()Returns the packet ID of the wrapped packet.
@Overridepublic NetworkChannel getChannel()Returns the network channel of the wrapped packet.
@Overridepublic void serialize(@Nonnull ByteBuf buf)Writes the pre-serialized bytes into the given buffer. Throws IllegalStateException if the cached buffer has already been released.
@Overridepublic int computeSize()Returns the size of the cached serialized data in bytes.
public Class<T> getPacketType()Returns the Class object for the wrapped packet type.
public int getCachedSize()Returns the number of readable bytes in the cached buffer.
@Overridepublic void close()Releases the underlying Netty ByteBuf. After calling close(), the CachedPacket must not be used for serialization.
Usage Pattern
Section titled “Usage Pattern”// Pre-serialize a packet onceCachedPacket<MyPacket> cached = CachedPacket.cache(myPacket);try { // Send to multiple clients without re-serialization for (Connection conn : connections) { conn.send(cached); }} finally { cached.close(); // Release the buffer}