Skip to content

RTAkland/knbt

Repository files navigation

knbt

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

Supported platforms

  1. linuxX64
  2. linuxArmX64
  3. windowsX64(mingwX64)
  4. macosX64
  5. macosArm64
  6. jvm(1.8 or later)
  7. js - nodejs/browser

Get started

// 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")
}

Example

Parse/Read

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

Write/Build

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

SNBT is not supported yet

Open source

Open source with Apache2.0 license

About

Kotlin multiplatform nbt reader/writer, provided async dsl api for writing nbt

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages