Package de.eldoria.eldoutilities.pdc
Class PersistentDataWrapper
java.lang.Object
de.eldoria.eldoutilities.pdc.PersistentDataWrapper
A wrapper class for
PersistentDataContainer.
Provides convenient methods for interacting with the persistent data of an object.-
Constructor Summary
ConstructorsConstructorDescriptionPersistentDataWrapper(org.bukkit.persistence.PersistentDataContainer container) -
Method Summary
Modifier and TypeMethodDescription<T,Z> Z compute(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainerbased on the current value<T,Z> Z computeIfAbsent(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainerif it is not set.<T,Z> Z computeIfPresent(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainerif is set.static PersistentDataWrappercreate(org.bukkit.persistence.PersistentDataHolder holder) <T,Z> Optional<Z> get(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Get a value from a persistent data holder.<T,Z> Z getOrDefault(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.<T,Z> boolean hasKey(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer.<T,Z> void put(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer.<T,Z> boolean remove(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainerif it is set.<T,Z> void setIfAbsent(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Sets a value in a data container if it is absent.
-
Constructor Details
-
PersistentDataWrapper
public PersistentDataWrapper(org.bukkit.persistence.PersistentDataContainer container)
-
-
Method Details
-
create
-
setIfAbsent
public <T,Z> void setIfAbsent(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Sets a value in a data container if it is absent.- Type Parameters:
T- type of keyZ- type of value- Parameters:
key- key to settype- type of keyvalue- value of key
-
compute
@Nullable public <T,Z> Z compute(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainerbased on the current value- Type Parameters:
T- type of valueZ- type of value- Parameters:
key- key to computetype- type of keymap- map the current value to the new value. Current value is null if key is not set.- Returns:
- the mapped value. can be null if mapping function returns null or holder is null
-
computeIfAbsent
@Nullable public <T,Z> Z computeIfAbsent(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainerif it is not set.- Type Parameters:
T- type of valueZ- type of value- Parameters:
key- key to computetype- type of keyvalue- value which should be set if the key is not present.- Returns:
- the value associated with this key. can be null if holder is null.
-
computeIfPresent
@Nullable public <T,Z> Z computeIfPresent(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainerif is set.- Type Parameters:
T- type of valueZ- type of value- Parameters:
key- key to computetype- type of keymappingFunction- function to map the current value to the new value.- Returns:
- the value associated with this key. can be null if holder is null.
-
get
public <T,Z> Optional<Z> get(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Get a value from a persistent data holder.- Type Parameters:
T- type of valueZ- type of value- Parameters:
key- key to gettype- type of key- Returns:
- result wrapped in an optional if present.
-
getOrDefault
public <T,Z> Z getOrDefault(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.- Type Parameters:
T- type of valueZ- type of value- Parameters:
key- key to gettype- type of keydefaultValue- default value if key is absent- Returns:
- result wrapped in an optional if present.
-
put
public <T,Z> void put(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer.- Type Parameters:
T- type of valueZ- type of value- Parameters:
key- key to settype- type of keyvalue- value to set
-
hasKey
public <T,Z> boolean hasKey(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer.- Type Parameters:
T- type of valueZ- type of value- Parameters:
key- key to checktype- type of key- Returns:
- true if the key is set
-
remove
public <T,Z> boolean remove(org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainerif it is set.- Type Parameters:
T- type of valueZ- type of value- Parameters:
key- key to checktype- type of key- Returns:
- true if the key was present and removed.
-