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 byte
booleanToByte
(boolean aBoolean) Converts a boolean to a byte.static boolean
byteToBoolean
(Byte aByte) Converts a byte to boolean.static <T,
Z> Z compute
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> map) Gets thePersistentDataContainer
from item meta, computes the value by callingcompute(PersistentDataHolder, NamespacedKey, PersistentDataType, Function)
and applies the meta afterward again.static <T,
Z> Z compute
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainer
based on the current valuestatic <T,
Z> Z compute
(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainer
based on the current valuestatic <T,
Z> Z computeIfAbsent
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainer
if it is not set.static <T,
Z> Z computeIfAbsent
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainer
if it is not set.static <T,
Z> Z computeIfAbsent
(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainer
if it is not set.static <T,
Z> Z computeIfPresent
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainer
if is set.static <T,
Z> Z computeIfPresent
(@Nullable PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainer
if is set.static <T,
Z> Z computeIfPresent
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainer
if is set.static <T,
Z> Optional<Z> get
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type) Get a value from a persistent data holder.static <T,
Z> Optional<Z> get
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type) Get a value from a persistent data holder.static <T,
Z> Optional<Z> get
(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type) Get a value from a persistent data holder.static <T,
Z> Z getOrDefault
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.static <T,
Z> Z getOrDefault
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.static <T,
Z> Z getOrDefault
(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.static <T,
Z> boolean hasKey
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer
.static <T,
Z> boolean hasKey
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer
.static <T,
Z> boolean hasKey
(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer
.static <T,
Z> void putValue
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer
.static <T,
Z> void putValue
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer
.static <T,
Z> void putValue
(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer
.static <T,
Z> boolean remove
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainer
if it is set.static <T,
Z> boolean remove
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainer
if it is set.static <T,
Z> boolean remove
(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainer
if it is set.static <T,
Z> void setIfAbsent
(@Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Sets a value in a data container if it is absent.static <T,
Z> void setIfAbsent
(@Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Sets a value in a data container if it is absent.static <T,
Z> void setIfAbsent
(PersistentDataContainer container, NamespacedKey key, 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(PersistentDataContainer container, NamespacedKey key, 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
- thePersistentDataContainer
key
- key to settype
- type of keyvalue
- value of key
-
setIfAbsent
public static <T,Z> void setIfAbsent(@Nullable @Nullable PersistentDataHolder holder, NamespacedKey key, 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 thePersistentDataContainer
key
- key to settype
- type of keyvalue
- value of key
-
setIfAbsent
public static <T,Z> void setIfAbsent(@Nullable @Nullable ItemStack holder, NamespacedKey key, 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 thePersistentDataContainer
key
- key to settype
- type of keyvalue
- value of key
-
compute
@Nullable public static <T,Z> Z compute(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainer
based on the current value- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
container
- holder of thePersistentDataContainer
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
-
compute
@Nullable public static <T,Z> Z compute(@Nullable @Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainer
based on the current value- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
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
-
compute
@Nullable public static <T,Z> Z compute(@Nullable @Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> map) Gets thePersistentDataContainer
from 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(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainer
if it is not set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
container
- holder of thePersistentDataContainer
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.
-
computeIfAbsent
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfAbsent(@Nullable @Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainer
if it is not set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
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.
-
computeIfAbsent
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfAbsent(@Nullable @Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainer
if it is not set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
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
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfPresent(@Nullable @Nullable PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainer
if is set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
container
- thePersistentDataContainer
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.
-
computeIfPresent
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfPresent(@Nullable @Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainer
if is set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
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.
-
computeIfPresent
@Contract("null, _, _, _ -> null; !null, _, _, _, -> !null") @Nullable public static <T,Z> Z computeIfPresent(@Nullable @Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainer
if is set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
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 static <T,Z> Optional<Z> get(PersistentDataContainer container, NamespacedKey key, 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 thePersistentDataContainer
key
- key to gettype
- type of key- Returns:
- result wrapped in an optional if present.
-
get
public static <T,Z> Optional<Z> get(@Nullable @Nullable PersistentDataHolder holder, NamespacedKey key, 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 thePersistentDataContainer
key
- key to gettype
- type of key- Returns:
- result wrapped in an optional if present.
-
get
public static <T,Z> Optional<Z> get(@Nullable @Nullable ItemStack holder, NamespacedKey key, 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 thePersistentDataContainer
key
- key to gettype
- type of key- Returns:
- result wrapped in an optional if present.
-
getOrDefault
public static <T,Z> Z getOrDefault(PersistentDataContainer container, NamespacedKey key, 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 thePersistentDataContainer
key
- 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 PersistentDataHolder holder, NamespacedKey key, 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 thePersistentDataContainer
key
- 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 ItemStack holder, NamespacedKey key, 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 thePersistentDataContainer
key
- 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(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer
.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
container
- thePersistentDataContainer
key
- key to settype
- type of keyvalue
- value to set
-
putValue
public static <T,Z> void putValue(@Nullable @Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer
.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
key
- key to settype
- type of keyvalue
- value to set
-
putValue
public static <T,Z> void putValue(@Nullable @Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer
.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
key
- key to settype
- type of keyvalue
- value to set
-
hasKey
public static <T,Z> boolean hasKey(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer
.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
container
- thePersistentDataContainer
key
- key to checktype
- type of key- Returns:
- true if the key is set
-
hasKey
public static <T,Z> boolean hasKey(@Nullable @Nullable PersistentDataHolder holder, NamespacedKey key, 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 thePersistentDataContainer
key
- key to checktype
- type of key- Returns:
- true if the key is set
-
hasKey
public static <T,Z> boolean hasKey(@Nullable @Nullable ItemStack holder, NamespacedKey key, 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 thePersistentDataContainer
key
- key to checktype
- type of key- Returns:
- true if the key is set
-
remove
public static <T,Z> boolean remove(PersistentDataContainer container, NamespacedKey key, PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainer
if it is set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
container
- holder of thePersistentDataContainer
key
- key to checktype
- type of key- Returns:
- true if the key was present and removed.
-
remove
public static <T,Z> boolean remove(@Nullable @Nullable PersistentDataHolder holder, NamespacedKey key, PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainer
if it is set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
key
- key to checktype
- type of key- Returns:
- true if the key was present and removed.
-
remove
public static <T,Z> boolean remove(@Nullable @Nullable ItemStack holder, NamespacedKey key, PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainer
if it is set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
holder
- holder of thePersistentDataContainer
key
- 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.
-