1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Merge pull request #3037 from StarWishsama/chore/auto-enchant-event

Introduced AsyncAutoEnchanterProcessEvent
This commit is contained in:
TheBusyBiscuit 2021-05-11 14:13:37 +02:00 committed by GitHub
commit 9c05237af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 114 additions and 15 deletions

View File

@ -0,0 +1,92 @@
package io.github.thebusybiscuit.slimefun4.api.events;
import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoEnchanter;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import org.apache.commons.lang.Validate;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import javax.annotation.Nonnull;
/**
* An {@link Event} that is called whenever an {@link AutoEnchanter} is
* enchanting an {@link ItemStack}.
*
* @author StarWishsama
*/
public class AsyncAutoEnchanterProcessEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final ItemStack item;
private final ItemStack enchantedBook;
private final BlockMenu menu;
private boolean cancelled;
public AsyncAutoEnchanterProcessEvent(@Nonnull ItemStack item, @Nonnull ItemStack enchantedBook, @Nonnull BlockMenu menu) {
super(true);
Validate.notNull(item, "The item to enchant cannot be null!");
Validate.notNull(enchantedBook, "The enchanted book to enchant cannot be null!");
Validate.notNull(menu, "The menu of auto-enchanter cannot be null!");
this.item = item;
this.enchantedBook = enchantedBook;
this.menu = menu;
}
/**
* This returns the {@link ItemStack} that is being enchanted.
*
* @return The {@link ItemStack} that is being enchanted
*/
@Nonnull
public ItemStack getItem() {
return item;
}
/**
* This returns the {@link ItemStack} that is being used enchanted book
*
* @return The {@link ItemStack} that is being used enchanted book
*/
@Nonnull
public ItemStack getEnchantedBook() {
return enchantedBook;
}
/**
* This returns the {@link AutoEnchanter}'s {@link BlockMenu}
*
* @return The {@link BlockMenu} of {@link AutoEnchanter} that is enchanting item
*/
@Nonnull
public BlockMenu getMenu() {
return menu;
}
@Nonnull
public static HandlerList getHandlerList() {
return handlers;
}
@Nonnull
@Override
public HandlerList getHandlers() {
return getHandlerList();
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,18 +1,7 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import io.github.thebusybiscuit.cscorelib2.inventory.InvUtils;
import io.github.thebusybiscuit.slimefun4.api.events.AsyncAutoEnchanterProcessEvent;
import io.github.thebusybiscuit.slimefun4.api.events.AutoEnchantEvent;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
@ -20,6 +9,16 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.HashMap;
import java.util.Map;
/**
* The {@link AutoEnchanter}, in contrast to the {@link AutoDisenchanter}, adds
@ -65,10 +64,10 @@ public class AutoEnchanter extends AbstractEnchantmentMachine {
return null;
}
ItemStack secondItem = menu.getItemInSlot(slot);
ItemStack enchantedBook = menu.getItemInSlot(slot);
if (secondItem != null && secondItem.getType() == Material.ENCHANTED_BOOK) {
return enchant(menu, item, secondItem);
if (enchantedBook != null && enchantedBook.getType() == Material.ENCHANTED_BOOK) {
return enchant(menu, item, enchantedBook);
}
}
@ -78,6 +77,14 @@ public class AutoEnchanter extends AbstractEnchantmentMachine {
@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<>();