quilt-kotlin-libraries icon indicating copy to clipboard operation
quilt-kotlin-libraries copied to clipboard

NBT DSL

Open pluiedev opened this issue 2 years ago • 2 comments

This PR includes two APIs for creating, manipulating and retrieving NBT data.

The NBT creation DSL allows one to create nested NBT tags easily via an idiomatic DSL, like so:

fun sample() = buildNbtCompound {
    putCompound("explicitSyntax") {
        put("looks like this", 42.0)
    }

    "abbreviatedSyntax"("looks like that")

    "you"(3.0f)
    "can"(true)
    "put"(3.toByte())
    "a lot"(30L)
    "of"(Identifier("different data"))
    "in"(BlockPos.ORIGIN)
    "here" compound {
        "ooh"("nesting!")
        "listsAreWeird" list {
            add(40.0)
            add(2.0)
            add(30.0)
        }
    }
}

The structural access API revolves around NbtStruct, which allows users to define a data structure that gets data from and puts data into an NbtCompound. For example:

class Sample(nbt: NbtCompound): NbtStruct(nbt) {
    val elephant by int()
    val hi by double("explicitKey")
    val nested by listOfStructs(::Nested)
}

class Nested(nbt: NbtCompound): NbtStruct(nbt) {
    val hey by string()
}

The Sample struct should accept NBT data like this: (SNBT)

{elephant:33,explicitKey:42.0d,nested:[{hey:"there"},{hey:"yo"},{hey:"Jude"}]}
  • [X] NBT Creation API
  • [X] Structural Access API
    • [ ] Nullable/optional fields?
  • [X] Nullable access to NBT data
  • [ ] Finally decide on what to do with lists
  • [ ] Perhaps other APIs as well?

Closes #6.

pluiedev avatar Jun 15 '22 02:06 pluiedev

Why do classes have to extend NbtStruct? Feels quite limiting? It's not something that is needed for stuff like Kotlin's map delegations.

Kroppeb avatar Jun 16 '22 18:06 Kroppeb

Why do classes have to extend NbtStruct? Feels quite limiting? It's not something that is needed for stuff like Kotlin's map delegations.

I mean, are these meaningful outside of an NbtStruct? They seem to rely on the internal nbt field so im unsure how you would make them function without that

SilverAndro avatar Jun 30 '22 23:06 SilverAndro