Interface PersistentDataKey<C>
- Type Parameters:
C- the retrieved object type when applying this tag type
This class represents the union of a
Key and a
PersistentDataType.
The advantage of this class is more direct, typed access to data stored in a PersistentDataContainer.
Instead of having to keep track of the NamespacedKey and PersistentDataType separately
when working with PDC, this class allows you to store both together.
Example usage
public class CustomLogic {
public static final PersistentDataKey<Integer> TRACKED_VALUE = PersistentDataKey.of(
Key.key("custom:tracked_value"),
PersistentDataType.INTEGER
);
public static final PersistentDataKey<UUID> OWNING_PLAYER = PersistentDataKey.of(
Key.key("custom:owning_player"),
new UUIDTagType() // Custom type
);
// Checks if the stored PDC value for OWNING_PLAYER matches the executor UUID and adds
// 1 to the TRACKED_VALUE, if it does.
public static void incrementIfOwner(PersistentDataContainer pdc, UUID executor) {
if (!Objects.equals(pdc.get(OWNING_PLAYER), executor) {
return;
}
pdc.set(TRACKED_VALUE, pdc.getOrDefault(TRACKED_VALUE, 0) + 1);
}
}
-
Method Summary
Modifier and TypeMethodDescriptiondataType()Returns the data type of thisPersistentDataKey.key()Returns the access key of thisPersistentDataKey.static <C> PersistentDataKey<C> of(Key key, PersistentDataType<?, C> type)
-
Method Details
-
of
- Type Parameters:
C- the retrieved object type when applying this tag type- Parameters:
key- the access key to storetype- the persistent data type to reference- Returns:
- a new
PersistentDataKeyof the given key and type - Throws:
IllegalArgumentException- if either key or type are null
-
key
NamespacedKey key()Returns the access key of thisPersistentDataKey.- Returns:
- the access key of this
PersistentDataKey
-
dataType
PersistentDataType<?,C> dataType()Returns the data type of thisPersistentDataKey.- Returns:
- the data type of this
PersistentDataKey
-