Search in sources :

Example 1 with PersistentDataType

use of org.bukkit.persistence.PersistentDataType in project oraxen by oraxen.

the class ItemParser method parseVanillaSections.

@SuppressWarnings({ "unchecked", "deprecation" })
private void parseVanillaSections(ItemBuilder item) {
    if (section.contains("ItemFlags")) {
        List<String> itemFlags = section.getStringList("ItemFlags");
        for (String itemFlag : itemFlags) item.addItemFlags(ItemFlag.valueOf(itemFlag));
    }
    if (section.contains("PotionEffects")) {
        // because this sections must always return a List<LinkedHashMap<String, ?>>
        @SuppressWarnings("unchecked") List<LinkedHashMap<String, Object>> potionEffects = (List<LinkedHashMap<String, Object>>) section.getList("PotionEffects");
        for (Map<String, Object> serializedPotionEffect : potionEffects) {
            PotionEffectType effect = PotionEffectType.getByName((String) serializedPotionEffect.get("type"));
            int duration = (int) serializedPotionEffect.get("duration");
            int amplifier = (int) serializedPotionEffect.get("amplifier");
            boolean ambient = (boolean) serializedPotionEffect.get("ambient");
            boolean particles = (boolean) serializedPotionEffect.get("particles");
            boolean icon = (boolean) serializedPotionEffect.get("icon");
            item.addPotionEffect(new PotionEffect(effect, duration, amplifier, ambient, particles, icon));
        }
    }
    if (section.contains("PersistentData")) {
        try {
            List<LinkedHashMap<String, Object>> dataHolder = (List<LinkedHashMap<String, Object>>) section.getList("PersistentData");
            for (LinkedHashMap<String, Object> attributeJson : dataHolder) {
                String[] keyContent = ((String) attributeJson.get("key")).split(":");
                final Object persistentDataType = PersistentDataType.class.getDeclaredField((String) attributeJson.get("type")).get(null);
                item.addCustomTag(new NamespacedKey(keyContent[0], keyContent[1]), (PersistentDataType) persistentDataType, attributeJson.get("value"));
            }
        } catch (IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    if (section.contains("AttributeModifiers")) {
        // because this sections must always return a List<LinkedHashMap<String, ?>>
        @SuppressWarnings("unchecked") List<LinkedHashMap<String, Object>> attributes = (List<LinkedHashMap<String, Object>>) section.getList("AttributeModifiers");
        for (LinkedHashMap<String, Object> attributeJson : attributes) {
            AttributeModifier attributeModifier = AttributeModifier.deserialize(attributeJson);
            Attribute attribute = Attribute.valueOf((String) attributeJson.get("attribute"));
            item.addAttributeModifiers(attribute, attributeModifier);
        }
    }
    if (section.contains("Enchantments")) {
        ConfigurationSection enchantSection = section.getConfigurationSection("Enchantments");
        for (String enchant : enchantSection.getKeys(false)) item.addEnchant(EnchantmentWrapper.getByKey(NamespacedKey.minecraft(enchant)), enchantSection.getInt(enchant));
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) Attribute(org.bukkit.attribute.Attribute) PotionEffectType(org.bukkit.potion.PotionEffectType) PersistentDataType(org.bukkit.persistence.PersistentDataType) AttributeModifier(org.bukkit.attribute.AttributeModifier) LinkedHashMap(java.util.LinkedHashMap) NamespacedKey(org.bukkit.NamespacedKey) List(java.util.List) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 2 with PersistentDataType

use of org.bukkit.persistence.PersistentDataType in project LielsUtils by LielAmar.

the class ItemsUtils method getItemTag.

@NotNull
public static <T> T getItemTag(@NotNull Plugin plugin, @NotNull ItemMeta meta, @NotNull String key, @NotNull T defaultValue) {
    PersistentDataType<T, T> dataType = (PersistentDataType<T, T>) ItemTag.getPersistentDataTypeByType(defaultValue);
    PersistentDataContainer container = meta.getPersistentDataContainer();
    NamespacedKey namespacedKey = new NamespacedKey(plugin, key);
    if (container.get(namespacedKey, dataType) == null)
        container.set(namespacedKey, dataType, defaultValue);
    T value = container.get(namespacedKey, dataType);
    return value == null ? defaultValue : value;
}
Also used : NamespacedKey(org.bukkit.NamespacedKey) PersistentDataType(org.bukkit.persistence.PersistentDataType) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NamespacedKey (org.bukkit.NamespacedKey)2 PersistentDataType (org.bukkit.persistence.PersistentDataType)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Attribute (org.bukkit.attribute.Attribute)1 AttributeModifier (org.bukkit.attribute.AttributeModifier)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 PersistentDataContainer (org.bukkit.persistence.PersistentDataContainer)1 PotionEffect (org.bukkit.potion.PotionEffect)1 PotionEffectType (org.bukkit.potion.PotionEffectType)1 NotNull (org.jetbrains.annotations.NotNull)1