PacketByteBufferHelper
com.jsmacrosce.jsmacros.client.api.helper.PacketByteBufferHelper
Reads and writes typed values in a packet buffer; callers must use the packet's field order and matching read/write operations.
Packet-event buffers contain encoded packet data; Client.createPacketByteBuffer() creates a fresh buffer. Changes to a packet-event buffer do not change the packet object.
reset() restores the captured bytes and reader position (normally the start for a packet-backed helper); use it when you need to reread or decode from that captured position.
Supported standalone packets can be decoded using their registered codecs. Packet-backed helpers remember their network phase; overloads taking a protocol select a phase explicitly.
Conversion reads from the current reader position, so reset after inspecting a packet before decoding its untouched bytes. receivePacket() handles the original packet object,
whereas its named and class overloads decode the buffer first.
const payload = Client.createPacketByteBuffer();
payload.writeString("hello").writeVarInt(42);
Chat.log(`${payload.readString()} and ${payload.readVarInt()}`);Constructors
new PacketByteBufferHelper
new PacketByteBufferHelper()#play phase. Captures its initial contents for reset().new PacketByteBufferHelper(FriendlyByteBuf base)#reset(). This helper has no originating packet and uses the default play phase.Parameters:
base: the non-null buffer to wrap
reset(). The current connection phase is preferred when registered for the packet; otherwise its sole registered phase is selected. Unknown packets or packets with ambiguous phases throw IllegalArgumentException; a missing standalone codec also throws IllegalArgumentException.Parameters:
packet: the non-null packet to encode
Fields
BUFFER_TO_PACKET
static BUFFER_TO_PACKET: Map<Class<? extends Packet<?>>, Function<FriendlyByteBuf, ? extends Packet<?>>>read-only#init() for packet classes with supported standalone codecs. toPacket(Class) uses it for play-phase decoding; explicit phase decoding selects the matching codec directly.Methods
canSerialize
false for null, unknown packets, ambiguous phase selection, or missing codecs; it does not test whether encoding the packet's contents will succeed.Parameters:
packet: the packet to check, ornull
clear
forEachInCollection
forEachInCollection(MethodWrapper<FriendlyByteBuf, ?, Object, ?> reader): PacketByteBufferHelperSince1.8.4#Parameters:
reader: the function to read the collection's elements from the buffer
getBoolean
Parameters:
index: the index to read from
getByte
Parameters:
index: the index to read from
getBytes
Parameters:
index: the index to start reading fromlength: the length of the array to read
getChar
Parameters:
index: the index to read from
getDouble
Parameters:
index: the index to read from
getFloat
Parameters:
index: the index to read from
getInt
Parameters:
index: the byte index to read from
getLong
Parameters:
index: the index to read from
getMedium
Parameters:
index: the byte index to read from
getNetworkStateId
Parameters:
packetClass: the class of the packet to get the id for
Parameters:
packetClass: the packet classprotocol: the protocol identifier or name
getPacketId
Parameters:
packetClass: the class of the packet to get the id for
Parameters:
packetClass: the packet classprotocol: the protocol identifier or name
getPacketName
type and accepted by packet filterers.Parameters:
packet: the packet to name
getPacketNames
type values and by filterer setType. Most encode direction in an S2C or C2S suffix, but this is a naming convention, not authoritative direction metadata; use isClientbound(Class) or isServerbound(Class) when available. Names are convenience aliases and may change. getPacketName(Packet) can return a class's simple name when that class has no registered alias.getShort
Parameters:
index: the index to read from
getUnsignedByte
Parameters:
index: the index to read from
getUnsignedInt
Parameters:
index: the byte index to read from
getUnsignedMedium
Parameters:
index: the byte index to read from
getUnsignedShort
Parameters:
index: the index to read from
init
static init(): void#IllegalStateException.isClientbound
S2C and every serverbound one with C2S, and the event's type is that same name.Parameters:
packetClass: the class to get the side for
true if the packet is clientbound, false if it is serverboundisServerbound
Parameters:
packetClass: the class to get the id for
true if the packet is serverbound, false if it is clientboundmain
PACKETS.put lines to standard output. Class-path scanning can fail with IOException; arguments are ignored.Parameters:
args: ignored command-line arguments
markReaderIndex
markWriterIndex
preferredProtocol
static preferredProtocol(Packet<?> packet, ConnectionProtocol current): String#current when registered. If no current phase applies, selects the sole registered phase; throws IllegalArgumentException for an unknown packet or when multiple phases remain.Parameters:
packet: the packet whose registered phase to selectcurrent: the current connection phase, ornull
readBitSet
readBlockHitResultHelper
readBlockPos
readBoolean
readByte
readByteArray
Parameters:
maxSize: the maximum size of the byte array to read
readBytes
Parameters:
length: the length of the array to read
readChar
readChunkHelper
null if the lookup returns no chunk.readChunkPos
readChunkSectionPos
readDate
readDouble
readEnumConstant
readerIndex
readFloat
readIdentifier
readInstant
readInt
readIntArray
maxSize.Parameters:
maxSize: the maximum size of the int array to read
readIntList
readList
Parameters:
reader: reads each element
Type Parameters:
T: the type of elements read
readLong
readLongArray
maxSize argument is ignored; the underlying reader instead rejects a length greater than the number of complete longs remaining in the buffer.Parameters:
maxSize: the maximum size of the long array to read
readMap
readMap(MethodWrapper<FriendlyByteBuf, ?, K, ?> keyReader, MethodWrapper<FriendlyByteBuf, ?, V, ?> valueReader): Map<K, V>Since1.8.4#Parameters:
keyReader: reads each map keyvalueReader: reads each map value
Type Parameters:
K: the type of map keysV: the type of map values
readMedium
readNbt
readNullable
Parameters:
reader: the function to read the value from the buffer if it's not null
Type Parameters:
T: the value type
null if it was null.See:
readOptional
Parameters:
reader: the function to read the optional value from the buffer if present
Type Parameters:
T: the value type
See:
readPublicKey
readRegistryKey
Parameters:
registry: the registry the key belongs to
Type Parameters:
T: the type of values identified by the registry key
readShort
readString
readUnsignedByte
readUnsignedInt
readUnsignedMedium
readUnsignedShort
readUuid
readVarInt
readVarLong
receivePacket
replacePacket has no bearing on it. The listener type is not checked before handling; the packet's handler receives the current client game connection, so use a packet compatible with that listener.Parameters:
packetName: the name of the packet's class that should be received
Parameters:
clazz: the class of the packet to receive
reset
resetIndices
See:
resetReaderIndex
resetWriterIndex
sendPacket
Parameters:
packetName: the name of the packet's class that should be sent
Parameters:
clazz: the class of the packet to send
setBoolean
Parameters:
index: the index to write tovalue: the value to store
setByte
Parameters:
index: the index to write tovalue: the value to store
setBytes
Parameters:
index: the index to write tobytes: the bytes to store
setChar
Parameters:
index: the index to write tovalue: the value to store
setDouble
Parameters:
index: the index to write tovalue: the value to store
setFloat
Parameters:
index: the index to write tovalue: the value to store
setIndices
Parameters:
readerIndex: the readers new indexwriterIndex: the writers new index
setInt
Parameters:
index: the byte index to write tovalue: the value to write
setLong
Parameters:
index: the index to write tovalue: the value to store
setMedium
Parameters:
index: the index to write tovalue: the value to store
setReaderIndex
Parameters:
index: the readers new index
setShort
Parameters:
index: the index to write tovalue: the value to store
setWriterIndex
Parameters:
index: the writers new index
setZero
Parameters:
index: the index to write tolength: the amount of zeros to write
skipBytes
Parameters:
length: the amount of bytes to skip
toPacket
null. A packet-backed helper decodes the original packet class using its saved network phase and the current reader position. This returns a decoded packet, not the original object carried by the event.null if no packet was used to create this helperParameters:
packetName: the name of the packet's class that should be returned
Parameters:
packetName: a convenience name from getPacketNames()protocol: the network phase
Parameters:
clazz: the class of the packet to return
Parameters:
clazz: the packet classprotocol: the network phase
toString
writeBitSet
Parameters:
bitSet: the bit set to store
writeBlockHitResult
Parameters:
hitResult: the hit result to store
writeBlockHitResult(Pos3D pos, String direction, BlockPosHelper blockPos, boolean missed, boolean insideBlock): PacketByteBufferHelperSince1.8.4#Parameters:
pos: the position of the BlockHitResultdirection: the direction of the BlockHitResultblockPos: the block pos of the BlockHitResultmissed: whether the BlockHitResult missedinsideBlock: whether the BlockHitResult is inside a block
writeBlockPos
Parameters:
pos: the block position to store
Parameters:
x: the x coordinate of the block position to storey: the y coordinate of the block position to storez: the z coordinate of the block position to store
writeBoolean
Parameters:
value: the value to store
writeByte
Parameters:
value: the value to store
writeByteArray
Parameters:
bytes: the bytes to store
writeBytes
Parameters:
bytes: the bytes to store
writeChar
Parameters:
value: the value to store
writeChunkPos
Parameters:
x: the x coordinate of the chunk to storez: the z coordinate of the chunk to store
Parameters:
chunk: the chunk to store
writeChunkSectionPos
Parameters:
chunkX: the section's x coordinatey: the section's y coordinatechunkZ: the section's z coordinate
Parameters:
chunk: the chunk whose x and z coordinates define the section positiony: the section's y coordinate
writeCollection
writeCollection(Collection<T> collection, MethodWrapper<FriendlyByteBuf, T, ?, ?> writer): PacketByteBufferHelperSince1.8.4#Parameters:
collection: the collection to writewriter: writes each element
Type Parameters:
T: the type of elements in the collection
writeDate
Parameters:
date: the date to store
writeDouble
Parameters:
value: the value to store
writeEnumConstant
Parameters:
constant: the enum constant to encode by ordinal
writeFloat
Parameters:
value: the value to store
writeGlobalPos
Parameters:
dimension: the dimension identifier; vanilla dimensions includeoverworld,the_nether, andthe_endpos: the position to store
Parameters:
dimension: the dimension identifier; vanilla dimensions includeoverworld,the_nether, andthe_endx: the x coordinate of the position to storey: the y coordinate of the position to storez: the z coordinate of the position to store
writeIdentifier
Parameters:
id: the identifier to store
writeInstant
Parameters:
instant: the instant to store
writeInt
Parameters:
value: the value to write
writeIntArray
Parameters:
ints: the int array to store
writeIntList
Parameters:
list: the list to write
writeLong
Parameters:
value: the value to store
writeLongArray
Parameters:
longs: the long array to store
writeMap
writeMap(Map<K, V> map, MethodWrapper<FriendlyByteBuf, K, ?, ?> keyWriter, MethodWrapper<FriendlyByteBuf, V, ?, ?> valueWriter): PacketByteBufferHelperSince1.8.4#Parameters:
map: the map to writekeyWriter: writes each keyvalueWriter: writes each value
Type Parameters:
K: the type of map keysV: the type of map values
writeMedium
Parameters:
value: the value to store
writeNbt
Parameters:
nbt: the nbt
writeNullable
writeNullable(Object value, MethodWrapper<FriendlyByteBuf, Object, ?, ?> writer): PacketByteBufferHelperSince1.8.4#Parameters:
value: the optional value to storewriter: the function to write the optional value if it's not null to the buffer
See:
writeOptional
writeOptional(T value, MethodWrapper<FriendlyByteBuf, T, ?, ?> writer): PacketByteBufferHelperSince1.8.4#Parameters:
value: the optional value to storewriter: the function to write the optional value if present to the buffer
Type Parameters:
T: the value type
See:
writePublicKey
Parameters:
key: the public key to store
writeRegistryKey
Parameters:
key: the key to write
writerIndex
writeShort
Parameters:
value: the value to store
writeString
Parameters:
string: the string to store
Parameters:
string: the string to storemaxLength: the maximum length of the string
writeUuid
Parameters:
uuid: the UUID to store
writeVarInt
Parameters:
i: the integer to encode as a variable-length integer
writeVarLong
Parameters:
l: the long to store
writeZero
Parameters:
length: the amount of zeros to write