Search in sources :

Example 1 with CustomEnch

use of io.zivoric.enchantmentcore.CustomEnch in project ZEnchantmentCore by zivoric.

the class EnchantmentUtils method updateItemLore.

/**
 * Update the lore of the item meta
 *
 * @param meta the item meta
 */
public static void updateItemLore(ItemMeta meta) {
    if (meta == null) {
        return;
    }
    // Load enchants
    Map<Enchantment, Integer> enchs;
    if (meta instanceof EnchantmentStorageMeta) {
        enchs = ((EnchantmentStorageMeta) meta).getStoredEnchants();
    } else {
        enchs = meta.getEnchants();
    }
    // Filter the custom enchantments
    Map<CustomEnch, Integer> customEnchs = new HashMap<>();
    for (Map.Entry<Enchantment, Integer> entry : enchs.entrySet()) {
        if (entry.getKey() instanceof CustomEnch) {
            customEnchs.put((CustomEnch) entry.getKey(), entry.getValue());
        }
    }
    // Update the lore
    for (LoreHandler loreHandler : LORE_HANDLER_LIST) {
        customEnchs = loreHandler.updateItemLore(meta, customEnchs);
    }
}
Also used : EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) LoreHandler(io.zivoric.enchantmentcore.utils.lore.LoreHandler) PaperLoreHandler(io.zivoric.enchantmentcore.utils.lore.PaperLoreHandler) DefaultLoreHandler(io.zivoric.enchantmentcore.utils.lore.DefaultLoreHandler) CustomEnch(io.zivoric.enchantmentcore.CustomEnch) Enchantment(org.bukkit.enchantments.Enchantment)

Example 2 with CustomEnch

use of io.zivoric.enchantmentcore.CustomEnch in project ZEnchantmentCore by zivoric.

the class EnchantmentUtils method executeEnchantEvent.

/**
 * Execute the enchantment event handler, given the applicable item supplier
 *
 * @param applicableItemSupplier the applicable item supplier
 * @param handlerClass           the handler class
 * @param handlerConsumer        the consumer contains an instance of the handler, a list of levels and a list of applicable items
 * @param <T>                    the type of the handler
 */
private static <T> void executeEnchantEvent(Function<CustomEnch, ItemStack[]> applicableItemSupplier, Class<T> handlerClass, TriConsumer<T, List<Integer>, List<ItemStack>> handlerConsumer) {
    for (CustomEnch customEnch : CustomEnch.values()) {
        if (!handlerClass.isInstance(customEnch)) {
            continue;
        }
        T handler = handlerClass.cast(customEnch);
        ItemStack[] applicableItems = applicableItemSupplier.apply(customEnch);
        if (applicableItems.length > 0) {
            List<ItemStack> items = Arrays.asList(applicableItems);
            List<Integer> levels = items.stream().map(item -> item.getItemMeta().getEnchantLevel(customEnch)).collect(Collectors.toList());
            handlerConsumer.accept(handler, levels, items);
        }
    }
}
Also used : EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) java.util(java.util) Enchantment(org.bukkit.enchantments.Enchantment) ItemMeta(org.bukkit.inventory.meta.ItemMeta) TriConsumer(org.apache.logging.log4j.util.TriConsumer) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) LivingEntity(org.bukkit.entity.LivingEntity) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) CustomEnch(io.zivoric.enchantmentcore.CustomEnch) EnchantmentCore(io.zivoric.enchantmentcore.EnchantmentCore) ItemStack(org.bukkit.inventory.ItemStack) Damageable(org.bukkit.inventory.meta.Damageable) EnchantmentGenerator(io.zivoric.enchantmentcore.EnchantmentGenerator) LoreHandler(io.zivoric.enchantmentcore.utils.lore.LoreHandler) PaperLoreHandler(io.zivoric.enchantmentcore.utils.lore.PaperLoreHandler) DefaultLoreHandler(io.zivoric.enchantmentcore.utils.lore.DefaultLoreHandler) Entry(java.util.Map.Entry) EntityEquipment(org.bukkit.inventory.EntityEquipment) Material(org.bukkit.Material) CustomEnch(io.zivoric.enchantmentcore.CustomEnch) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with CustomEnch

use of io.zivoric.enchantmentcore.CustomEnch in project ZEnchantmentCore by zivoric.

the class PaperLoreHandler method updateItemLore.

@Override
public Map<CustomEnch, Integer> updateItemLore(ItemMeta meta, Map<CustomEnch, Integer> currentEnchantMap) {
    Map<CustomEnch, Integer> remaining = new HashMap<>();
    // Create enchant lore
    List<Component> createdLore = new ArrayList<>();
    // Only add lore if the item doesn't hide enchants
    boolean showEnchants = !meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS);
    currentEnchantMap.forEach((ench, level) -> {
        if (ench instanceof PaperCustomEnch) {
            if (showEnchants) {
                createdLore.add(ench.displayName(level).decoration(TextDecoration.ITALIC, false).append(ENCH_CODE));
            }
        } else {
            remaining.put(ench, level);
        }
    });
    // Add current lore
    List<Component> currentLore = meta.lore();
    if (currentLore != null) {
        for (Component comp : currentLore) {
            if (!comp.children().contains(ENCH_CODE)) {
                // Should not contain enchant lore
                createdLore.add(comp);
            }
        }
    }
    // Set lore to item meta
    if (!createdLore.equals(currentLore)) {
        meta.lore(createdLore.isEmpty() ? null : createdLore);
    }
    return remaining;
}
Also used : PaperCustomEnch(io.zivoric.enchantmentcore.paper.PaperCustomEnch) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PaperCustomEnch(io.zivoric.enchantmentcore.paper.PaperCustomEnch) CustomEnch(io.zivoric.enchantmentcore.CustomEnch) Component(net.kyori.adventure.text.Component)

Aggregations

CustomEnch (io.zivoric.enchantmentcore.CustomEnch)3 DefaultLoreHandler (io.zivoric.enchantmentcore.utils.lore.DefaultLoreHandler)2 LoreHandler (io.zivoric.enchantmentcore.utils.lore.LoreHandler)2 PaperLoreHandler (io.zivoric.enchantmentcore.utils.lore.PaperLoreHandler)2 Enchantment (org.bukkit.enchantments.Enchantment)2 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)2 EnchantmentCore (io.zivoric.enchantmentcore.EnchantmentCore)1 EnchantmentGenerator (io.zivoric.enchantmentcore.EnchantmentGenerator)1 PaperCustomEnch (io.zivoric.enchantmentcore.paper.PaperCustomEnch)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Component (net.kyori.adventure.text.Component)1 TriConsumer (org.apache.logging.log4j.util.TriConsumer)1 Material (org.bukkit.Material)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 LivingEntity (org.bukkit.entity.LivingEntity)1