Last active
April 7, 2020 21:46
-
-
Save derkalaender/e71348f2e92326e19a0b3f610374c7e3 to your computer and use it in GitHub Desktop.
Easy and safe Minecraft/Forge NBT dsl example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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