diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/handlers/MachineBlockBreakHandler.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/handlers/MachineBlockBreakHandler.java new file mode 100644 index 0000000000..0128450120 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/handlers/MachineBlockBreakHandler.java @@ -0,0 +1,92 @@ +package io.github.thebusybiscuit.slimefun4.implementation.handlers; + +import io.github.thebusybiscuit.slimefun4.core.machines.MachineOperation; +import io.github.thebusybiscuit.slimefun4.core.machines.MachineProcessor; +import io.github.thebusybiscuit.slimefun4.implementation.operations.CraftingOperation; +import me.mrCookieSlime.Slimefun.api.BlockStorage; +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.inventory.ItemStack; + +import javax.annotation.Nonnull; + +/** + * A {@link SimpleBlockBreakHandler} given the processor and the slots to drop of a machine. + * Uses the machine's {@link MachineProcessor} to drop the items that didn't finish processing for crafting recipes + * The slots field is used to drop items in those slots of block's inventory. + * + * @author Vaan1310/Intybyte + * + */ +public class MachineBlockBreakHandler extends SimpleBlockBreakHandler { + private final MachineProcessor processor; + private final int[][] slots; + + public MachineBlockBreakHandler(MachineProcessor processor, int[]... slots) { + this.processor = processor; + this.slots = slots; + } + + public MachineBlockBreakHandler(int[]... slots) { + this(null, slots); + } + + protected void beforeBreak(@Nonnull Block b) { + + } + + @Override + public final void onBlockBreak(@Nonnull Block b) { + beforeBreak(b); + BlockMenu inv = BlockStorage.getInventory(b); + Location loc = b.getLocation(); + + // Check if the world where we might want to drop stuff is valid + World world = loc.getWorld(); + if (world == null) { + if (processor != null) { + processor.endOperation(b); + } + afterBreak(b); + return; + } + + if (inv != null) { + for (int[] slotArray : slots) { + inv.dropItems(loc, slotArray); + } + } + + if (processor == null) { + afterBreak(b); + return; + } + + var operation = processor.getOperation(b); + if (operation == null) { + afterBreak(b); + return; + } + + // if the processed operation is a crafting recipe give back recipe ingredients, Fixes #4268 + if (!(operation instanceof CraftingOperation craftingOperation)) { + processor.endOperation(b); + afterBreak(b); + return; + } + + ItemStack[] ingredients = craftingOperation.getIngredients(); + for (ItemStack ingredient : ingredients) { + world.dropItemNaturally(loc, ingredient); + } + + processor.endOperation(b); + afterBreak(b); + } + + protected void afterBreak(@Nonnull Block b) { + + } +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/AbstractFilterNode.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/AbstractFilterNode.java index d3197270bf..2bb2e42ff7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/AbstractFilterNode.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/AbstractFilterNode.java @@ -4,6 +4,7 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -52,17 +53,7 @@ public boolean hasItemFilter() { @Nonnull private BlockBreakHandler onBreak() { - return new SimpleBlockBreakHandler() { - - @Override - public void onBlockBreak(@Nonnull Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), SLOTS); - } - } - }; + return new MachineBlockBreakHandler(SLOTS); } @Nonnull diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/ReactorAccessPort.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/ReactorAccessPort.java index 241c1f00f7..28730a6960 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/ReactorAccessPort.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/ReactorAccessPort.java @@ -4,6 +4,7 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -117,19 +118,7 @@ public int[] getSlotsAccessedByItemTransport(DirtyChestMenu menu, ItemTransportF @Nonnull private BlockBreakHandler onBreak() { - return new SimpleBlockBreakHandler() { - - @Override - public void onBlockBreak(@Nonnull Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), getFuelSlots()); - inv.dropItems(b.getLocation(), getCoolantSlots()); - inv.dropItems(b.getLocation(), getOutputSlots()); - } - } - }; + return new MachineBlockBreakHandler(getFuelSlots(), getCoolantSlots(), getOutputSlots()); } private void constructMenu(@Nonnull BlockMenuPreset preset) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/FluidPump.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/FluidPump.java index 729fd2ff51..94492962eb 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/FluidPump.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/FluidPump.java @@ -8,6 +8,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; @@ -70,18 +71,7 @@ public FluidPump(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeT @Nonnull private BlockBreakHandler onBreak() { - return new SimpleBlockBreakHandler() { - - @Override - public void onBlockBreak(Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), getInputSlots()); - inv.dropItems(b.getLocation(), getOutputSlots()); - } - } - }; + return new MachineBlockBreakHandler(getOutputSlots(), getOutputSlots()); } private void constructMenu(@Nonnull BlockMenuPreset preset) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/accelerators/AbstractGrowthAccelerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/accelerators/AbstractGrowthAccelerator.java index 7136101287..324df08387 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/accelerators/AbstractGrowthAccelerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/accelerators/AbstractGrowthAccelerator.java @@ -3,6 +3,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; @@ -39,17 +40,7 @@ protected AbstractGrowthAccelerator(ItemGroup itemGroup, SlimefunItemStack item, @Nonnull private BlockBreakHandler onBreak() { - return new SimpleBlockBreakHandler() { - - @Override - public void onBlockBreak(Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), getInputSlots()); - } - } - }; + return new MachineBlockBreakHandler(getInputSlots()); } private void constructMenu(BlockMenuPreset preset) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/AutoBreeder.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/AutoBreeder.java index e62fd7951d..98fa4c7835 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/AutoBreeder.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/AutoBreeder.java @@ -3,6 +3,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.bukkit.Material; import org.bukkit.Particle; import org.bukkit.block.Block; @@ -50,17 +51,7 @@ public AutoBreeder(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recip @Nonnull private ItemHandler onBreak() { - return new SimpleBlockBreakHandler() { - - @Override - public void onBlockBreak(Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), getInputSlots()); - } - } - }; + return new MachineBlockBreakHandler(getInputSlots()); } protected void constructMenu(BlockMenuPreset preset) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java index 4697ec75fd..27435173a6 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java @@ -5,6 +5,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -77,17 +78,7 @@ public void onPlayerPlace(@Nonnull BlockPlaceEvent e) { } private @Nonnull ItemHandler onBreak() { - return new SimpleBlockBreakHandler() { - - @Override - public void onBlockBreak(@Nonnull Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), getOutputSlots()); - } - } - }; + return new MachineBlockBreakHandler(getOutputSlots()); } @Override diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/reactors/Reactor.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/reactors/Reactor.java index 47b524616f..7d6b9a94b5 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/reactors/Reactor.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/reactors/Reactor.java @@ -10,6 +10,7 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -122,19 +123,9 @@ public MachineProcessor getMachineProcessor() { @Nonnull private BlockBreakHandler onBreak() { - return new SimpleBlockBreakHandler() { - + return new MachineBlockBreakHandler(processor, getFuelSlots(), getCoolantSlots(), getOutputSlots()) { @Override - public void onBlockBreak(@Nonnull Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), getFuelSlots()); - inv.dropItems(b.getLocation(), getCoolantSlots()); - inv.dropItems(b.getLocation(), getOutputSlots()); - } - - processor.endOperation(b); + public void afterBreak(@Nonnull Block b) { removeHologram(b); } }; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/GEOMiner.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/GEOMiner.java index d21815533b..a789a424f5 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/GEOMiner.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/GEOMiner.java @@ -7,6 +7,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.apache.commons.lang.Validate; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -197,18 +198,10 @@ public void onPlayerPlace(BlockPlaceEvent e) { @Nonnull private BlockBreakHandler onBlockBreak() { - return new SimpleBlockBreakHandler() { - + return new MachineBlockBreakHandler(OUTPUT_SLOTS) { @Override - public void onBlockBreak(@Nonnull Block b) { + protected void beforeBreak(@Nonnull Block b) { removeHologram(b); - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), OUTPUT_SLOTS); - } - - processor.endOperation(b); } }; } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java index 04747b4f84..0dbb76e479 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java @@ -8,6 +8,7 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.apache.commons.lang.Validate; import org.bukkit.Location; import org.bukkit.Material; @@ -67,21 +68,7 @@ protected AContainer(ItemGroup itemGroup, SlimefunItemStack item, RecipeType rec @Nonnull protected BlockBreakHandler onBlockBreak() { - return new SimpleBlockBreakHandler() { - - @Override - public void onBlockBreak(Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), getInputSlots()); - inv.dropItems(b.getLocation(), getOutputSlots()); - } - - processor.endOperation(b); - } - - }; + return new MachineBlockBreakHandler(processor, getOutputSlots(), getInputSlots()); } @ParametersAreNonnullByDefault diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java index db5555f6c9..8776424373 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java @@ -7,6 +7,7 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.MachineBlockBreakHandler; import org.apache.commons.lang.Validate; import org.bukkit.Location; import org.bukkit.Material; @@ -89,20 +90,7 @@ public MachineProcessor getMachineProcessor() { @Nonnull protected BlockBreakHandler onBlockBreak() { - return new SimpleBlockBreakHandler() { - - @Override - public void onBlockBreak(Block b) { - BlockMenu inv = BlockStorage.getInventory(b); - - if (inv != null) { - inv.dropItems(b.getLocation(), getInputSlots()); - inv.dropItems(b.getLocation(), getOutputSlots()); - } - - processor.endOperation(b); - } - }; + return new MachineBlockBreakHandler(processor, getInputSlots(), getOutputSlots()); } private void constructMenu(BlockMenuPreset preset) {