Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Created March 8, 2023 14:59
Show Gist options
  • Save Cryptite/33046a0ebf96e8160ee7ada6a0e3c3ec to your computer and use it in GitHub Desktop.
Save Cryptite/33046a0ebf96e8160ee7ada6a0e3c3ec to your computer and use it in GitHub Desktop.
PDC Handler
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.Optional;
public class PDC {
public static <T, Z> void setPersistentData(Entity e, NamespacedKey key, PersistentDataType<T, Z> dataType, Z value) {
e.getPersistentDataContainer().set(key, dataType, value);
}
public static <T, Z> Optional<Z> getPersistentData(Entity e, NamespacedKey key, PersistentDataType<T, Z> dataType) {
PersistentDataContainer pdc = e.getPersistentDataContainer();
if (pdc.has(key, dataType)) {
Z value = pdc.get(key, dataType);
if (value != null) {
return Optional.of(value);
}
}
return Optional.empty();
}
public static <T, Z> boolean hasPersistentData(Entity e, NamespacedKey key, PersistentDataType<T, Z> dataType) {
return e.getPersistentDataContainer().has(key, dataType);
}
public static boolean hasPersistentData(Entity e, NamespacedKey key) {
return e.getPersistentDataContainer().has(key);
}
public static <T, Z> void setPersistentData(ItemStack item, NamespacedKey key, PersistentDataType<T, Z> dataType, Z value) {
if (item == null || item.getType() == Material.AIR) return;
ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(key, dataType, value);
item.setItemMeta(meta);
}
public static <T, Z> Optional<Z> getPersistentData(ItemStack item, NamespacedKey key, PersistentDataType<T, Z> dataType) {
if (item == null || item.getType() == Material.AIR) return Optional.empty();
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta == null) return Optional.empty();
PersistentDataContainer pdc = itemMeta.getPersistentDataContainer();
if (pdc.has(key, dataType)) {
Z value = pdc.get(key, dataType);
if (value != null) {
return Optional.of(value);
}
}
return Optional.empty();
}
public static void removePersistentData(ItemStack item, NamespacedKey key) {
if (item == null || item.getType() == Material.AIR) return;
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.getPersistentDataContainer().remove(key);
item.setItemMeta(meta);
}
}
public static void removePersistentData(Entity entity, NamespacedKey key) {
if (entity == null) return;
PersistentDataContainer pdc = entity.getPersistentDataContainer();
pdc.remove(key);
}
public static boolean hasPersistentData(ItemStack item, NamespacedKey key) {
if (item == null || item.getType() == Material.AIR) return false;
ItemMeta itemMeta = item.getItemMeta();
return itemMeta != null && itemMeta.getPersistentDataContainer().has(key);
}
public static <T, Z> boolean hasPersistentData(ItemStack item, NamespacedKey key, PersistentDataType<T, Z> dataType) {
if (item == null || item.getType() == Material.AIR) return false;
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta == null) return false;
PersistentDataContainer pdc = itemMeta.getPersistentDataContainer();
return pdc.has(key, dataType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment