Interface PersistentDataKey<C>

Type Parameters:
C - the retrieved object type when applying this tag type

public interface PersistentDataKey<C>
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 Details

    • of

      static <C> PersistentDataKey<C> of(Key key, PersistentDataType<?,C> type)
      Creates a new PersistentDataKey with the given Key and PersistentDataType.
      Type Parameters:
      C - the retrieved object type when applying this tag type
      Parameters:
      key - the access key to store
      type - the persistent data type to reference
      Returns:
      a new PersistentDataKey of the given key and type
      Throws:
      IllegalArgumentException - if either key or type are null
    • key

      Returns the access key of this PersistentDataKey.
      Returns:
      the access key of this PersistentDataKey
    • dataType

      PersistentDataType<?,C> dataType()
      Returns the data type of this PersistentDataKey.
      Returns:
      the data type of this PersistentDataKey