silk-nbt

Provides NBT serialization and deserialization using kotlinx.serialization. Additionally, this module contains some NBT utilities, like simple conversion extensions.

Dependency

modImplementation("net.silkmc:silk-nbt:1.10.4")

Serialization

You can serialize any class annotated with @Serializable to an net.minecraft.nbt.NbtElement.

Nbt.encodeToNbtElement(value)

Deserialization

In the same way it is possible deserialize any net.minecraft.nbt.NbtElement containing the correct entries to a serializable class.

Nbt.decodeFromNbtElement(nbtElement)

Configuration

You can configure the Nbt instance in the following way:

val nbt = Nbt {
encodeDefaults = true // false by default
ignoreUnknownKeys = false // true by default
}

DSL

An NBT DSL is also available. The DSL supports normal key value pairs, compounds and nbt lists.

nbtCompound {
put("x", 6)
put("y", 21)
put("name", "Peter")
list("stringList", listOf("jo", "yes"))
longArray("longSet", setOf(3L, 8L))
compound("inner") {
put("test", true)
}
}

Conversion

Extension functions to convert Kotlin basic types to net.minecraft.nbt.NbtElements are available as well

1.toNbt(); true.toNbt(); IntArray(3) { it }.toNbt()
// etc

Compound write access

You can modify an net.minecraft.nbt.NbtCompound using the provided operator functions

nbtCompound["myint"] = 3
nbtCompound["coolboolean"] = false

Packages

Link copied to clipboard

Contains NbtCompound access functions and conversion functions to create NbtElements

Link copied to clipboard

NBT builder DSL for NbtCompound and NbtList

Link copied to clipboard

The main and public NBT serialization API

Logic for decoding an NbtElement to a serializable class

Logic for encoding a serializable class to an NbtElement