Difference between revisions of "Saved Data"

From Forge Community Wiki
m (ChampionAsh5357 moved page Saved Data to Saved Data: Class rename)
(Update to 1.17)
 
Line 1: Line 1:
World Saved Data is an alternative to capabilities that stores data per dimension or globally depending on how the context is passed.
+
<code>SavedData</code> is an alternative to capabilities that stores data per dimension or globally depending on how the context is passed.
  
 
==Class Structure==
 
==Class Structure==
  
The class can be broken down into three important methods:
+
The class can be broken down into two important methods:
  - <code>read</code>: Read the stored nbt data.
+
  - <code>save</code>: Write the data to nbt.
- <code>write</code>: Write the object to nbt.
+
  - <code>setDirty</code>: Tell the game that the data has changed and needs to be saved to file.
  - <code>markDirty</code>: Tell the object to save the data to file.
 
  
The <code>read</code> and <code>write</code> methods need to be implemented while <code>markDirty</code> 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.
+
The <code>save</code> method need to be implemented while <code>setDirty</code> 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.
  
The super call requires a <code>String</code> argument. This is used to denote the name of the data file within the dimension itself. For example, if the argument was "pipes", then a file would be created within each dimension folder called <code>data/pipes.dat</code>.
+
==Attaching to a Level ==
  
==Attaching to a Dimension==
+
To attach a Saved Data to a particular level, you must have access to an instance of <code>ServerLevel</code> or <code>ServerChunkCache</code>. From there, you can call the method <code>#getSavedData</code> which will give you an instance of the <code>DimensionDataStorage</code>: 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 <code>DimensionDataStorage#computeIfAbsent</code>. This takes in three arguments: the first to construct a saved data with data already existing in some <code>CompoundTag</code>, 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 <code>data</code> folder in the overworld or in the specific dimension of the save.
  
To attach a World Saved Data to a particular dimension, you must have access to an instance of <code>ServerWorld</code> or <code>ServerChunkProvider</code>. From there, you can call the method <code>#getSavedData</code> which will give you an instance of the <code>DimensionSavedDataManager</code>: 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 <code>DimensionSavedDataManager#getOrCreate</code>. This should be used in most cases to get an instance of the specified data or create a new one if not available. From there, you can operate on your saved data as if it was a capability.
+
Global attachments are the same except that they should only be attached to the overworld as that level will always persist.
 
 
Global attachments are the same by either synchronizing the data between the three worlds or using the same object reference stored in each world.
 
  
  
 
[[Category:Data Storage]]
 
[[Category:Data Storage]]

Latest revision as of 21:01, 2 August 2021

SavedData is an alternative to capabilities that stores data per dimension or globally depending on how the context is passed.

Class Structure

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.

The 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 ServerLevel or 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.