Class NetByteBuf

java.lang.Object
io.netty.buffer.ByteBuf
net.minecraft.network.PacketByteBuf
alexiil.mc.lib.net.NetByteBuf
All Implemented Interfaces:
io.netty.buffer.ByteBufConvertible, io.netty.util.ReferenceCounted, Comparable<io.netty.buffer.ByteBuf>
Direct Known Subclasses:
CheckingNetByteBuf

public class NetByteBuf extends net.minecraft.network.PacketByteBuf
Special PacketByteBuf class that provides methods specific to "offset" reading and writing - like writing a single bit to the stream, and auto-compacting it with similar bits into a single byte.

In addition this overrides a number of existing methods (like writeBoolean(boolean), writeEnumConstant(Enum), writeVarInt(int), writeVarLong(long)

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
     

    Nested classes/interfaces inherited from class net.minecraft.network.PacketByteBuf

    net.minecraft.network.PacketByteBuf.PacketReader<T extends Object>, net.minecraft.network.PacketByteBuf.PacketWriter<T extends Object>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final NetByteBuf
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    final boolean
    If true then all PacketByteBuf override methods that this NetByteBuf optimises will instead just write using the normal minecraft methods, rather than the (potentially) optimised versions.

    Fields inherited from class net.minecraft.network.PacketByteBuf

    DEFAULT_MAX_STRING_LENGTH, MAX_TEXT_LENGTH
  • Constructor Summary

    Constructors
    Constructor
    Description
    NetByteBuf(io.netty.buffer.ByteBuf wrapped)
     
    NetByteBuf(io.netty.buffer.ByteBuf wrapped, boolean passthrough)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static NetByteBuf
    asNetByteBuf(io.netty.buffer.ByteBuf buf)
    Returns the given ByteBuf as NetByteBuf.
    static NetByteBuf
    asNetByteBuf(io.netty.buffer.ByteBuf buf, boolean passthrough)
    Returns the given ByteBuf as NetByteBuf, but with passthrough mode enabled.
    static NetByteBuf
    asPassthroughNetByteBuf(io.netty.buffer.ByteBuf buf)
    Returns the given ByteBuf as NetByteBuf, but with passthrough mode enabled.
    static NetByteBuf
     
    static NetByteBuf
    buffer(boolean passthrough)
     
    static NetByteBuf
    buffer(int initialCapacity)
     
    static NetByteBuf
    buffer(int initialCapacity, boolean passthrough)
     
     
     
    int
    Bit version of PacketByteBuf.readerIndex().
    int
    Bit version of PacketByteBuf.writerIndex().
    boolean
     
     
    net.minecraft.util.math.BlockPos
    Reads a BlockPos using 3 readVarInt()s rather than BlockPos.fromLong(long).
    boolean
    Reads a single boolean from some position in this buffer.
    readBytes(int length)
     
    <E extends Enum<E>>
    E
    readEnumConstant(Class<E> enumClass)
     
    int
    readFixedBits(int length)
     
    net.minecraft.util.Identifier
    Deprecated.
    Because PacketByteBuf.readIdentifier() can throw an InvalidIdentifierException, but
    net.minecraft.util.Identifier
    Like readIdentifierSafe(), but returns null instead of throwing an error if the read string was invalid.
    net.minecraft.util.Identifier
    Reads in a string, and tries to parse it as an Identifier.
    void
    Reads a "marker" for debug purposes.
    Reads a string of up to Short.MAX_VALUE length.
    int
    Reads out an integer using a variable number of bytes, assuming it was written by writeVarInt(int)
    long
     
    int
    Exposes the vanilla method for reading an unsigned integer using a variable number of bytes.
    long
    Exposes the vanilla method for reading an unsigned long integer using a variable number of bytes.
     
     
     
    writeBlockPos(net.minecraft.util.math.BlockPos pos)
    Writes out a BlockPos using 3 writeVarInt(int)s rather than BlockPos.asLong().
    writeBoolean(boolean flag)
    Writes a single boolean out to some position in this buffer.
     
    writeFixedBits(int value, int length)
    Writes a fixed number of bits out to the stream.
    writeIdentifier(net.minecraft.util.Identifier id)
     
    void
    Writes a "marker" for debug purposes.
    writeVarInt(int ival)
    Writes out an integer using a variable number of bytes.
    writeVarLong(long lval)
    Writes out a long integer using a variable number of bytes.
    Exposes the vanilla method for writing out an unsigned integer using a variable number of bytes.
    Exposes the vanilla method for writing out an unsigned long integer using a variable number of bytes.

    Methods inherited from class net.minecraft.network.PacketByteBuf

    alloc, array, arrayOffset, asReadOnly, bytesBefore, bytesBefore, bytesBefore, capacity, capacity, compareTo, copy, decode, discardReadBytes, discardSomeReadBytes, duplicate, encode, ensureWritable, ensureWritable, equals, forEachByte, forEachByte, forEachByteDesc, forEachByteDesc, forEachInCollection, getBoolean, getByte, getBytes, getBytes, getBytes, getBytes, getBytes, getBytes, getBytes, getBytes, getBytes, getChar, getCharSequence, getDouble, getFloat, getInt, getIntLE, getLong, getLongLE, getMaxValidator, getMedium, getMediumLE, getShort, getShortLE, getUnsignedByte, getUnsignedInt, getUnsignedIntLE, getUnsignedMedium, getUnsignedMediumLE, getUnsignedShort, getUnsignedShortLE, getVarIntLength, getVarLongLength, getWrittenBytes, hasArray, hashCode, hasMemoryAddress, indexOf, internalNioBuffer, isDirect, isReadable, isReadable, isReadOnly, isWritable, isWritable, markWriterIndex, maxCapacity, maxWritableBytes, memoryAddress, nioBuffer, nioBuffer, nioBufferCount, nioBuffers, nioBuffers, order, order, readableBytes, readBitSet, readBlockHitResult, readByte, readByteArray, readByteArray, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readChar, readCharSequence, readChunkPos, readChunkSectionPos, readCollection, readDate, readDouble, readEither, readerIndex, readerIndex, readFloat, readGameProfile, readGlobalPos, readInstant, readInt, readIntArray, readIntArray, readIntLE, readIntList, readItemStack, readList, readLong, readLongArray, readLongArray, readLongArray, readLongLE, readMap, readMap, readMedium, readMediumLE, readNbt, readNbt, readNullable, readOptional, readProperty, readPublicKey, readRegistryKey, readRegistryValue, readRetainedSlice, readShort, readShortLE, readSlice, readString, readText, readUnlimitedNbt, readUnsignedByte, readUnsignedInt, readUnsignedIntLE, readUnsignedMedium, readUnsignedMediumLE, readUnsignedShort, readUnsignedShortLE, readUuid, refCnt, release, release, resetWriterIndex, retain, retain, retainedDuplicate, retainedSlice, retainedSlice, setBoolean, setByte, setBytes, setBytes, setBytes, setBytes, setBytes, setBytes, setBytes, setBytes, setBytes, setChar, setCharSequence, setDouble, setFloat, setIndex, setInt, setIntLE, setLong, setLongLE, setMedium, setMediumLE, setShort, setShortLE, setZero, skipBytes, slice, slice, toString, toString, toString, touch, touch, unwrap, writableBytes, writeBitSet, writeBlockHitResult, writeByte, writeByteArray, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeChar, writeCharSequence, writeChunkPos, writeChunkSectionPos, writeCollection, writeDate, writeDouble, writeEither, writeFloat, writeGameProfile, writeGlobalPos, writeInstant, writeInt, writeIntArray, writeIntLE, writeIntList, writeItemStack, writeLong, writeLongArray, writeLongLE, writeMap, writeMedium, writeMediumLE, writeNbt, writeNullable, writeOptional, writeProperty, writePublicKey, writeRegistryKey, writeRegistryValue, writerIndex, writerIndex, writeShort, writeShortLE, writeString, writeString, writeText, writeUuid, writeZero

    Methods inherited from class io.netty.buffer.ByteBuf

    asByteBuf, getDoubleLE, getFloatLE, isContiguous, maxFastWritableBytes, readDoubleLE, readFloatLE, setDoubleLE, setFloatLE, writeDoubleLE, writeFloatLE

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • EMPTY_BUFFER

      public static final NetByteBuf EMPTY_BUFFER
    • MIN_VAR_S_INT_1_BYTE

      public static final int MIN_VAR_S_INT_1_BYTE
      See Also:
    • MAX_VAR_S_INT_1_BYTE

      public static final int MAX_VAR_S_INT_1_BYTE
      See Also:
    • MIN_VAR_S_INT_2_BYTES

      public static final int MIN_VAR_S_INT_2_BYTES
      See Also:
    • MAX_VAR_S_INT_2_BYTES

      public static final int MAX_VAR_S_INT_2_BYTES
      See Also:
    • MIN_VAR_S_INT_3_BYTES

      public static final int MIN_VAR_S_INT_3_BYTES
      See Also:
    • MAX_VAR_S_INT_3_BYTES

      public static final int MAX_VAR_S_INT_3_BYTES
      See Also:
    • MIN_VAR_S_INT_4_BYTES

      public static final int MIN_VAR_S_INT_4_BYTES
      See Also:
    • MAX_VAR_S_INT_4_BYTES

      public static final int MAX_VAR_S_INT_4_BYTES
      See Also:
    • MIN_VAR_U_INT_SMALL

      public static final int MIN_VAR_U_INT_SMALL
      See Also:
    • MAX_VAR_U_INT_1_BYTE

      public static final int MAX_VAR_U_INT_1_BYTE
      See Also:
    • MAX_VAR_U_INT_2_BYTES

      public static final int MAX_VAR_U_INT_2_BYTES
      See Also:
    • MAX_VAR_U_INT_3_BYTES

      public static final int MAX_VAR_U_INT_3_BYTES
      See Also:
    • MAX_VAR_U_INT_4_BYTES

      public static final int MAX_VAR_U_INT_4_BYTES
      See Also:
    • passthrough

      public final boolean passthrough
      If true then all PacketByteBuf override methods that this NetByteBuf optimises will instead just write using the normal minecraft methods, rather than the (potentially) optimised versions.
  • Constructor Details

    • NetByteBuf

      public NetByteBuf(io.netty.buffer.ByteBuf wrapped)
    • NetByteBuf

      public NetByteBuf(io.netty.buffer.ByteBuf wrapped, boolean passthrough)
  • Method Details

    • buffer

      public static NetByteBuf buffer()
      Returns:
      A new NetByteBuf from Unpooled.buffer()
    • buffer

      public static NetByteBuf buffer(int initialCapacity)
      Returns:
      A new NetByteBuf from Unpooled.buffer(int)
    • buffer

      public static NetByteBuf buffer(boolean passthrough)
      Returns:
      A new NetByteBuf from Unpooled.buffer()
    • buffer

      public static NetByteBuf buffer(int initialCapacity, boolean passthrough)
      Returns:
      A new NetByteBuf from Unpooled.buffer(int)
    • asNetByteBuf

      public static NetByteBuf asNetByteBuf(io.netty.buffer.ByteBuf buf)
      Returns the given ByteBuf as NetByteBuf. if the given instance is already a NetByteBuf then the given buffer is returned (note that this may result in unexpected consequences if multiple read/write Boolean methods are called on the given buffer before you called this).
    • asPassthroughNetByteBuf

      public static NetByteBuf asPassthroughNetByteBuf(io.netty.buffer.ByteBuf buf)
      Returns the given ByteBuf as NetByteBuf, but with passthrough mode enabled. if the given instance is already a NetByteBuf then the given buffer is returned (note that this may result in unexpected consequences if multiple read/write Boolean methods are called on the given buffer before you called this).
    • asNetByteBuf

      public static NetByteBuf asNetByteBuf(io.netty.buffer.ByteBuf buf, boolean passthrough)
      Returns the given ByteBuf as NetByteBuf, but with passthrough mode enabled. if the given instance is already a NetByteBuf then the given buffer is returned (note that this may result in unexpected consequences if multiple read/write Boolean methods are called on the given buffer before you called this).
    • getBitWriterIndex

      public int getBitWriterIndex()
      Bit version of PacketByteBuf.writerIndex().
    • getBitReaderIndex

      public int getBitReaderIndex()
      Bit version of PacketByteBuf.readerIndex().
    • copy

      public NetByteBuf copy()
      Overrides:
      copy in class net.minecraft.network.PacketByteBuf
    • readBytes

      public NetByteBuf readBytes(int length)
      Overrides:
      readBytes in class net.minecraft.network.PacketByteBuf
    • clear

      public NetByteBuf clear()
      Overrides:
      clear in class net.minecraft.network.PacketByteBuf
    • markReaderIndex

      public NetByteBuf markReaderIndex()
      Overrides:
      markReaderIndex in class net.minecraft.network.PacketByteBuf
    • resetReaderIndex

      public NetByteBuf resetReaderIndex()
      Overrides:
      resetReaderIndex in class net.minecraft.network.PacketByteBuf
    • saveReaderIndex

      public NetByteBuf.SavedReaderIndex saveReaderIndex()
    • resetReaderIndex

      public NetByteBuf resetReaderIndex(NetByteBuf.SavedReaderIndex index)
    • writeBoolean

      public NetByteBuf writeBoolean(boolean flag)
      Writes a single boolean out to some position in this buffer. The boolean flag might be written to a new byte (increasing the writerIndex) or it might be added to an existing byte that was written with a previous call to this method.
      Overrides:
      writeBoolean in class net.minecraft.network.PacketByteBuf
    • readBoolean

      public boolean readBoolean()
      Reads a single boolean from some position in this buffer. The boolean flag might be read from a new byte (increasing the readerIndex) or it might be read from a previous byte that was read with a previous call to this method.
      Overrides:
      readBoolean in class net.minecraft.network.PacketByteBuf
    • writeFixedBits

      public NetByteBuf writeFixedBits(int value, int length) throws IllegalArgumentException
      Writes a fixed number of bits out to the stream.
      Parameters:
      value - the value to write out.
      length - The number of bits to write.
      Returns:
      This buffer.
      Throws:
      IllegalArgumentException - if the length argument was less than 1 or greater than 32.
    • readFixedBits

      public int readFixedBits(int length) throws IllegalArgumentException
      Parameters:
      length - The number of bits to read.
      Returns:
      The read bits, compacted into an int.
      Throws:
      IllegalArgumentException - if the length argument was less than 1 or greater than 32.
    • writeEnumConstant

      public NetByteBuf writeEnumConstant(Enum<?> value)
      Overrides:
      writeEnumConstant in class net.minecraft.network.PacketByteBuf
    • readEnumConstant

      public <E extends Enum<E>> E readEnumConstant(Class<E> enumClass)
      Overrides:
      readEnumConstant in class net.minecraft.network.PacketByteBuf
    • writeBlockPos

      public NetByteBuf writeBlockPos(net.minecraft.util.math.BlockPos pos)
      Writes out a BlockPos using 3 writeVarInt(int)s rather than BlockPos.asLong().
      Overrides:
      writeBlockPos in class net.minecraft.network.PacketByteBuf
    • readBlockPos

      public net.minecraft.util.math.BlockPos readBlockPos()
      Reads a BlockPos using 3 readVarInt()s rather than BlockPos.fromLong(long).
      Overrides:
      readBlockPos in class net.minecraft.network.PacketByteBuf
    • writeVarInt

      public NetByteBuf writeVarInt(int ival)
      Writes out an integer using a variable number of bytes.

      Unlike vanilla this doesn't use 5 bytes for all negative numbers.

      Overrides:
      writeVarInt in class net.minecraft.network.PacketByteBuf
    • readVarInt

      public int readVarInt()
      Reads out an integer using a variable number of bytes, assuming it was written by writeVarInt(int)
      Overrides:
      readVarInt in class net.minecraft.network.PacketByteBuf
    • writeVarUnsignedInt

      public NetByteBuf writeVarUnsignedInt(int ival)
      Exposes the vanilla method for writing out an unsigned integer using a variable number of bytes.

      Unlike writeVarInt(int) this only uses less than 5 bytes for non-negative integers less than pow(2, 8 * 3 - 1) ()

    • readVarUnsignedInt

      public int readVarUnsignedInt()
      Exposes the vanilla method for reading an unsigned integer using a variable number of bytes.

      Unlike readVarInt() this only uses less than 5 bytes for non-negative integers less than pow(2, 8 * 3 - 1) ()

    • writeVarLong

      public NetByteBuf writeVarLong(long lval)
      Writes out a long integer using a variable number of bytes.
      • 1 byte for -64 to 63
      • 2 bytes for -8,192 to 8,191
      • 3 bytes for -1,048,576 to 1,048,575
      • 4 bytes for -134,217,728 to 134,217,727

      Unlike vanilla this doesn't use 9 bytes for all negative numbers.

      Overrides:
      writeVarLong in class net.minecraft.network.PacketByteBuf
    • readVarLong

      public long readVarLong()
      Overrides:
      readVarLong in class net.minecraft.network.PacketByteBuf
    • writeVarUnsignedLong

      public NetByteBuf writeVarUnsignedLong(long lval)
      Exposes the vanilla method for writing out an unsigned long integer using a variable number of bytes.

      Unlike writeVarInt(int) this only uses less than 9 bytes for non-negative integers less than pow(2, 8 * 7 - 1) ()

    • readVarUnsignedLong

      public long readVarUnsignedLong()
      Exposes the vanilla method for reading an unsigned long integer using a variable number of bytes.

      Unlike readVarInt() this only uses less than 9 bytes for non-negative integers less than pow(2, 8 * 7 - 1) ()

    • writeIdentifier

      public NetByteBuf writeIdentifier(net.minecraft.util.Identifier id)
      Overrides:
      writeIdentifier in class net.minecraft.network.PacketByteBuf
    • readIdentifier

      @Deprecated public net.minecraft.util.Identifier readIdentifier()
      Deprecated.
      Because PacketByteBuf.readIdentifier() can throw an InvalidIdentifierException, but
      Overrides:
      readIdentifier in class net.minecraft.network.PacketByteBuf
    • readIdentifierSafe

      public net.minecraft.util.Identifier readIdentifierSafe() throws InvalidInputDataException
      Reads in a string, and tries to parse it as an Identifier. If the string is a valid identifier then it is returned, however if it isn't then InvalidInputDataException is thrown.
      Throws:
      InvalidInputDataException - if the read string wasn't a valid Identifier.
    • readIdentifierOrNull

      @Nullable public net.minecraft.util.Identifier readIdentifierOrNull()
      Like readIdentifierSafe(), but returns null instead of throwing an error if the read string was invalid.
    • readString

      public String readString()
      Reads a string of up to Short.MAX_VALUE length.

      NOTE: This is just PacketByteBuf.readString() but available on the server as well.

      Overrides:
      readString in class net.minecraft.network.PacketByteBuf
    • isRecordingMarkers

      public boolean isRecordingMarkers()
      Returns:
      True if writeMarker(String) actually does anything.
    • writeMarker

      public void writeMarker(String id)
      Writes a "marker" for debug purposes. If debugging is disabled then this won't do anything.
    • readMarker

      public void readMarker(String id) throws InvalidInputDataException
      Reads a "marker" for debug purposes. If debugging is disabled then this won't do anything. Otherwise this will throw an exception if the marker ID's didn't match.
      Throws:
      InvalidInputDataException