Skip to content

Instantly share code, notes, and snippets.

@derkalaender
Last active April 7, 2020 21:46
Show Gist options
  • Save derkalaender/e71348f2e92326e19a0b3f610374c7e3 to your computer and use it in GitHub Desktop.
Save derkalaender/e71348f2e92326e19a0b3f610374c7e3 to your computer and use it in GitHub Desktop.
Easy and safe Minecraft/Forge NBT dsl example
class ExampleItem : Item(Properties().group(ItemGroup.MISC)) {
/**
* root
* |- someString (String)
* |- someBoolean (Boolean)
* |- nested (Compound)
* - |- immutableString (String)
* |- nullableUUID (UUID)
* |- evenMoreNested (Compound)
* - |- someIntArray (int[])
* |- someOtherIntArray (int[])
* -
*/
inner class ExampleItemNBT(stack: ItemStack) : ItemNBTHolder(stack, stack.orCreateTag) {
var someString by string("default")
var someBoolean by boolean(false)
val nested by compound(::NestedNBT)
inner class NestedNBT(stack: ItemStack, compound: CompoundNBT) : ItemNBTHolder(stack, compound) {
val immutableString by string("I am read-only")
var nullableUUID by uuid()
val evenMoreNested by compound(::MoreNestedNBT)
inner class MoreNestedNBT(stack: ItemStack, compound: CompoundNBT) : ItemNBTHolder(stack, compound) {
var someIntArray by intArray()
var someOtherIntArray by intArray(arrayOf(1, 2, 3).toIntArray())
}
}
}
fun updateNBT() {
ExampleItemNBT(toItemStack()).apply {
someString = "Hallo"
someBoolean = true
nested {
evenMoreNested {
someIntArray = arrayOf(6, 9, 4, 2, 0).toIntArray()
}
}
}
}
fun checkNBT(): Boolean {
return GlassShardNBT(toItemStack()).run {
someString == "Hallo" && someBoolean && nested { nullableUUID != null }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment