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