Allows you to persistently store any (serializable) data on chunks, entities (including players), worlds and more.
If the component is currently loaded, the data is kept in memory as well for faster access.
Define a compound key
A compound key is required to write and write values to a persistent compound. These read and write operations can then happen type-safe thanks to the compound key.
Such a key can be created in the following way:
val personKey = compoundKey<Person>(identifier)
Person must be serializable here
Other compound key types
There are other compound key types:
nbtElementCompoundKey for storing NbtElements, as they don't have to (and cannot) be serialized
customCompoundKey for defining custom serialization logic (object to net.minecraft.nbt.NbtElement), for example if the class you are storing is not serializable, or if it is significantly faster than the built-in NBT serialization of silk-nbt
Retrieve a persistent compound
A compound can be retrieved from any compound provider, currently these are net.minecraft.world.chunk.Chunk, net.minecraft.entity.Entity and net.minecraft.world.World.
For easy access, use the
provider.persistentCompound extension value:
Use a persistent compound
Currently, persistent compounds are not thread safe, you should only modify them on the main game / server thread.
You can write to a compound using the index operator.
it[personKey] = Person("John", 32, "France")
You can read from a compound using the index operator.
val person = it[personKey]
// and then do something with it
Compounds offer more functions for more specific operations, see PersistentCompound for a list of these functions.