Skip to content

Commit b6bc21d

Browse files
committed
chore: update pdc docs
1 parent 1954c2f commit b6bc21d

File tree

1 file changed

+47
-29
lines changed
  • src/content/docs/paper/dev/api

1 file changed

+47
-29
lines changed

src/content/docs/paper/dev/api/pdc.md

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ slug: paper/dev/pdc
77
The Persistent Data Container (PDC) is a way to store custom data on a whole range of objects; such as items, entities, and block entities.
88
The full list of classes that support the PDC are:
99

10+
- [`ItemStack`](#itemstack)
1011
- [`Chunk`](#chunk)
1112
- [`World`](#world)
1213
- [`Entity`](#entity)
1314
- [`TileState`](#tilestate)
1415
- [`Structure`](#structure)
15-
- [`ItemMeta`](#itemmeta)
1616
- [`GeneratedStructure`](#generatedstructure)
1717
- [`Raid`](#raid)
1818
- [`OfflinePlayer`](#offlineplayer)
19-
- [`ItemStack`](#itemstack)
19+
- [`ItemMeta`](#itemmeta)
2020

2121
## What is it used for?
2222
In the past, developers resorted to a variety of methods to store custom data on objects:
@@ -37,10 +37,22 @@ which is the object you want to store the data on. The third is the data itself.
3737
// Create a NamespacedKey
3838
NamespacedKey key = new NamespacedKey(pluginInstance, "example-key");
3939

40-
ItemStack item = ItemStack.of(Material.DIAMOND);
41-
// ItemMeta implements PersistentDataHolder, so we can get the PDC from it
42-
item.editMeta(meta -> {
43-
meta.getPersistentDataContainer().set(key, PersistentDataType.STRING, "I love Tacos!");
40+
World world = Bukkit.getServer().getWorlds().getFirst();
41+
42+
PersistentDataContainer pdc = world.getPersistentDataContainer();
43+
44+
pdc.set(key, PersistentDataType.STRING, "I love Tacos!");
45+
```
46+
47+
[`ItemStack`](jd:paper:org.bukkit.inventory.ItemStack) however doesn't have this method and instead requires you to use it's builder-style consumer:
48+
49+
```java
50+
NamespacedKey key = ...; // Retrieve the key from before
51+
52+
ItemStack item = ItemType.DIAMOND.createItemStack();
53+
// 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!");
4456
});
4557
```
4658

@@ -49,8 +61,9 @@ item.editMeta(meta -> {
4961
It is considered good practice to reuse `NamespacedKey` objects. They can be constructed with either:
5062
- A [`Plugin`](jd:paper:org.bukkit.plugin.Plugin) instance and a [`String`](jd:java:java.lang.String) identifier
5163
- 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.
5265

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
5467
want to use a different namespace or access the data from another plugin.
5568

5669
:::
@@ -63,10 +76,10 @@ To get data from the PDC, you need to know the `NamespacedKey` and the `Persiste
6376
NamespacedKey key = new NamespacedKey(pluginInstance, "example-key");
6477

6578
ItemStack item = ...; // Retrieve the item from before
66-
// Get the data from the PDC
67-
PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer();
68-
if (container.has(key, PersistentDataType.STRING)) {
69-
String value = container.get(key, PersistentDataType.STRING);
79+
// Get the data from the PDC. Do note that ItemStack provides a "view", which is read-only
80+
PersistentDataContainerView containerView = item.getPersistentDataContainer();
81+
if (containerView.has(key, PersistentDataType.STRING)) {
82+
String value = containerView.get(key, PersistentDataType.STRING);
7083
// Do something with the value
7184
player.sendMessage(Component.text(value));
7285
}
@@ -124,7 +137,14 @@ The `PersistentDataType`'s job is to "deconstruct" a complex data type into some
124137
Here is an example of how to do that for a UUID:
125138

126139
```java title="UUIDDataType.java"
140+
@NullMarked
127141
public class UUIDDataType implements PersistentDataType<byte[], UUID> {
142+
143+
public static final UUIDDataType INSTANCE = new UUIDDataType();
144+
145+
// We just need a singleton, so there's no need to allow instantiation
146+
private UUIDDataType() {}
147+
128148
@Override
129149
public Class<byte[]> getPrimitiveType() {
130150
return byte[].class;
@@ -160,7 +180,7 @@ In order to use your own `PersistentDataType`, you must pass an instance of it t
160180
[`set`](jd:paper:org.bukkit.persistence.PersistentDataContainer#set(org.bukkit.NamespacedKey,org.bukkit.persistence.PersistentDataType,C))/
161181
[`has`](jd:paper:io.papermc.paper.persistence.PersistentDataContainerView#has(org.bukkit.NamespacedKey,org.bukkit.persistence.PersistentDataType)) methods.
162182
```java
163-
container.set(key, new UUIDDataType(), uuid);
183+
container.set(key, UUIDDataType.INSTANCE, uuid);
164184
```
165185

166186
:::
@@ -178,6 +198,19 @@ E.g. Placing an ItemStack as a Block (with a TileState) ***does not*** copy over
178198
Objects that can have a PDC implement the [`PersistentDataHolder`](jd:paper:org.bukkit.persistence.PersistentDataHolder) interface
179199
and their PDC can be fetched with [`PersistentDataHolder#getPersistentDataContainer()`](jd:paper:org.bukkit.persistence.PersistentDataHolder#getPersistentDataContainer()).
180200

201+
- ##### [`ItemStack`](jd:paper:org.bukkit.inventory.ItemStack)
202+
- 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+
```
181214
- ##### [`Chunk`](jd:paper:org.bukkit.Chunk)
182215
- `Chunk#getPersistentDataContainer()`
183216
- ##### [`World`](jd:paper:org.bukkit.World)
@@ -196,27 +229,12 @@ and their PDC can be fetched with [`PersistentDataHolder#getPersistentDataContai
196229
```
197230
- ##### [`Structure`](jd:paper:org.bukkit.structure.Structure)
198231
- `Structure#getPersistentDataContainer()`
199-
- ##### [`ItemMeta`](jd:paper:org.bukkit.inventory.meta.ItemMeta)
200-
- `ItemMeta#getPersistentDataContainer()`
201232
- ##### [`GeneratedStructure`](jd:paper:org.bukkit.generator.structure.GeneratedStructure)
202233
- `GeneratedStructure#getPersistentDataContainer()`
203234
- ##### [`Raid`](jd:paper:org.bukkit.Raid)
204235
- `Raid#getPersistentDataContainer()`
205236
- ##### [`OfflinePlayer`](jd:paper:org.bukkit.OfflinePlayer)
206237
- OfflinePlayer only exposes a read-only version of the persistent data container.
207238
It can be accessed via `OfflinePlayer#getPersistentDataContainer()`.
208-
- ##### [`ItemStack`](jd:paper:org.bukkit.inventory.ItemStack)
209-
- The persistent data container of an `ItemStack` has historically been accessed by
210-
the `ItemStack`'s `ItemMeta`. This, however, includes the overhead of constructing the entire
211-
`ItemMeta`, which acts as a snapshot of the `ItemStack`'s data at the point of creation.
212-
213-
To avoid this overhead, ItemStack exposes a read-only view of its persistent data container at
214-
`ItemStack#getPersistentDataContainer()`.
215-
Edits to the persistent data container can be achieved via `ItemStack#editPersistentDataContainer(Consumer)`.
216-
The persistent data container available in the consumer is not valid outside the consumer.
217-
```java
218-
ItemStack itemStack = ...;
219-
itemStack.editPersistentDataContainer(pdc -> {
220-
pdc.set(key, PersistentDataType.STRING, "I love Tacos!");
221-
});
222-
```
239+
- ##### [`ItemMeta`](jd:paper:org.bukkit.inventory.meta.ItemMeta)
240+
- `ItemMeta#getPersistentDataContainer()`

0 commit comments

Comments
 (0)