|
| 1 | +package io.github.thebusybiscuit.slimefun4.implementation.handlers; |
| 2 | + |
| 3 | +import com.google.common.base.Preconditions; |
| 4 | +import io.github.thebusybiscuit.slimefun4.core.machines.MachineOperation; |
| 5 | +import io.github.thebusybiscuit.slimefun4.core.machines.MachineProcessor; |
| 6 | +import io.github.thebusybiscuit.slimefun4.implementation.operations.CraftingOperation; |
| 7 | +import me.mrCookieSlime.Slimefun.api.BlockStorage; |
| 8 | +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; |
| 9 | +import org.bukkit.Location; |
| 10 | +import org.bukkit.World; |
| 11 | +import org.bukkit.block.Block; |
| 12 | +import org.bukkit.inventory.ItemStack; |
| 13 | + |
| 14 | +import javax.annotation.Nonnull; |
| 15 | +import javax.annotation.Nullable; |
| 16 | + |
| 17 | +/** |
| 18 | + * A {@link SimpleBlockBreakHandler} given the processor and the slots to drop of a machine. |
| 19 | + * Uses the machine's {@link MachineProcessor} to drop the items that didn't finish processing for crafting recipes |
| 20 | + * The slots field is used to drop items in those slots of block's inventory. |
| 21 | + * |
| 22 | + * @author Vaan1310/Intybyte |
| 23 | + * |
| 24 | + */ |
| 25 | +public class MachineBlockBreakHandler extends SimpleBlockBreakHandler { |
| 26 | + private final MachineProcessor<? extends MachineOperation> processor; |
| 27 | + private final int[][] slots; |
| 28 | + |
| 29 | + public MachineBlockBreakHandler(@Nullable MachineProcessor<?> processor, @Nonnull int[]... slots) { |
| 30 | + Preconditions.checkNotNull(slots, "MachineBlockBreakHandler doesn't allow null slots, you should probably use another type of SimpleBlockBreakHandler or create your own implementation."); |
| 31 | + this.processor = processor; |
| 32 | + this.slots = slots; |
| 33 | + } |
| 34 | + |
| 35 | + public MachineBlockBreakHandler(@Nonnull int[]... slots) { |
| 36 | + this(null, slots); |
| 37 | + } |
| 38 | + |
| 39 | + protected void beforeBreak(@Nonnull Block b) { |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public final void onBlockBreak(@Nonnull Block b) { |
| 45 | + beforeBreak(b); |
| 46 | + BlockMenu inv = BlockStorage.getInventory(b); |
| 47 | + Location loc = b.getLocation(); |
| 48 | + |
| 49 | + // Check if the world where we might want to drop stuff is valid |
| 50 | + World world = loc.getWorld(); |
| 51 | + if (world == null) { |
| 52 | + if (processor != null) { |
| 53 | + processor.endOperation(b); |
| 54 | + } |
| 55 | + afterBreak(b); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (inv != null) { |
| 60 | + for (int[] slotArray : slots) { |
| 61 | + inv.dropItems(loc, slotArray); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (processor == null) { |
| 66 | + afterBreak(b); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + var operation = processor.getOperation(b); |
| 71 | + if (operation == null) { |
| 72 | + afterBreak(b); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // if the processed operation is a crafting recipe give back recipe ingredients, Fixes #4268 |
| 77 | + if (!(operation instanceof CraftingOperation craftingOperation)) { |
| 78 | + processor.endOperation(b); |
| 79 | + afterBreak(b); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + ItemStack[] ingredients = craftingOperation.getIngredients(); |
| 84 | + for (ItemStack ingredient : ingredients) { |
| 85 | + world.dropItemNaturally(loc, ingredient); |
| 86 | + } |
| 87 | + |
| 88 | + processor.endOperation(b); |
| 89 | + afterBreak(b); |
| 90 | + } |
| 91 | + |
| 92 | + protected void afterBreak(@Nonnull Block b) { |
| 93 | + |
| 94 | + } |
| 95 | +} |
0 commit comments