BenWoodworth / knbt

Kotlin NBT library for kotlinx.serialization
GNU Lesser General Public License v3.0
72 stars 2 forks source link

Make NbtTags mutable #15

Open Gaming32 opened 1 year ago

Gaming32 commented 1 year ago

It would be extremely useful to have data structure-based tags (such as NbtCompound) be mutable.

BenWoodworth commented 1 year ago

I was actually planning to add mutable versions when I originally started designing the API. I decided against it and probably won't because I'm trying to closely mirror kotlinx.serialization's API. JsonElement, for example

If they ever do add mutable versions (and it might be worth opening an issue/discussion about it) then 100% I'll add those right in!

BenWoodworth commented 1 year ago

What's your use case for them? I can think of a few possible alternatives, depending on what you're trying to do.

Gaming32 commented 1 year ago

My use case is using NBT data similar to how Minecraft uses NBT data. Notably, ItemStack mutates the item data NBT. I was able to make my thing work by creating a second set of classes (one for each NbtTag subclass) that are mutable and can be converted to and from NbtTag. However creating a mutable deep copy doesn't feel like the best way.

Gaming32 commented 1 year ago

Also, yes, JsonElement is immutable. I do think that JSON isn't modified directly as much though, as it's usually used purely for serialization. NBT, on the other hand, is often used to store arbitrary dynamic data that has less of a static format.