Skip to content

Commit 3e398c2

Browse files
authored
API 8 Data (Part 2) (#1010)
Co-authored-by: mosemister <mosemister@users.noreply.github.com>
1 parent a5c62a9 commit 3e398c2

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

source/plugin/data/customdata.rst

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Custom Data
1313
org.spongepowered.api.data.persistence.DataQuery
1414
org.spongepowered.api.data.persistence.DataSerializable
1515
org.spongepowered.api.data.persistence.DataStore
16+
org.spongepowered.api.data.DataProvider
1617
org.spongepowered.api.data.value.MapValue
1718
org.spongepowered.api.data.value.Value
1819
org.spongepowered.api.entity.Entity
@@ -99,12 +100,15 @@ the :javadoc:`DataBuilder` which can be implemented as follows:
99100
Registration
100101
============
101102

102-
Registering your ``DataSerializable`` allows it to be accessible by Sponge and by other plugins in a generic way. The
103-
game/plugin can create copies of your data and serialize/deserialize your data without referencing any of your classes
104-
directly.
103+
All data needs to be registered before the Minecraft registers freeze, therefore the event of :javadoc:`RegisterDataEvent`
104+
exsits for your plugin to register custom data. Data is registered using the :javadoc:`DataRegistration` class that holds
105+
your custom :javadoc:`Key`, custom :javadoc:`DataStore` and custom :javadoc:`DataProvider`.
105106

106-
To register a ``DataSerializable`` Sponge has the :javadoc:`RegisterDataEvent` event. This will allow you to register
107-
your data with the appropriate ``DataHolder``
107+
Data is required to be registered so that your custom data can be serialized/deserialize into persistence data containers,
108+
such as Players, Entity, BlockEntity, ItemStacks, etc.
109+
110+
.. tip::
111+
Not including either the DataStore or the DataProvider is valid, however your custom data will not persist across reboots
108112

109113
Simple Custom Data
110114
==================
@@ -131,7 +135,7 @@ other developers access to your data manipulator.
131135
import org.spongepowered.api.data.Key;
132136
import org.spongepowered.api.data.value.Value;
133137
134-
ResourceKey resourceKey = ResourceKey(pluginContainer, "last_attacker_manipulator");
138+
ResourceKey resourceKey = ResourceKey(pluginContainer, "last_attacker_data");
135139
Key<? extends Value<LastAttackerDataSerilizable>> key = Key
136140
.builder()
137141
.key(resourceKey)
@@ -140,18 +144,18 @@ other developers access to your data manipulator.
140144
141145
.. warning::
142146

143-
Be sure to store your ``Key`` somewhere global so you can access it later.
147+
Retain your ``Key`` reference readily available for later access, otherwise you incur additional processing expense recreating a new `Key` each time.
144148

145149
.. tip::
146150

147-
You can register a key for a specific element within a DataSerializable
151+
You can register a key for a specific field within a DataSerializable
148152

149153
Data Store
150154
==========
151155

152-
The :javadoc:`DataStore` is used to register your ``Key`` with the appropriate ``DataHolder`` and also register
153-
any other keys you may have accessing your ``DataSerializable``. In the example below, it creates a ``DataStore``
154-
and makes it appliciable to only the :javadoc:`Entity` ``DataHolder``.
156+
The :javadoc:`DataStore` is used to register your ``Key`` with the appropriate ``DataHolder`` so that Key knows how/where to store the custom data whenever
157+
serializing/deserializing. The DataStore also is used to register any other keys you may have accessing your ``DataSerializable``. In the example below,
158+
it creates a ``DataStore`` and makes it appliciable to only the :javadoc:`Entity` ``DataHolder``.
155159

156160
.. code-block:: java
157161
@@ -197,7 +201,7 @@ Data Provider
197201

198202
For data that requires more code to be used whenever the getter, setter, deleter are used will require the use of
199203
a ``DataProvider``. With a ``dataProvider`` a plugin is able to manipulate how its data should be received, set, and
200-
deleted automatically.
204+
deleted automatically such as being stored to a external database.
201205

202206
In the following example, we will be getting the UUID from the last attacker but if there is no last attacker, then
203207
return the player's UUID instead.
@@ -230,6 +234,8 @@ Data Registration
230234

231235
The final object you will need to register your data is the :javadoc:`DataRegistration` which combines
232236
your ``Key``, ``DataStore`` and ``DataProvider`` together into a single package that you can register.
237+
Only the ``key`` is required, however not providing the ``store`` or the ``provider`` will mean that
238+
data will not persist across reboots.
233239

234240
.. code-block:: java
235241

0 commit comments

Comments
 (0)