Class PersistentDataWrapper

java.lang.Object
de.eldoria.eldoutilities.pdc.PersistentDataWrapper

public class PersistentDataWrapper extends Object
A wrapper class for PersistentDataContainer. Provides convenient methods for interacting with the persistent data of an object.
  • Constructor Details

  • Method Details

    • create

      public static PersistentDataWrapper create(PersistentDataHolder holder)
    • setIfAbsent

      public <T, Z> void setIfAbsent(NamespacedKey key, PersistentDataType<T,Z> type, Z value)
      Sets a value in a data container if it is absent.
      Type Parameters:
      T - type of key
      Z - type of value
      Parameters:
      key - key to set
      type - type of key
      value - value of key
    • compute

      @Nullable public <T, Z> Z compute(NamespacedKey key, PersistentDataType<T,Z> type, Function<@Nullable Z,Z> map)
      Compute a value in a PersistentDataContainer based on the current value
      Type Parameters:
      T - type of value
      Z - type of value
      Parameters:
      key - key to compute
      type - type of key
      map - 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(NamespacedKey key, PersistentDataType<T,Z> type, Z value)
      Compute a value in a PersistentDataContainer if it is not set.
      Type Parameters:
      T - type of value
      Z - type of value
      Parameters:
      key - key to compute
      type - type of key
      value - 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(NamespacedKey key, PersistentDataType<T,Z> type, Function<Z,Z> mappingFunction)
      Compute a value in a PersistentDataContainer if is set.
      Type Parameters:
      T - type of value
      Z - type of value
      Parameters:
      key - key to compute
      type - type of key
      mappingFunction - 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(NamespacedKey key, PersistentDataType<T,Z> type)
      Get a value from a persistent data holder.
      Type Parameters:
      T - type of value
      Z - type of value
      Parameters:
      key - key to get
      type - type of key
      Returns:
      result wrapped in an optional if present.
    • getOrDefault

      public <T, Z> Z getOrDefault(NamespacedKey key, PersistentDataType<T,Z> type, Z defaultValue)
      Get a value from a persistent data holder.
      Type Parameters:
      T - type of value
      Z - type of value
      Parameters:
      key - key to get
      type - type of key
      defaultValue - default value if key is absent
      Returns:
      result wrapped in an optional if present.
    • put

      public <T, Z> void put(NamespacedKey key, PersistentDataType<T,Z> type, Z value)
      Set a value in a PersistentDataContainer.
      Type Parameters:
      T - type of value
      Z - type of value
      Parameters:
      key - key to set
      type - type of key
      value - value to set
    • hasKey

      public <T, Z> boolean hasKey(NamespacedKey key, PersistentDataType<T,Z> type)
      Check if a key is present in the PersistentDataContainer.
      Type Parameters:
      T - type of value
      Z - type of value
      Parameters:
      key - key to check
      type - type of key
      Returns:
      true if the key is set
    • remove

      public <T, Z> boolean remove(NamespacedKey key, PersistentDataType<T,Z> type)
      Remove a key from the PersistentDataContainer if it is set.
      Type Parameters:
      T - type of value
      Z - type of value
      Parameters:
      key - key to check
      type - type of key
      Returns:
      true if the key was present and removed.