Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/machine inventory drop #4271

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<? extends MachineOperation> processor;
private final int[][] slots;

public MachineBlockBreakHandler(MachineProcessor<? extends MachineOperation> 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) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,19 +123,9 @@ public MachineProcessor<FuelOperation> 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);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,20 +90,7 @@ public MachineProcessor<FuelOperation> 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) {
Expand Down
Loading