Interface PersistentDataKey<C>

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

@NullMarked 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);
  }
}