Package de.eldoria.eldoutilities.pdc
Class DataContainerUtil
java.lang.Object
de.eldoria.eldoutilities.pdc.DataContainerUtil
Utilities to work with the
PersistentDataContainer-
Method Summary
Modifier and TypeMethodDescriptionstatic bytebooleanToByte(boolean aBoolean) Converts a boolean to a byte.static booleanbyteToBoolean(Byte aByte) Converts a byte to boolean.static <T,Z> Z compute(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<Z, Z> map) Gets thePersistentDataContainerfrom item meta, computes the value by callingcompute(PersistentDataHolder, NamespacedKey, PersistentDataType, Function)and applies the meta afterward again.static <T,Z> Z compute(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainerbased on the current valuestatic <T,Z> Z compute(org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainerbased on the current valuestatic <T,Z> Z computeIfAbsent(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainerif it is not set.static <T,Z> Z computeIfAbsent(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainerif it is not set.static <T,Z> Z computeIfAbsent(org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainerif it is not set.static <T,Z> Z computeIfPresent(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainerif is set.static <T,Z> Z computeIfPresent(@Nullable org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainerif is set.static <T,Z> Z computeIfPresent(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainerif is set.static <T,Z> Optional<Z> get(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Get a value from a persistent data holder.static <T,Z> Optional<Z> get(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Get a value from a persistent data holder.static <T,Z> Optional<Z> get(org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Get a value from a persistent data holder.static <T,Z> Z getOrDefault(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.static <T,Z> Z getOrDefault(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.static <T,Z> Z getOrDefault(org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.static <T,Z> boolean hasKey(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer.static <T,Z> boolean hasKey(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer.static <T,Z> boolean hasKey(org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer.static <T,Z> void putValue(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer.static <T,Z> void putValue(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer.static <T,Z> void putValue(org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer.static <T,Z> boolean remove(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainerif it is set.static <T,Z> boolean remove(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainerif it is set.static <T,Z> boolean remove(org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainerif it is set.static <T,Z> void setIfAbsent(@Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Sets a value in a data container if it is absent.static <T,Z> void setIfAbsent(@Nullable org.bukkit.persistence.PersistentDataHolder holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Sets a value in a data container if it is absent.static <T,Z> void setIfAbsent(org.bukkit.persistence.PersistentDataContainer container, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Z value) Sets a value in a data container if it is absent.
-
Method Details
-
setIfAbsent
public static <T,Z> void setIfAbsent(org.bukkit.persistence.PersistentDataContainer container, 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:
container- thePersistentDataContainerkey- key to settype- type of keyvalue- value of key
-
setIfAbsent
public static <T,Z> void setIfAbsent(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- key to settype- type of keyvalue- value of key
-
setIfAbsent
public static <T,Z> void setIfAbsent(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, 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:
holder- holder of thePersistentDataContainerkey- key to settype- type of keyvalue- value of key
-
compute
@Nullable public static <T,Z> Z compute(org.bukkit.persistence.PersistentDataContainer container, 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:
container- holder of thePersistentDataContainerkey- 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
-
compute
@Nullable public static <T,Z> Z compute(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- 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
-
compute
@Nullable public static <T,Z> Z compute(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, org.bukkit.NamespacedKey key, org.bukkit.persistence.PersistentDataType<T, Z> type, Function<Z, Z> map) Gets thePersistentDataContainerfrom item meta, computes the value by callingcompute(PersistentDataHolder, NamespacedKey, PersistentDataType, Function)and applies the meta afterward again.- Type Parameters:
T- type of valueZ- type of value- Parameters:
holder- item stack to change. This causes the item meta to be reapplied after computation of the value.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
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfAbsent(org.bukkit.persistence.PersistentDataContainer container, 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:
container- holder of thePersistentDataContainerkey- 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.
-
computeIfAbsent
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfAbsent(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- 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.
-
computeIfAbsent
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfAbsent(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, 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:
holder- holder of thePersistentDataContainerkey- 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
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfPresent(@Nullable @Nullable org.bukkit.persistence.PersistentDataContainer container, 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:
container- thePersistentDataContainerkey- 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.
-
computeIfPresent
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfPresent(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- 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.
-
computeIfPresent
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfPresent(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, 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:
holder- holder of thePersistentDataContainerkey- 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 static <T,Z> Optional<Z> get(org.bukkit.persistence.PersistentDataContainer container, 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:
container- holder of thePersistentDataContainerkey- key to gettype- type of key- Returns:
- result wrapped in an optional if present.
-
get
public static <T,Z> Optional<Z> get(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- key to gettype- type of key- Returns:
- result wrapped in an optional if present.
-
get
public static <T,Z> Optional<Z> get(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, 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:
holder- holder of thePersistentDataContainerkey- key to gettype- type of key- Returns:
- result wrapped in an optional if present.
-
getOrDefault
public static <T,Z> Z getOrDefault(org.bukkit.persistence.PersistentDataContainer container, 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:
container- holder of thePersistentDataContainerkey- key to gettype- type of keydefaultValue- default value if key is absent- Returns:
- result wrapped in an optional if present.
-
getOrDefault
public static <T,Z> Z getOrDefault(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- key to gettype- type of keydefaultValue- default value if key is absent- Returns:
- result wrapped in an optional if present.
-
getOrDefault
public static <T,Z> Z getOrDefault(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, 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:
holder- holder of thePersistentDataContainerkey- key to gettype- type of keydefaultValue- default value if key is absent- Returns:
- result wrapped in an optional if present.
-
putValue
public static <T,Z> void putValue(org.bukkit.persistence.PersistentDataContainer container, 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:
container- thePersistentDataContainerkey- key to settype- type of keyvalue- value to set
-
putValue
public static <T,Z> void putValue(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- key to settype- type of keyvalue- value to set
-
putValue
public static <T,Z> void putValue(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, 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:
holder- holder of thePersistentDataContainerkey- key to settype- type of keyvalue- value to set
-
hasKey
public static <T,Z> boolean hasKey(org.bukkit.persistence.PersistentDataContainer container, 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:
container- thePersistentDataContainerkey- key to checktype- type of key- Returns:
- true if the key is set
-
hasKey
public static <T,Z> boolean hasKey(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- key to checktype- type of key- Returns:
- true if the key is set
-
hasKey
public static <T,Z> boolean hasKey(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, 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:
holder- holder of thePersistentDataContainerkey- key to checktype- type of key- Returns:
- true if the key is set
-
remove
public static <T,Z> boolean remove(org.bukkit.persistence.PersistentDataContainer container, 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:
container- holder of thePersistentDataContainerkey- key to checktype- type of key- Returns:
- true if the key was present and removed.
-
remove
public static <T,Z> boolean remove(@Nullable @Nullable org.bukkit.persistence.PersistentDataHolder holder, 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:
holder- holder of thePersistentDataContainerkey- key to checktype- type of key- Returns:
- true if the key was present and removed.
-
remove
public static <T,Z> boolean remove(@Nullable @Nullable org.bukkit.inventory.ItemStack holder, 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:
holder- holder of thePersistentDataContainerkey- key to checktype- type of key- Returns:
- true if the key was present and removed.
-
byteToBoolean
Converts a byte to boolean.- Parameters:
aByte- byte to convert- Returns:
- byte as boolean. false if null
-
booleanToByte
public static byte booleanToByte(boolean aBoolean) Converts a boolean to a byte.- Parameters:
aBoolean- boolean to convert- Returns:
- boolean as byte.
-