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")
Content copied to clipboard
Serialization
You can serialize any class annotated with @Serializable
to an net.minecraft.nbt.NbtElement.
Nbt.encodeToNbtElement(value)
Content copied to clipboard
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)
Content copied to clipboard
Configuration
You can configure the Nbt
instance in the following way:
val nbt = Nbt {
encodeDefaults = true // false by default
ignoreUnknownKeys = false // true by default
}
Content copied to clipboard
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)
}
}
Content copied to clipboard
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
Content copied to clipboard
Compound write access
You can modify an net.minecraft.nbt.NbtCompound using the provided operator functions
nbtCompound["myint"] = 3
nbtCompound["coolboolean"] = false
Content copied to clipboard