A Kotlin multiplatform library to read/write nbt binary format in memory or filesystem, provided async dsl api for writing nbt.
zlib, gzip compression is supported
- linuxX64
- linuxArmX64
- windowsX64(mingwX64)
- macosX64
- macosArm64
- jvm(1.8 or later)
- js - nodejs/browser
// build.gradle.kts
repositories {
mavenCentral()
maven("https://repo.maven.rtast.cn/releases")
}
dependencies {
// to get latest version, goto
// https://next.pkg.rtast.cn/#/releases/cn/rtast/knbt/nbt
implementation("cn.rtast.knbt:nbt:0.3")
}
val nbtBytes: ByteArray = byteArrayOf()
// by default compression is Gzip, order is BigEndian
val parsed = nbtBytes.toNbtInput(compressionType=CompressionType.Gzip, order=NbtByteOrder.BigEndian)
.readRootNBT()
val parsed2 = cn.rtast.knbt.io.byteArrayNbtInput(nbtBytes, compressionType=CompressionType.Gzip, order=NbtByteOrder.BigEndian)Bedrock Edition level.dat has 8-bytes prefix, before parsing drop first 8-bytes
Build a nbt and write to a ByteArray
val root = nbtCompound {
"string" string "String1"
"int" int 100
"short" short 1.toShort()
"double" double 0.01
"float" float 0.1f
"byte" byte 0x01
"long" long 1L
"byteArray" byteArray byteArrayOf(0x01)
"intArray" intArray intArrayOf(1, 2, 3)
"longArray" longArray longArrayOf(0L, 1L, 2L)
"list".list(NBTType.String) {
string("Str1")
string("Str2")
string("Str3")
}
"compound" compound {
// nested compound
"int" int 100
"short" short 1.toShort()
"double" double 0.01
"float" float 0.1f
"byte" byte 0x01
"long" long 1L
}
}
val output: ByteArray = byteArrayNBTOutput(root, "", compressionType=CompressionType.Gzip, order=NbtByteOrder.BigEndian).writeRootNBT()SNBT is not supported yet
Open source with Apache2.0 license