Formatted code
This commit is contained in:
14 files changed
+98
-95
No files matched your search
@@ -1,7 +1,7 @@
|
||||
# libmc
|
||||
|
||||
A lightweight minecraft related library, such as rcon client, motd ping and more for kotlin multiplatform and java,
|
||||
no dependencies in `jvm` target except `kotlin-stdlib`
|
||||
A lightweight minecraft related library, such as rcon client, motd ping and more for kotlin multiplatform and java, no
|
||||
dependencies in `jvm` target except `kotlin-stdlib`
|
||||
|
||||
native platforms required `ktor-network` and `kotlinx-io`
|
||||
|
||||
@@ -60,9 +60,10 @@ void main() {
|
||||
|
||||
## Other resources
|
||||
|
||||
> An example of java ping json response can be found at [ping-response-example](example/java-ping-response.json) (Formatted),
|
||||
> a raw response of bedrock ping response can be found at [bedrock-pinng-raw-response](example/bedrock-pinng-raw-response.txt)
|
||||
|
||||
> An example of java ping json response can be found at [ping-response-example](example/java-ping-response.json)
|
||||
> (Formatted),
|
||||
> a raw response of bedrock ping response can be found
|
||||
> at [bedrock-pinng-raw-response](example/bedrock-pinng-raw-response.txt)
|
||||
|
||||
# rcon client
|
||||
|
||||
|
||||
@@ -3,6 +3,5 @@ kotlin.native.ignoreDisabledTargets=true
|
||||
kotlin.native.enableKlibsCrossCompilation=true
|
||||
kotlin.daemon.jvmargs=-Xmx2048M
|
||||
org.gradle.jvmargs=-Xmx3g -Dfile.encoding=UTF-8
|
||||
|
||||
libVersion=0.1.0
|
||||
protocolVersion=26.2
|
||||
@@ -7,8 +7,6 @@
|
||||
|
||||
package cn.rtast.libmc.common
|
||||
|
||||
import kotlin.uuid.Uuid
|
||||
|
||||
public expect class BytesBuffer {
|
||||
public constructor()
|
||||
public constructor(bytes: ByteArray)
|
||||
@@ -42,82 +40,6 @@ public expect class BytesBuffer {
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
public inline fun ByteArray.wrap(): BytesBuffer = BytesBuffer(this)
|
||||
|
||||
public fun BytesBuffer.writeUuid(uuid: Uuid): Unit = uuid.toLongs { mostSignificantBits, leastSignificantBits ->
|
||||
this.writeLong(mostSignificantBits)
|
||||
this.writeLong(leastSignificantBits)
|
||||
}
|
||||
|
||||
public fun BytesBuffer.readUuid(): Uuid {
|
||||
val most = this.readLong()
|
||||
val least = this.readLong()
|
||||
return Uuid.fromLongs(most, least)
|
||||
}
|
||||
|
||||
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.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)
|
||||
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()
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
package cn.rtast.libmc.common
|
||||
|
||||
import kotlin.uuid.Uuid
|
||||
|
||||
public object VarIntCodec : PacketCodec<Int> {
|
||||
override fun encode(buffer: BytesBuffer, value: Int) {
|
||||
var v = value
|
||||
@@ -95,4 +97,80 @@ public inline fun <T> BytesBuffer.writeIdOrX(value: IdOrX<T>, writeX: BytesBuffe
|
||||
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)
|
||||
}
|
||||
|
||||
public fun BytesBuffer.writeUuid(uuid: Uuid): Unit = uuid.toLongs { mostSignificantBits, leastSignificantBits ->
|
||||
this.writeLong(mostSignificantBits)
|
||||
this.writeLong(leastSignificantBits)
|
||||
}
|
||||
|
||||
public fun BytesBuffer.readUuid(): Uuid {
|
||||
val most = this.readLong()
|
||||
val least = this.readLong()
|
||||
return Uuid.fromLongs(most, least)
|
||||
}
|
||||
|
||||
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.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)
|
||||
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)
|
||||
}
|
||||
@@ -10,7 +10,7 @@ package cn.rtast.libmc.common.packet
|
||||
/**
|
||||
* reserved packet
|
||||
*/
|
||||
public data class UnknownPacket(val packetId: Int, val data: ByteArray): MinecraftPacket {
|
||||
public data class UnknownPacket(val packetId: Int, val data: ByteArray) : MinecraftPacket {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other == null || this::class != other::class) return false
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
package cn.rtast.libmc.common
|
||||
|
||||
import io.ktor.utils.io.*
|
||||
import io.ktor.utils.io.bits.reverseByteOrder
|
||||
import io.ktor.utils.io.bits.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
public actual class ReadChannel(private val _readChannel: ByteReadChannel) {
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
|
||||
package cn.rtast.libmc.mcping.bedrock
|
||||
|
||||
import cn.rtast.libmc.common.PacketCodec
|
||||
import cn.rtast.libmc.common.BytesBuffer
|
||||
import cn.rtast.libmc.common.PacketCodec
|
||||
import kotlin.random.Random
|
||||
|
||||
private val RAKNET_MAGIC = byteArrayOf(
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
|
||||
package cn.rtast.libmc.mcping.bedrock
|
||||
|
||||
import cn.rtast.libmc.common.PacketCodec
|
||||
import cn.rtast.libmc.common.BytesBuffer
|
||||
import cn.rtast.libmc.common.PacketCodec
|
||||
import cn.rtast.libmc.common.UdpSocket
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -95,7 +95,7 @@ public data class BedrockPingResponse(
|
||||
* latency ms
|
||||
* reserved not exists in response packet
|
||||
*/
|
||||
val latency: Int
|
||||
val latency: Int,
|
||||
)
|
||||
|
||||
internal fun PingResponse.parseBedrockPingResponse(): BedrockPingResponse {
|
||||
|
||||
+3
-2
@@ -7,8 +7,9 @@
|
||||
|
||||
package cn.rtast.libmc.protocol.packet.configuration.clientbound
|
||||
|
||||
import cn.rtast.libmc.common.*
|
||||
import cn.rtast.libmc.protocol.protocol.util.readNetworkNBTCompound
|
||||
import cn.rtast.libmc.common.BytesBuffer
|
||||
import cn.rtast.libmc.common.PacketCodec
|
||||
import cn.rtast.libmc.common.readUuid
|
||||
import kotlin.uuid.Uuid
|
||||
|
||||
public data class ClientboundRemoveResourcePackPacket(val uuid: Uuid) : ClientboundConfigurationPacket {
|
||||
|
||||
+3
-1
@@ -10,9 +10,11 @@ package cn.rtast.libmc.protocol.packet.play.clientbound
|
||||
import cn.rtast.libmc.common.BytesBuffer
|
||||
import cn.rtast.libmc.common.PacketCodec
|
||||
import cn.rtast.libmc.common.readVarInt
|
||||
import cn.rtast.libmc.protocol.protocol.game.*
|
||||
import cn.rtast.libmc.protocol.protocol.game.GameMode
|
||||
import cn.rtast.libmc.protocol.protocol.game.Identifier
|
||||
import cn.rtast.libmc.protocol.protocol.game.block.BlockPos
|
||||
import cn.rtast.libmc.protocol.protocol.game.block.readBlockPos
|
||||
import cn.rtast.libmc.protocol.protocol.game.readIdentifier
|
||||
|
||||
public data class ClientboundLoginPlayPacket(
|
||||
val entityId: Int,
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ public data class ServerboundSpectatorActionPacket(
|
||||
* If 0, the player was not targeting an entity.
|
||||
* Otherwise, the ID of the targeted entity plus 1.
|
||||
*/
|
||||
val entityId: Int
|
||||
val entityId: Int,
|
||||
) : MinecraftPacket {
|
||||
public companion object Codec : PacketCodec<ServerboundSpectatorActionPacket> {
|
||||
override fun encode(buffer: BytesBuffer, value: ServerboundSpectatorActionPacket) {
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ public data class ServerboundUseItemOnPacket(
|
||||
/**
|
||||
* see [cn.rtast.libmc.protocol.packet.play.clientbound.ClientboundAcknowledgeBlockChangePacket]
|
||||
*/
|
||||
val sequence: Int
|
||||
val sequence: Int,
|
||||
) : MinecraftPacket {
|
||||
public companion object Codec : PacketCodec<ServerboundUseItemOnPacket> {
|
||||
override fun encode(buffer: BytesBuffer, value: ServerboundUseItemOnPacket) {
|
||||
|
||||
+1
-1
@@ -7,8 +7,8 @@
|
||||
|
||||
package cn.rtast.libmc.protocol.protocol.game.block
|
||||
|
||||
import cn.rtast.libmc.common.PacketCodec
|
||||
import cn.rtast.libmc.common.BytesBuffer
|
||||
import cn.rtast.libmc.common.PacketCodec
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user