Complete all serverbound packets
This commit is contained in:
43 files changed
+1002
-100
No files matched your search
@@ -73,6 +73,22 @@ public fun BytesBuffer.writePrefixedByteArray(data: ByteArray) {
|
||||
this.writeBytes(data)
|
||||
}
|
||||
|
||||
public fun BytesBuffer.readOptionalPrefixedByteArray(): ByteArray? {
|
||||
val isPresent = this.readBoolean()
|
||||
return if (isPresent) {
|
||||
val length = this.readVarInt()
|
||||
this.readBytes(length)
|
||||
} else null
|
||||
}
|
||||
|
||||
public fun BytesBuffer.writeOptionalPrefixedByteArray(data: ByteArray?) {
|
||||
if (data != null) {
|
||||
this.writeBoolean(true)
|
||||
this.writeVarInt(data.size)
|
||||
this.writeBytes(data)
|
||||
} else this.writeBoolean(false)
|
||||
}
|
||||
|
||||
public fun BytesBuffer.readPrefixedVarIntArray(): List<Int> {
|
||||
val length = readVarInt()
|
||||
val list = ArrayList<Int>(length)
|
||||
|
||||
@@ -74,4 +74,25 @@ public object McStringCodec : PacketCodec<String> {
|
||||
val bytes = buffer.readBytes(length)
|
||||
return bytes.decodeToString()
|
||||
}
|
||||
}
|
||||
|
||||
public sealed interface IdOrX<out T> {
|
||||
public data class Inline<T>(val value: T) : IdOrX<T>
|
||||
public data class Reference(val registryId: Int) : IdOrX<Nothing>
|
||||
}
|
||||
|
||||
public inline fun <T> BytesBuffer.writeIdOrX(value: IdOrX<T>, writeX: BytesBuffer.(T) -> Unit) {
|
||||
when (value) {
|
||||
is IdOrX.Inline -> {
|
||||
this.writeVarInt(0)
|
||||
this.writeX(value.value)
|
||||
}
|
||||
|
||||
is IdOrX.Reference -> this.writeVarInt(value.registryId + 1)
|
||||
}
|
||||
}
|
||||
|
||||
public inline fun <T> BytesBuffer.readIdOrX(readX: BytesBuffer.() -> T): IdOrX<T> {
|
||||
val id = this.readVarInt()
|
||||
return if (id == 0) IdOrX.Inline(this.readX()) else IdOrX.Reference(id - 1)
|
||||
}
|
||||
Reference in New Issue
Block a user