From 63414033a6f05c2d8e2051326f4491a77dd475a7 Mon Sep 17 00:00:00 2001 From: JustAHuman-xD <65748158+JustAHuman-xD@users.noreply.github.com> Date: Tue, 21 Nov 2023 14:07:00 -0600 Subject: [PATCH] Add MultiBlockCraftEvent (Updated version of #3439) (#3928) --- .../api/events/MultiBlockCraftEvent.java | 120 ++++++++++++++++++ .../items/multiblocks/AbstractSmeltery.java | 8 +- .../items/multiblocks/ArmorForge.java | 9 +- .../multiblocks/AutomatedPanningMachine.java | 16 ++- .../items/multiblocks/Compressor.java | 10 +- .../multiblocks/EnhancedCraftingTable.java | 9 +- .../items/multiblocks/GrindStone.java | 10 +- .../items/multiblocks/Juicer.java | 10 +- .../items/multiblocks/MagicWorkbench.java | 8 +- .../items/multiblocks/OreCrusher.java | 8 +- .../items/multiblocks/OreWasher.java | 34 +++-- .../items/multiblocks/PressureChamber.java | 10 +- .../items/multiblocks/TableSaw.java | 11 +- 13 files changed, 234 insertions(+), 29 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/api/events/MultiBlockCraftEvent.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/MultiBlockCraftEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/MultiBlockCraftEvent.java new file mode 100644 index 0000000000..7a3fc3c535 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/MultiBlockCraftEvent.java @@ -0,0 +1,120 @@ +package io.github.thebusybiscuit.slimefun4.api.events; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine; + +/** + * This {@link Event} is called when a {@link Player} crafts an item using a {@link MultiBlockMachine}. + * Unlike the {@link MultiBlockInteractEvent}, this event only fires if an output to a craft is expected. + * If this event is cancelled, ingredients will not be consumed and no output item results. + * + * @author char321 + * @author JustAHuman + */ +public class MultiBlockCraftEvent extends PlayerEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private final MultiBlockMachine machine; + private final ItemStack[] input; + private ItemStack output; + private boolean cancelled; + + /** + * Creates a new {@link MultiBlockCraftEvent}. + * + * @param p The player that crafts using a multiblock + * @param machine The multiblock machine used to craft + * @param input The input items of the craft + * @param output The resulting item of the craft + */ + @ParametersAreNonnullByDefault + public MultiBlockCraftEvent(Player p, MultiBlockMachine machine, ItemStack[] input, ItemStack output) { + super(p); + this.machine = machine; + this.input = input; + this.output = output; + } + + /** + * Creates a new {@link MultiBlockCraftEvent}. + * + * @param p The player that crafts using a multiblock + * @param machine The multiblock machine used to craft + * @param input The input item of the craft + * @param output The resulting item of the craft + */ + @ParametersAreNonnullByDefault + public MultiBlockCraftEvent(Player p, MultiBlockMachine machine, ItemStack input, ItemStack output) { + this(p, machine, new ItemStack[]{input}, output); + } + + /** + * Gets the machine that was used to craft. + * + * @return The {@link MultiBlockMachine} used to craft. + */ + public @Nonnull MultiBlockMachine getMachine() { + return machine; + } + + /** + * Gets the input of the craft. + * + * @return The {@link ItemStack ItemStack[]} input that is used in the craft. + */ + public @Nonnull ItemStack[] getInput() { + return input; + } + + /** + * Gets the output of the craft. + * + * @return The {@link ItemStack} output that results from the craft. + */ + public @Nonnull ItemStack getOutput() { + return output; + } + + /** + * Sets the output of the craft. Keep in mind that this overwrites any existing output. + * + * @param output + * The new item for the event to produce. + * + * @return The previous {@link ItemStack} output that was replaced. + */ + public @Nullable ItemStack setOutput(@Nullable ItemStack output) { + ItemStack oldOutput = this.output; + this.output = output; + return oldOutput; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + public static @Nonnull HandlerList getHandlerList() { + return handlers; + } + + @Override + public @Nonnull HandlerList getHandlers() { + return getHandlerList(); + } +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AbstractSmeltery.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AbstractSmeltery.java index e11e76b3c8..7f2f27f0cb 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AbstractSmeltery.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AbstractSmeltery.java @@ -4,6 +4,7 @@ import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -14,6 +15,7 @@ import org.bukkit.inventory.ItemStack; import io.github.bakedlibs.dough.inventory.InvUtils; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; @@ -48,12 +50,14 @@ public void onInteract(Player p, Block b) { for (int i = 0; i < inputs.size(); i++) { if (canCraft(inv, inputs, i)) { ItemStack output = RecipeType.getRecipeOutputList(this, inputs.get(i)).clone(); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, inputs.get(i), output); - if (SlimefunUtils.canPlayerUseItem(p, output, true)) { + Bukkit.getPluginManager().callEvent(event); + if (!event.isCancelled() && SlimefunUtils.canPlayerUseItem(p, output, true)) { Inventory outputInv = findOutputInventory(output, possibleDispenser, inv); if (outputInv != null) { - craft(p, b, inv, inputs.get(i), output, outputInv); + craft(p, b, inv, inputs.get(i), event.getOutput(), outputInv); } else { Slimefun.getLocalization().sendMessage(p, "machines.full-inventory", true); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/ArmorForge.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/ArmorForge.java index d8e7dae44b..4ee70ac722 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/ArmorForge.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/ArmorForge.java @@ -4,6 +4,7 @@ import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -13,9 +14,9 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import io.github.thebusybiscuit.slimefun4.core.services.sounds.SoundEffect; import io.github.bakedlibs.dough.items.CustomItemStack; import io.github.bakedlibs.dough.items.ItemUtils; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; @@ -43,9 +44,11 @@ public void onInteract(Player p, Block b) { for (ItemStack[] input : inputs) { if (isCraftable(inv, input)) { ItemStack output = RecipeType.getRecipeOutputList(this, input).clone(); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output); - if (SlimefunUtils.canPlayerUseItem(p, output, true)) { - craft(p, output, inv, possibleDispenser); + Bukkit.getPluginManager().callEvent(event); + if (!event.isCancelled() && SlimefunUtils.canPlayerUseItem(p, output, true)) { + craft(p, event.getOutput(), inv, possibleDispenser); } return; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AutomatedPanningMachine.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AutomatedPanningMachine.java index c5e407404d..ca2d0c7124 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AutomatedPanningMachine.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AutomatedPanningMachine.java @@ -7,6 +7,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.GameMode; import org.bukkit.Material; @@ -18,6 +19,7 @@ import io.github.bakedlibs.dough.items.ItemUtils; import io.github.bakedlibs.dough.scheduling.TaskQueue; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine; @@ -74,6 +76,14 @@ public void onInteract(Player p, Block b) { return; } + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output); + + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + + ItemStack finalOutput = event.getOutput(); if (p.getGameMode() != GameMode.CREATIVE) { ItemUtils.consumeItem(input, false); } @@ -82,13 +92,13 @@ public void onInteract(Player p, Block b) { queue.thenRepeatEvery(20, 5, () -> b.getWorld().playEffect(b.getRelative(BlockFace.DOWN).getLocation(), Effect.STEP_SOUND, material)); queue.thenRun(20, () -> { - if (output.getType() != Material.AIR) { + if (finalOutput.getType() != Material.AIR) { Optional outputChest = OutputChest.findOutputChestFor(b.getRelative(BlockFace.DOWN), output); if (outputChest.isPresent()) { - outputChest.get().addItem(output.clone()); + outputChest.get().addItem(finalOutput.clone()); } else { - b.getWorld().dropItemNaturally(b.getLocation(), output.clone()); + b.getWorld().dropItemNaturally(b.getLocation(), finalOutput.clone()); } SoundEffect.AUTOMATED_PANNING_MACHINE_SUCCESS_SOUND.playAt(b); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/Compressor.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/Compressor.java index c799ae879c..f0b48882e6 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/Compressor.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/Compressor.java @@ -6,6 +6,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -16,6 +17,7 @@ import org.bukkit.inventory.ItemStack; import io.github.bakedlibs.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; @@ -66,13 +68,19 @@ public void onInteract(Player p, Block b) { if (recipeInput != null && SlimefunUtils.isItemSimilar(item, recipeInput, true)) { ItemStack output = RecipeType.getRecipeOutput(this, recipeInput); Inventory outputInv = findOutputInventory(output, dispBlock, inv); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, item, output); + + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } if (outputInv != null) { ItemStack removing = item.clone(); removing.setAmount(recipeInput.getAmount()); inv.removeItem(removing); - craft(p, output, dispBlock, inv); + craft(p, event.getOutput(), dispBlock, inv); } else { Slimefun.getLocalization().sendMessage(p, "machines.full-inventory", true); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/EnhancedCraftingTable.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/EnhancedCraftingTable.java index 67b1ebbf58..4486197b62 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/EnhancedCraftingTable.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/EnhancedCraftingTable.java @@ -4,6 +4,7 @@ import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -13,8 +14,8 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import io.github.thebusybiscuit.slimefun4.core.services.sounds.SoundEffect; import io.github.bakedlibs.dough.items.ItemUtils; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; @@ -44,9 +45,11 @@ public void onInteract(Player p, Block b) { for (ItemStack[] input : inputs) { if (isCraftable(inv, input)) { ItemStack output = RecipeType.getRecipeOutputList(this, input).clone(); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output); - if (SlimefunUtils.canPlayerUseItem(p, output, true)) { - craft(inv, possibleDispenser, p, b, output); + Bukkit.getPluginManager().callEvent(event); + if (!event.isCancelled() && SlimefunUtils.canPlayerUseItem(p, output, true)) { + craft(inv, possibleDispenser, p, b, event.getOutput()); } return; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/GrindStone.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/GrindStone.java index 140aa83f8d..f149093042 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/GrindStone.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/GrindStone.java @@ -6,6 +6,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -16,6 +17,7 @@ import org.bukkit.inventory.ItemStack; import io.github.bakedlibs.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; @@ -126,12 +128,18 @@ public void onInteract(Player p, Block b) { if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) { ItemStack output = RecipeType.getRecipeOutput(this, convert); Inventory outputInv = findOutputInventory(output, possibleDispenser, inv); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, current, output); + + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } if (outputInv != null) { ItemStack removing = current.clone(); removing.setAmount(1); inv.removeItem(removing); - outputInv.addItem(output); + outputInv.addItem(event.getOutput()); SoundEffect.GRIND_STONE_INTERACT_SOUND.playAt(b); } else { Slimefun.getLocalization().sendMessage(p, "machines.full-inventory", true); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/Juicer.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/Juicer.java index 40978b05ca..a81c731434 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/Juicer.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/Juicer.java @@ -6,6 +6,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Material; import org.bukkit.block.Block; @@ -17,6 +18,7 @@ import org.bukkit.inventory.ItemStack; import io.github.bakedlibs.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; @@ -62,12 +64,18 @@ public void onInteract(Player p, Block b) { if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) { ItemStack adding = RecipeType.getRecipeOutput(this, convert); Inventory outputInv = findOutputInventory(adding, possibleDispenser, inv); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, current, adding); + + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } if (outputInv != null) { ItemStack removing = current.clone(); removing.setAmount(1); inv.removeItem(removing); - outputInv.addItem(adding); + outputInv.addItem(event.getOutput()); SoundEffect.JUICER_USE_SOUND.playAt(b); // Not changed since this is supposed to be a natural sound. diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/MagicWorkbench.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/MagicWorkbench.java index 98cebcf884..25ce8cdcc8 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/MagicWorkbench.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/MagicWorkbench.java @@ -4,6 +4,7 @@ import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Material; import org.bukkit.block.Block; @@ -15,6 +16,7 @@ import org.bukkit.inventory.ItemStack; import io.github.bakedlibs.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; @@ -50,9 +52,11 @@ public void onInteract(Player p, Block b) { for (ItemStack[] input : inputs) { if (isCraftable(inv, input)) { ItemStack output = RecipeType.getRecipeOutputList(this, input).clone(); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output); - if (SlimefunUtils.canPlayerUseItem(p, output, true)) { - craft(inv, possibleDispener, p, b, output); + Bukkit.getPluginManager().callEvent(event); + if (!event.isCancelled() && SlimefunUtils.canPlayerUseItem(p, output, true)) { + craft(inv, possibleDispener, p, b, event.getOutput()); } return; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/OreCrusher.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/OreCrusher.java index 2a0dad3d61..ea5141f099 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/OreCrusher.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/OreCrusher.java @@ -7,6 +7,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Material; import org.bukkit.block.Block; @@ -18,6 +19,7 @@ import org.bukkit.inventory.ItemStack; import io.github.bakedlibs.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; @@ -190,13 +192,15 @@ public void onInteract(Player p, Block b) { if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) { ItemStack adding = RecipeType.getRecipeOutput(this, convert); Inventory outputInv = findOutputInventory(adding, possibleDispenser, inv); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, current, adding); - if (SlimefunUtils.canPlayerUseItem(p, adding, true)) { + Bukkit.getPluginManager().callEvent(event); + if (!event.isCancelled() && SlimefunUtils.canPlayerUseItem(p, adding, true)) { if (outputInv != null) { ItemStack removing = current.clone(); removing.setAmount(convert.getAmount()); inv.removeItem(removing); - outputInv.addItem(adding); + outputInv.addItem(event.getOutput()); p.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, 1); } else { Slimefun.getLocalization().sendMessage(p, "machines.full-inventory", true); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/OreWasher.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/OreWasher.java index 80dc6c6cc3..25a3282369 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/OreWasher.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/OreWasher.java @@ -2,12 +2,12 @@ import java.util.List; import java.util.concurrent.ThreadLocalRandom; -import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Material; import org.bukkit.block.Block; @@ -18,6 +18,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine; @@ -84,8 +85,8 @@ protected void registerDefaultRecipes(List recipes) { } @Override - public List getDisplayRecipes() { - return recipes.stream().map(items -> items[0]).collect(Collectors.toList()); + public @Nonnull List getDisplayRecipes() { + return recipes.stream().map(items -> items[0]).toList(); } @Override @@ -93,15 +94,14 @@ public void onInteract(Player p, Block b) { Block dispBlock = b.getRelative(BlockFace.UP); BlockState state = PaperLib.getBlockState(dispBlock, false).getState(); - if (state instanceof Dispenser) { - Dispenser disp = (Dispenser) state; + if (state instanceof Dispenser disp) { Inventory inv = disp.getInventory(); for (ItemStack input : inv.getContents()) { if (input != null) { if (SlimefunUtils.isItemSimilar(input, SlimefunItems.SIFTED_ORE, true)) { ItemStack output = getRandomDust(); - Inventory outputInv = null; + Inventory outputInv; if (!legacyMode) { /* @@ -119,7 +119,12 @@ public void onInteract(Player p, Block b) { outputInv = findOutputInventory(output, dispBlock, inv); } - removeItem(p, b, inv, outputInv, input, output, 1); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output); + if (event.isCancelled()) { + return; + } + + removeItem(p, b, inv, outputInv, input, event.getOutput(), 1); if (outputInv != null) { outputInv.addItem(SlimefunItems.STONE_CHUNK); @@ -130,14 +135,25 @@ public void onInteract(Player p, Block b) { ItemStack output = SlimefunItems.SALT; Inventory outputInv = findOutputInventory(output, dispBlock, inv); - removeItem(p, b, inv, outputInv, input, output, 2); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output); + if (event.isCancelled()) { + return; + } + + removeItem(p, b, inv, outputInv, input, event.getOutput(), 2); return; } else if (SlimefunUtils.isItemSimilar(input, SlimefunItems.PULVERIZED_ORE, true)) { ItemStack output = SlimefunItems.PURE_ORE_CLUSTER; Inventory outputInv = findOutputInventory(output, dispBlock, inv); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output); + + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } - removeItem(p, b, inv, outputInv, input, output, 1); + removeItem(p, b, inv, outputInv, input, event.getOutput(), 1); return; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/PressureChamber.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/PressureChamber.java index 4080aa31a0..c8b75de5a3 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/PressureChamber.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/PressureChamber.java @@ -6,6 +6,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Material; import org.bukkit.block.Block; @@ -17,6 +18,7 @@ import org.bukkit.inventory.ItemStack; import io.github.bakedlibs.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; @@ -51,13 +53,19 @@ public void onInteract(Player p, Block b) { if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) { ItemStack output = RecipeType.getRecipeOutput(this, convert); Inventory outputInv = findOutputInventory(output, possibleDispenser, inv); + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, current, output); + + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } if (outputInv != null) { ItemStack removing = current.clone(); removing.setAmount(convert.getAmount()); inv.removeItem(removing); - craft(p, b, output, inv, possibleDispenser); + craft(p, b, event.getOutput(), inv, possibleDispenser); } else { Slimefun.getLocalization().sendMessage(p, "machines.full-inventory", true); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/TableSaw.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/TableSaw.java index 0e920cbe04..40bb1b2fdd 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/TableSaw.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/TableSaw.java @@ -8,6 +8,7 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.GameMode; import org.bukkit.Material; @@ -19,6 +20,7 @@ import org.bukkit.inventory.ItemStack; import io.github.bakedlibs.dough.items.ItemUtils; +import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine; @@ -107,11 +109,18 @@ public void onInteract(@Nonnull Player p, @Nonnull Block b) { return; } + MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, item, output); + + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + if (p.getGameMode() != GameMode.CREATIVE) { ItemUtils.consumeItem(item, true); } - outputItems(b, output); + outputItems(b, event.getOutput()); b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, item.getType()); }