SavedData is an alternative to capabilities that stores data per dimension or globally depending on how the context is passed.
The class can be broken down into two important methods:
save: Write the data to nbt. -
setDirty: Tell the game that the data has changed and needs to be saved to file.
save method need to be implemented while
setDirty should be called whenever the data is going to be manipulated. As the class is abstract, it should be subclassed with these three methods implemented.
Attaching to a Level
To attach a Saved Data to a particular level, you must have access to an instance of
ServerChunkCache. From there, you can call the method
#getSavedData which will give you an instance of the
DimensionDataStorage: the class that stores all saved data for that particular dimension. You can attach an instance or get the current instance of the data using
DimensionDataStorage#computeIfAbsent. This takes in three arguments: the first to construct a saved data with data already existing in some
CompoundTag, the second to construct a saved data with no existing data, and the third to specify the name of the file to save the data to within the
data folder in the overworld or in the specific dimension of the save.
Global attachments are the same except that they should only be attached to the overworld as that level will always persist.