Fix network nbt reader
This commit is contained in:
8 files changed
+27
-13
No files matched your search
@@ -27,6 +27,7 @@ public expect class BytesBuffer {
|
||||
public fun readLong(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Long
|
||||
public fun readBytes(length: Int): ByteArray
|
||||
public fun readBoolean(): Boolean
|
||||
public fun readRemainingBytes(): ByteArray
|
||||
|
||||
public fun toByteArray(): ByteArray
|
||||
public fun hasRemaining(): Boolean
|
||||
|
||||
@@ -29,13 +29,8 @@ public class PacketRegistry {
|
||||
register(id, T::class, codec)
|
||||
}
|
||||
|
||||
public fun decodePacket(packetId: Int, buffer: BytesBuffer): MinecraftPacket {
|
||||
val codec = idToCodec[packetId]
|
||||
if (codec != null) return codec.decode(buffer) else {
|
||||
// println("Ignored unknown packet 0x${packetId.toString(16).uppercase()}")
|
||||
return UnknownPacket(packetId, buffer.readBytes(buffer.remaining.toInt()))
|
||||
}
|
||||
}
|
||||
public fun decodePacket(packetId: Int, buffer: BytesBuffer): MinecraftPacket =
|
||||
idToCodec[packetId]?.decode(buffer) ?: UnknownPacket(packetId, buffer.readBytes(buffer.remaining.toInt()))
|
||||
|
||||
public fun <T : MinecraftPacket> encodePacket(buffer: BytesBuffer, packet: T) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
|
||||
@@ -93,6 +93,12 @@ public actual class BytesBuffer {
|
||||
|
||||
public actual fun readBoolean(): Boolean = this.readByte() != 0x00.toByte()
|
||||
|
||||
public actual fun readRemainingBytes(): ByteArray {
|
||||
val array = ensureReadArray()
|
||||
if (readOffset >= array.size) return byteArrayOf()
|
||||
return array.copyOfRange(readOffset, array.size)
|
||||
}
|
||||
|
||||
public actual fun toByteArray(): ByteArray = outStream.toByteArray()
|
||||
public actual fun hasRemaining(): Boolean = readOffset < ensureReadArray().size
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ public actual class BytesBuffer {
|
||||
|
||||
public actual fun readBytes(length: Int): ByteArray = _delegateBuf.readByteArray(length)
|
||||
public actual fun readBoolean(): Boolean = _delegateBuf.readByte() != 0x00.toByte()
|
||||
public actual fun readRemainingBytes(): ByteArray = this.toByteArray()
|
||||
|
||||
public actual fun toByteArray(): ByteArray {
|
||||
val copy = _delegateBuf.peek()
|
||||
|
||||
Reference in New Issue
Block a user