Search in sources :

Example 1 with AsyncAutoEnchanterProcessEvent

use of io.github.thebusybiscuit.slimefun4.api.events.AsyncAutoEnchanterProcessEvent in project Slimefun4 by Slimefun.

the class AutoEnchanter method enchant.

@Nullable
@ParametersAreNonnullByDefault
protected MachineRecipe enchant(BlockMenu menu, ItemStack target, ItemStack enchantedBook) {
    // Call an event so other Plugins can modify it.
    AsyncAutoEnchanterProcessEvent event = new AsyncAutoEnchanterProcessEvent(target, enchantedBook, menu);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        return null;
    }
    EnchantmentStorageMeta meta = (EnchantmentStorageMeta) enchantedBook.getItemMeta();
    Map<Enchantment, Integer> enchantments = new HashMap<>();
    // Find applicable enchantments
    for (Map.Entry<Enchantment, Integer> entry : meta.getStoredEnchants().entrySet()) {
        if (entry.getKey().canEnchantItem(target)) {
            if (isEnchantmentLevelAllowed(entry.getValue())) {
                enchantments.put(entry.getKey(), entry.getValue());
            } else if (!menu.toInventory().getViewers().isEmpty()) {
                showEnchantmentLevelWarning(menu);
                return null;
            }
        }
    }
    /*
         * If override is false, remove those with lower level so we don't override existing enchants
         * This also removes those with the same level so they aren't accounted for enchanting time
         */
    if (!overrideExistingEnchantsLvl.getValue()) {
        enchantments.entrySet().removeIf(e -> target.getEnchantmentLevel(e.getKey()) >= e.getValue());
    }
    // Check if we found any valid enchantments
    if (!enchantments.isEmpty()) {
        ItemStack enchantedItem = target.clone();
        enchantedItem.setAmount(1);
        enchantedItem.addUnsafeEnchantments(enchantments);
        MachineRecipe recipe = new MachineRecipe(75 * enchantments.size() / getSpeed(), new ItemStack[] { target, enchantedBook }, new ItemStack[] { enchantedItem, new ItemStack(Material.BOOK) });
        if (!InvUtils.fitAll(menu.toInventory(), recipe.getOutput(), getOutputSlots())) {
            return null;
        }
        for (int inputSlot : getInputSlots()) {
            menu.consumeItem(inputSlot);
        }
        return recipe;
    } else {
        return null;
    }
}
Also used : EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) HashMap(java.util.HashMap) MachineRecipe(me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe) AsyncAutoEnchanterProcessEvent(io.github.thebusybiscuit.slimefun4.api.events.AsyncAutoEnchanterProcessEvent) Enchantment(org.bukkit.enchantments.Enchantment) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack) HashMap(java.util.HashMap) Map(java.util.Map) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) Nullable(javax.annotation.Nullable)

Aggregations

AsyncAutoEnchanterProcessEvent (io.github.thebusybiscuit.slimefun4.api.events.AsyncAutoEnchanterProcessEvent)1 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)1 MachineRecipe (me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe)1 Enchantment (org.bukkit.enchantments.Enchantment)1 ItemStack (org.bukkit.inventory.ItemStack)1 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)1