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