#nbt compounds (1.20.1)

8 messages · Page 1 of 1 (latest)

echo plank
#

Im trying to make a nbt compound of this kind

{a:{a1:{},a2:{},a3:[{a31:""}, ... ]}, ... }

But i cannot figure out how to put a compound in a compound.

Also being my first time dealing with making nbt and reconstructing objects from nbt, i don't really know what is the best practice, so any giude/documentation/suggestion is really appreciated

(Please ping me when answering)

spice briar
#

Are you trying to get stringified nbt and load it, or are you simply trying to put an NbtCompound into an NbtCompound like in code

spice briar
# echo plank Im trying to make a nbt compound of this kind {a:{a1:{},a2:{},a3:[{a31:""}, ......

In code you'd just do

NbtCompound root = new NbtCompound();
NbtCompound nbtA = new NbtCompound();
NbtCompound nbtA1 = new NbtCompound();
NbtCompound nbtA2 = new NbtCompound();
NbtCompound nbtA3 = new NbtCompound();
// Inside a: {a1: {}, a2: {}, a3: {}}
nbtA.put("a1", nbtA1);
nbtA.put("a2", nbtA2);
nbtA.put("a3", nbtA3);
// Inside Root: {a: {a1: {}, a2: {}, a3: {}}}
root.put("a", nbtA);
echo plank
#

I have a class with a method to create an nbt compound, containing the data needed to another method to instantiate an object of that class.

My idea was to save on an item the resulting compound, so that from the item i can create the object i need using the compound

echo plank
#

What about going backward from nbt to a map or record?

spice briar
#

You'd use nbt.getCompound("some_key") to get the NbtCompound and then you can create the map depending on the data

#

I mean fundamentally it's for reading: nbt.getCompound(key) and for writing: nbt.put(key, someNbt) from there on out it's up to you with your data etc.