Complete all serverbound packets at PLAY state
This commit is contained in:
153 files changed
+3636
-131
No files matched your search
@@ -56,9 +56,52 @@ public fun BytesBuffer.readUuid(): Uuid {
|
||||
public fun BytesBuffer.writeVarInt(value: Int): Unit = VarIntCodec.encode(this, value)
|
||||
public fun BytesBuffer.readVarInt(): Int = VarIntCodec.decode(this)
|
||||
|
||||
public fun BytesBuffer.writeVarLong(value: Long): Unit = VarLongCodec.encode(this, value)
|
||||
public fun BytesBuffer.readVarLong(): Long = VarLongCodec.decode(this)
|
||||
|
||||
public fun BytesBuffer.writeMcString(value: String): Unit = McStringCodec.encode(this, value)
|
||||
public fun BytesBuffer.readMcString(): String = McStringCodec.decode(this)
|
||||
|
||||
public fun BytesBuffer.readPrefixedByteArray(): ByteArray {
|
||||
val length = this.readVarInt()
|
||||
val data = this.readBytes(length)
|
||||
return data
|
||||
}
|
||||
|
||||
public fun BytesBuffer.writePrefixedByteArray(data: ByteArray) {
|
||||
this.writeVarInt(data.size)
|
||||
this.writeBytes(data)
|
||||
}
|
||||
|
||||
public fun BytesBuffer.readPrefixedVarIntArray(): List<Int> {
|
||||
val length = readVarInt()
|
||||
val list = ArrayList<Int>(length)
|
||||
repeat(length) { list.add(readVarInt()) }
|
||||
return list
|
||||
}
|
||||
|
||||
public fun BytesBuffer.writePrefixedVarIntArray(value: List<Int>) {
|
||||
writeVarInt(value.size)
|
||||
for (item in value) writeVarInt(item)
|
||||
}
|
||||
|
||||
public fun BytesBuffer.readPrefixedStringArray(): List<String> {
|
||||
val length = readVarInt()
|
||||
val list = ArrayList<String>(length)
|
||||
repeat(length) { list.add(readMcString()) }
|
||||
return list
|
||||
}
|
||||
|
||||
public fun BytesBuffer.writePrefixedStringArray(value: List<String>) {
|
||||
writeVarInt(value.size)
|
||||
for (item in value) writeMcString(item)
|
||||
}
|
||||
|
||||
public inline fun <T> BytesBuffer.writePrefixedArray(value: List<T>, writeItem: BytesBuffer.(T) -> Unit) {
|
||||
writeVarInt(value.size)
|
||||
for (item in value) writeItem(item)
|
||||
}
|
||||
|
||||
public fun ReadChannel.readPacketFrame(): BytesBuffer {
|
||||
val length = this.readVarInt()
|
||||
return this.readBytes(length).wrap()
|
||||
|
||||
@@ -34,6 +34,34 @@ public object VarIntCodec : PacketCodec<Int> {
|
||||
}
|
||||
}
|
||||
|
||||
public object VarLongCodec : PacketCodec<Long> {
|
||||
override fun encode(buffer: BytesBuffer, value: Long) {
|
||||
var v = value
|
||||
while (true) {
|
||||
if ((v and 0x7FL.inv()) == 0L) {
|
||||
buffer.writeByte(v.toByte())
|
||||
return
|
||||
}
|
||||
buffer.writeByte(((v and 0x7F) or 0x80).toByte())
|
||||
v = v ushr 7
|
||||
}
|
||||
}
|
||||
|
||||
override fun decode(buffer: BytesBuffer): Long {
|
||||
var numRead = 0
|
||||
var result = 0L
|
||||
var read: Byte
|
||||
do {
|
||||
read = buffer.readByte()
|
||||
val value = (read.toLong() and 0x7F)
|
||||
result = result or (value shl (7 * numRead))
|
||||
numRead++
|
||||
if (numRead > 10) throw IllegalArgumentException("VarLong is too big")
|
||||
} while ((read.toInt() and 0x80) != 0)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
public object McStringCodec : PacketCodec<String> {
|
||||
override fun encode(buffer: BytesBuffer, value: String) {
|
||||
val bytes = value.encodeToByteArray()
|
||||
|
||||
Reference in New Issue
Block a user