You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// ItemStack provides a util method, so we can directly edit its PDC
54
+
item.editPersistentDataContainer(pdc -> {
55
+
pdc.set(key, PersistentDataType.STRING, "I love Tacos!");
44
56
});
45
57
```
46
58
@@ -49,8 +61,9 @@ item.editMeta(meta -> {
49
61
It is considered good practice to reuse `NamespacedKey` objects. They can be constructed with either:
50
62
- A [`Plugin`](jd:paper:org.bukkit.plugin.Plugin) instance and a [`String`](jd:java:java.lang.String) identifier
51
63
- A [`String`](jd:java:java.lang.String) namespace and a [`String`](jd:java:java.lang.String) identifier
64
+
- Some classes such as ItemStack and OfflinePlayer provide a read-only container "view" and other than ItemStack, OfflinePlayer has no other method to add data.
52
65
53
-
The first option is often preferred as it will automatically use the plugin's namespace; however, the second option can be used if you
66
+
The first option is often preferred as it will automatically use the plugin's name as namespace; however, the second option can be used if you
54
67
want to use a different namespace or access the data from another plugin.
55
68
56
69
:::
@@ -63,10 +76,10 @@ To get data from the PDC, you need to know the `NamespacedKey` and the `Persiste
@@ -178,6 +198,19 @@ E.g. Placing an ItemStack as a Block (with a TileState) ***does not*** copy over
178
198
Objects that can have a PDC implement the [`PersistentDataHolder`](jd:paper:org.bukkit.persistence.PersistentDataHolder) interface
179
199
and their PDC can be fetched with [`PersistentDataHolder#getPersistentDataContainer()`](jd:paper:org.bukkit.persistence.PersistentDataHolder#getPersistentDataContainer()).
- The persistent data container of an `ItemStack` has historically been accessed by
203
+
the `ItemStack`'s `ItemMeta`. This, however, includes the overhead of constructing the entire `ItemMeta`, which acts as a snapshot of the `ItemStack`'s data at the point of creation.
204
+
205
+
To avoid this overhead, ItemStack exposes a read-only view of its persistent data container at `ItemStack#getPersistentDataContainer()`.
206
+
Edits to the persistent data container can be achieved via `ItemStack#editPersistentDataContainer(Consumer)`.
207
+
The persistent data container available in the consumer is not valid outside the consumer.
208
+
```java
209
+
ItemStack itemStack =...;
210
+
itemStack.editPersistentDataContainer(pdc -> {
211
+
pdc.set(key, PersistentDataType.STRING, "I love Tacos!");
212
+
});
213
+
```
181
214
- ##### [`Chunk`](jd:paper:org.bukkit.Chunk)
182
215
- `Chunk#getPersistentDataContainer()`
183
216
- ##### [`World`](jd:paper:org.bukkit.World)
@@ -196,27 +229,12 @@ and their PDC can be fetched with [`PersistentDataHolder#getPersistentDataContai
0 commit comments