Class PacketIO

java.lang.Object
com.hypixel.hytale.protocol.io.PacketIO

public final class PacketIO extends Object
Utility class for reading and writing protocol data.
  • Field Details

    • FRAME_HEADER_SIZE

      public static final int FRAME_HEADER_SIZE
      Header size for framed packets (4 bytes for packet ID).
      See Also:
    • PACKET_HEADER_SIZE

      public static final int PACKET_HEADER_SIZE
      Packet header size, 4 bytes for id, 4 bytes for length
      See Also:
    • UTF8

      public static final Charset UTF8
    • ASCII

      public static final Charset ASCII
    • PROTO_BOOL

      public static final ValueLayout.OfBoolean PROTO_BOOL
    • PROTO_BYTE

      public static final ValueLayout.OfByte PROTO_BYTE
    • PROTO_SHORT

      public static final ValueLayout.OfShort PROTO_SHORT
    • PROTO_INT

      public static final ValueLayout.OfInt PROTO_INT
    • PROTO_LONG

      public static final ValueLayout.OfLong PROTO_LONG
    • PROTO_FLOAT

      public static final ValueLayout.OfFloat PROTO_FLOAT
    • PROTO_DOUBLE

      public static final ValueLayout.OfDouble PROTO_DOUBLE
    • UUID_LONG

      public static final ValueLayout.OfLong UUID_LONG
    • ZERO_VECTOR2

      public static final org.joml.Vector2fc ZERO_VECTOR2
    • ZERO_VECTOR3

      public static final org.joml.Vector3fc ZERO_VECTOR3
    • ZERO_VECTOR4

      public static final org.joml.Vector4fc ZERO_VECTOR4
    • ZERO_QUATERNION

      public static final org.joml.Quaternionfc ZERO_QUATERNION
    • ZERO_MATRIX

      public static final org.joml.Matrix4fc ZERO_MATRIX
  • Method Details

    • readBytes

      @Nonnull public static byte[] readBytes(@Nonnull io.netty.buffer.ByteBuf buf, int offset, int length)
    • readFixedAsciiString

      @Nonnull public static String readFixedAsciiString(@Nonnull io.netty.buffer.ByteBuf buf, int offset, int length)
    • readFixedString

      @Nonnull public static String readFixedString(@Nonnull io.netty.buffer.ByteBuf buf, int offset, int length)
    • readFixedAsciiString

      @Nonnull public static String readFixedAsciiString(@Nonnull MemorySegment mem, int offset, int length)
    • readFixedString

      @Nonnull public static String readFixedString(@Nonnull MemorySegment mem, int offset, int length)
    • readVarString

      @Nonnull public static String readVarString(@Nonnull io.netty.buffer.ByteBuf buf, int offset)
    • readVarString

      @Nonnull public static String readVarString(@Nonnull io.netty.buffer.ByteBuf buf, int offset, Charset charset)
    • readValidatedAsciiString

      @Nonnull public static String readValidatedAsciiString(@Nonnull io.netty.buffer.ByteBuf buf, int offset, int length, @Nonnull String fieldName)
      Reads bytes from the buffer, validates they are ASCII (0-127), and returns the decoded string. Combines validation and reading in a single pass over the bytes.
      Throws:
      ProtocolException - if any byte is outside the ASCII range
    • readVarString

      @Nonnull public static String readVarString(String fieldName, @Nonnull MemorySegment mem, int offset, int minLength, int maxLength)
    • readVarString

      @Nonnull public static String readVarString(String fieldName, @Nonnull MemorySegment mem, int offset, int minLength, int maxLength, long packed)
      The packed parameter is the VarInt.getWithLength result for offset, so a caller that already decoded the prefix does not decode it twice.
    • isWellFormedUtf8

      public static boolean isWellFormedUtf8(@Nonnull byte[] bytes)
      Table 3-7 of the Unicode standard. Decoding substitutes U+FFFD rather than rejecting, and Java and .NET disagree on how many it substitutes for one ill-formed sequence, so neither the decoded length nor a re-encoded length is a portable test. Both decode walks have to accept exactly the same bytes, or one end rejects a payload the other end accepted.
    • readVarString

      @Nonnull public static String readVarString(String fieldName, @Nonnull MemorySegment mem, int offset, int maxLength)
    • readValidatedAsciiString

      @Nonnull public static String readValidatedAsciiString(String fieldName, @Nonnull MemorySegment mem, int offset, int minLength, int maxLength)
      Reads a length-prefixed ASCII string from a MemorySegment, validating every byte is in the ASCII range (0-127). Mirrors readValidatedAsciiString(ByteBuf, int, int, String) so FFM-emitted readers reject non-ASCII payloads instead of silently replacing bytes with ? via StandardCharsets.US_ASCII.
    • readValidatedAsciiString

      @Nonnull public static String readValidatedAsciiString(String fieldName, @Nonnull MemorySegment mem, int offset, int minLength, int maxLength, long packed)
      The packed parameter is the VarInt.getWithLength result for offset, so a caller that already decoded the prefix does not decode it twice.
    • readValidatedAsciiString

      @Nonnull public static String readValidatedAsciiString(String fieldName, @Nonnull MemorySegment mem, int offset, int maxLength)
    • requireFinite

      public static float requireFinite(float value, @Nonnull String fieldName)
      Returns value if it is finite (not NaN and not infinite), otherwise throws a ProtocolException. Apply to any float field that must carry only valid finite wire values.
    • requireFinite

      public static double requireFinite(double value, @Nonnull String fieldName)
      Returns value if it is finite, otherwise throws. See requireFinite(float, String).
    • isFinite

      public static boolean isFinite(float value)
      True if value is finite. No-throw counterpart of requireFinite(float, String), used by the structural validator.
    • isFinite

      public static boolean isFinite(double value)
      True if value is finite. No-throw counterpart of requireFinite(double, String), used by the structural validator.
    • isFinite

      public static boolean isFinite(@Nonnull org.joml.Vector2fc v)
      True if every component of v is finite.
    • isFinite

      public static boolean isFinite(@Nonnull org.joml.Vector3fc v)
      True if every component of v is finite.
    • isFinite

      public static boolean isFinite(@Nonnull org.joml.Vector4fc v)
      True if every component of v is finite.
    • isFinite

      public static boolean isFinite(@Nonnull org.joml.Quaternionfc q)
      True if every component of q is finite.
    • isFinite

      public static boolean isFinite(@Nonnull org.joml.Matrix4fc m)
      True if every one of the 16 components of m is finite.
    • requireFinite

      @Nonnull public static org.joml.Vector2fc requireFinite(@Nonnull org.joml.Vector2fc v, @Nonnull String fieldName)
      Returns v if every component is finite, otherwise throws. Composite counterpart of requireFinite(float, String).
    • requireFinite

      @Nonnull public static org.joml.Vector3fc requireFinite(@Nonnull org.joml.Vector3fc v, @Nonnull String fieldName)
      Returns v if every component is finite, otherwise throws. Composite counterpart of requireFinite(float, String).
    • requireFinite

      @Nonnull public static org.joml.Vector4fc requireFinite(@Nonnull org.joml.Vector4fc v, @Nonnull String fieldName)
      Returns v if every component is finite, otherwise throws. Composite counterpart of requireFinite(float, String).
    • requireFinite

      @Nonnull public static org.joml.Quaternionfc requireFinite(@Nonnull org.joml.Quaternionfc q, @Nonnull String fieldName)
      Returns q if every component is finite, otherwise throws. Composite counterpart of requireFinite(float, String).
    • requireFinite

      @Nonnull public static org.joml.Matrix4fc requireFinite(@Nonnull org.joml.Matrix4fc m, @Nonnull String fieldName)
      Returns m if every component is finite, otherwise throws. Composite counterpart of requireFinite(float, String).
    • utf8ByteLength

      public static int utf8ByteLength(@Nonnull String s)
      Compute the number of UTF-8 bytes needed to encode a string. This avoids actually encoding the string just to get its byte count.
    • stringSize

      public static int stringSize(@Nonnull String s)
      Compute the total wire size of a VarInt-prefixed UTF-8 string. This is VarInt.size(byteLength) + byteLength, computed without double-encoding.
    • writeFixedAsciiString

      public static void writeFixedAsciiString(@Nonnull io.netty.buffer.ByteBuf buf, @Nullable String value, int length)
    • writeFixedString

      public static void writeFixedString(@Nonnull io.netty.buffer.ByteBuf buf, @Nullable String value, int length)
    • writeVarString

      public static void writeVarString(@Nonnull io.netty.buffer.ByteBuf buf, @Nonnull String value, int maxLength)
    • writeVarAsciiString

      public static void writeVarAsciiString(@Nonnull io.netty.buffer.ByteBuf buf, @Nonnull String value, int maxLength)
    • writeVarString

      public static int writeVarString(@Nonnull MemorySegment mem, int offset, @Nonnull String value, int maxLength)
    • writeVarAsciiString

      public static int writeVarAsciiString(@Nonnull MemorySegment mem, int offset, @Nonnull String value, int maxLength)
    • readVector2f

      @Nonnull public static org.joml.Vector2f readVector2f(@Nonnull MemorySegment mem, int offset)
    • readVector3f

      @Nonnull public static org.joml.Vector3f readVector3f(@Nonnull MemorySegment mem, int offset)
    • readVector4f

      @Nonnull public static org.joml.Vector4f readVector4f(@Nonnull MemorySegment mem, int offset)
    • readQuaternionf

      @Nonnull public static org.joml.Quaternionf readQuaternionf(@Nonnull MemorySegment mem, int offset)
    • writeVector2f

      public static void writeVector2f(@Nonnull MemorySegment mem, int offset, @Nonnull org.joml.Vector2fc v)
    • writeVector3f

      public static void writeVector3f(@Nonnull MemorySegment mem, int offset, @Nonnull org.joml.Vector3fc v)
    • writeVector4f

      public static void writeVector4f(@Nonnull MemorySegment mem, int offset, @Nonnull org.joml.Vector4fc v)
    • writeQuaternionf

      public static void writeQuaternionf(@Nonnull MemorySegment mem, int offset, @Nonnull org.joml.Quaternionfc q)
    • writeFixedAsciiString

      public static void writeFixedAsciiString(@Nonnull MemorySegment mem, int offset, @Nullable String value, int length)
    • writeFixedString

      public static void writeFixedString(@Nonnull MemorySegment mem, int offset, @Nullable String value, int length)
    • readUUID

      @Nonnull public static UUID readUUID(@Nonnull io.netty.buffer.ByteBuf buf, int offset)
    • writeUUID

      public static void writeUUID(@Nonnull io.netty.buffer.ByteBuf buf, @Nonnull UUID value)
    • readUUID

      @Nonnull public static UUID readUUID(@Nonnull MemorySegment mem, int offset)
    • writeUUID

      public static void writeUUID(@Nonnull MemorySegment mem, int offset, @Nonnull UUID value)
    • halfToFloat

      public static float halfToFloat(short half)
    • floatToHalf

      public static short floatToHalf(float f)
    • writeFramedPacket

      public static void writeFramedPacket(@Nonnull Packet packet, @Nonnull Class<? extends Packet> packetClass, @Nonnull io.netty.buffer.ByteBuf out, @Nonnull io.netty.buffer.ByteBufAllocator allocator, @Nonnull PacketStatsRecorder statsRecorder)
      Write a packet with framing, using an explicit packet class for registry lookup. This is needed for CachedPacket where the actual class differs from the registered type. Wire format: [4-byte payload length] [4-byte packet ID] [payload]
      Parameters:
      packet - the packet to write
      packetClass - the class to use for registry lookup
      out - the ByteBuf to write to
      allocator - the buffer allocator to use for temporary buffers
      statsRecorder - the recorder for capturing packet statistics
    • writeFramedPacketWithInfo

      public static void writeFramedPacketWithInfo(@Nonnull Packet packet, @Nonnull PacketRegistry.PacketInfo info, @Nonnull io.netty.buffer.ByteBuf out, @Nonnull io.netty.buffer.ByteBufAllocator allocator, @Nonnull PacketStatsRecorder statsRecorder)
    • readFramedPacket

      @Nonnull public static Packet readFramedPacket(@Nonnull io.netty.buffer.ByteBuf in, int payloadLength, @Nonnull io.netty.buffer.ByteBufAllocator allocator, @Nonnull PacketStatsRecorder statsRecorder)
      Read a framed packet from a ByteBuf. The buffer should be positioned at the packet ID (after the length prefix has been consumed). Handles decompression based on packet definition. Wire format: [4-byte payload length] [4-byte packet ID] [payload]
      Parameters:
      in - the ByteBuf positioned at packet ID
      payloadLength - the payload length (not including packet ID)
      allocator - the buffer allocator to use for decompression buffers
      statsRecorder - the recorder for capturing packet statistics
      Returns:
      the deserialized packet, or null if packet ID is unknown
    • readFramedPacketWithInfo

      @Nonnull public static Packet readFramedPacketWithInfo(@Nonnull io.netty.buffer.ByteBuf in, int payloadLength, @Nonnull io.netty.buffer.ByteBufAllocator allocator, @Nonnull PacketRegistry.PacketInfo info, @Nonnull PacketStatsRecorder statsRecorder)
      Read a framed packet with pre-validated PacketInfo (packet ID and size already validated). Used by PacketDecoder for fail-fast validation - packet ID is validated before waiting for payload.
      Parameters:
      in - the ByteBuf positioned at payload start (after packet ID)
      payloadLength - the payload length (already validated against info.maxSize())
      allocator - the buffer allocator to use for decompression buffers
      info - the pre-validated packet info
      statsRecorder - the recorder for capturing packet statistics
      Returns:
      the deserialized packet
    • writeFramedPacket

      public static int writeFramedPacket(@Nonnull MemorySegment compressionContext, @Nonnull Packet packet, @Nonnull PacketRegistry.PacketInfo packetInfo, @Nonnull MemorySegment out, int offset, int packetSize, @Nonnull PacketStatsRecorder statsRecorder)
    • readFramedPacket

      @Nonnull public static Packet readFramedPacket(@Nonnull MemorySegment decompressionContext, @Nonnull MemorySegment in, int payloadLength, @Nonnull PacketStatsRecorder statsRecorder)
    • readFramedPacketWithInfo

      @Nonnull public static Packet readFramedPacketWithInfo(@Nonnull MemorySegment decompressionContext, @Nonnull MemorySegment in, int offset, int payloadLength, @Nonnull PacketRegistry.PacketInfo info, @Nonnull PacketStatsRecorder statsRecorder)