Support SNBT

This commit is contained in:
2026-09-08 12:23:55 +08:00
parent 08392f92b8
commit 74f9e6f529
22 files changed
+772 -41

No files matched your search

+10 -2
View File
@@ -1,5 +1,7 @@
# libmc
[中文](zh/README-zh.md)
A lightweight, modern Minecraft client protocol library designed for Kotlin Native & JVM. The core protocol library
module relies on the following dependencies:
@@ -19,11 +21,17 @@ module relies on the following dependencies:
# Get started
# Protocol
> `libmc-protocol` is current under development. It only supports the latest Minecraft version
> (Current supported Minecraft version: `26.2`, Protocol Version: `776`)
[Start using libmc-protocol](Get-started.md)
[Start using libmc-protocol](en/Get-started.md)
# Assemble all context APIs
[Assemble context APIs](Assemble-context.md)
[Assemble context APIs](en/Assemble-context.md)
## NBT & SNBT
[NBT & SNBT](en/NBT-SNBT.md)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+75
View File
@@ -0,0 +1,75 @@
# NBT
```kotlin
fun main() {
// build
val nbt = buildNBT {
"t_b" byte 0x01
"n" compound {
"n_s" string "STR"
"n_d" double 0.0
"n_f" float 0.1f
"n_ia" intArray intArrayOf(1, 1, 1, 1)
}
}
val buf = BytesBuffer().toNBTOutput(
NBTTag.CompoundTag(
mapOf("" to nbt)
)
).writeNBTRootCompound()
println(buf.toHexString())
// read
val levelDat: BytesBuffer = File("src/commonTest/resources/level.dat").readBytes().wrap()
val readRoot = levelDat.toNBTInput().readNBTRootCompound()
println(readRoot)
}
```
# SNBT
```kotlin
fun main() {
// parse snbt from snbt string
val raw = """{key1: 123,'key2': 'somevalue1',"key3": {subkey1: 0x1C8,"subkey2": "somevalue2"}}"""
println(snbt(raw))
// serialize snbt from NBTCompound
println(snbt(raw).toSNBT())
// build snbt
val snbt = buildSNBT {
"key1" string "TEST"
"intValue" int 1
"Count" byte 1
"Damage" int 0
}
val prettySnbt = buildSNBT(prettyPrint = true) {
"Name" string "Steve"
"Health" float 20.0f
"IsCreative" boolean true
"Pos" intArray intArrayOf(100, 64, -200)
"Custom Name" string "Alex\nWith Newline"
"Attributes" compound {
"AttackDamage" double 5.5
"MovementSpeed" float 0.1f
}
"Inventory" list {
compound {
"id" string "minecraft:diamond_sword"
"Count" byte 1
}
compound {
"id" string "minecraft:apple"
"Count" byte 16
}
}
}
println(snbt) // SNBT String
println(prettySnbt) // SNBT String
println(snbt(snbt)) // parse snbt string to NBTCompound
println(snbt(prettySnbt)) // parse snbt string to NBTCompound
}
```
+6 -2
View File
@@ -8,7 +8,7 @@
## 需要实现的API
| 名称 | 是否必须实现 | 描述 |
| 名称 | 是否必须实现 | 额外说明 |
|:-------------|:-------------|:-----------------------------------------------------------------------------------------------------------------------------------------------|
| TCP Socket | 是 | `protocol` 模块没有内置TCP Socket实现. [实现 TCP Socket](Impl-TCP-Socket-zh.md) |
| AES-128-CFB8 | 视情况而定 | 仅在登录开启了正版验证(`online-mode`)的服务器时需要, 用于加密/解密流量. [实现AES-128-CFB8](Impl-Crypto-zh.md#AES-128-CFB8) |
@@ -25,4 +25,8 @@
# 将实现的API组合起来
[将所有上下文API组合](Assemble-context-zh.md)
[将所有上下文API组合](Assemble-context-zh.md)
# NBT & SNBT
[NBT & SNBT](../en/NBT-SNBT.md)