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));
}
}
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;
}
Aggregations