diff --git a/src/main/java/binnie/core/machines/MachineUtil.java b/src/main/java/binnie/core/machines/MachineUtil.java index 55e3e8ba..290f6941 100644 --- a/src/main/java/binnie/core/machines/MachineUtil.java +++ b/src/main/java/binnie/core/machines/MachineUtil.java @@ -8,6 +8,7 @@ import binnie.core.machines.power.ITankMachine; import binnie.core.machines.power.PowerSystem; import binnie.core.util.ItemStackSet; import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.ItemStackHelper; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidTank; @@ -59,8 +60,8 @@ public class MachineUtil { this.setStack(slot, null); } - public ItemStack decreaseStack(final int slotWood, final int amount) { - return this.getInventory().decrStackSize(slotWood, amount); + public ItemStack decreaseStack(final int slot, final int amount) { + return this.getInventory().decrStackSize(slot, amount); } public void setStack(final int slot, final ItemStack stack) { @@ -78,8 +79,7 @@ public class MachineUtil { } else { final ItemStack merge = this.getStack(slot); if (merge.isItemEqual(addition) && merge.stackSize + addition.stackSize <= merge.getMaxStackSize()) { - final ItemStack itemStack = merge; - itemStack.stackSize += addition.stackSize; + merge.stackSize += addition.stackSize; this.setStack(slot, merge); } } @@ -114,16 +114,20 @@ public class MachineUtil { return (this.getTank(tankInput).getFluid() == null) ? null : this.getTank(tankInput).getFluid(); } - public ItemStack[] getStacks(final int[] slotGrains) { - final ItemStack[] stacks = new ItemStack[slotGrains.length]; - for (int i = 0; i < slotGrains.length; ++i) { - stacks[i] = this.getStack(slotGrains[i]); - } - return stacks; + public ItemStack[] getStacks(final int[] slots) { + return getStacks(slots, false); } - public ItemStack hasIngredients(final int recipe, final int[] inventory) { - return null; + public ItemStack[] getStacks(final int[] slots, boolean copy) { + final ItemStack[] stacks = new ItemStack[slots.length]; + for (int i = 0; i < slots.length; ++i) { + ItemStack stack = this.getStack(slots[i]); + if (copy && stack != null) { + stack = stack.copy(); + } + stacks[i] = stack; + } + return stacks; } public boolean hasIngredients(final int[] recipe, final int[] inventory) { @@ -135,6 +139,32 @@ public class MachineUtil { return requiredStacks.isEmpty(); } + public boolean removeIngredients(final int[] recipe, final int[] inventorySlots) { + if (!hasIngredients(recipe, inventorySlots)) { + return false; + } + + List requiredStacks = this.getNonNullStacks(recipe, true); + for (ItemStack requiredStack : requiredStacks) { + IInventory inventory = this.getInventory(); + for (int slot : inventorySlots) { + ItemStack stackInSlot = this.getStack(slot); + if (ItemStack.areItemsEqual(requiredStack, stackInSlot) && ItemStack.areItemStackTagsEqual(requiredStack, stackInSlot)) { + if (requiredStack.stackSize >= stackInSlot.stackSize) { + requiredStack.stackSize -= stackInSlot.stackSize; + inventory.removeStackFromSlot(slot); + } else { + stackInSlot.stackSize -= requiredStack.stackSize; + requiredStack.stackSize = 0; + break; + } + } + } + } + + return true; + } + public void useEnergyMJ(final float powerUsage) { this.getPoweredMachine().getInterface().useEnergy(PowerSystem.MJ, powerUsage, true); } @@ -164,9 +194,13 @@ public class MachineUtil { return this.machine.getInterface(IProcess.class); } - public List getNonNullStacks(final int[] slotacclimatiser) { + public List getNonNullStacks(final int[] slots) { + return getNonNullStacks(slots, false); + } + + public List getNonNullStacks(final int[] slots, boolean copy) { final List stacks = new ArrayList<>(); - for (final ItemStack stack : this.getStacks(slotacclimatiser)) { + for (final ItemStack stack : this.getStacks(slots, copy)) { if (stack != null) { stacks.add(stack); } diff --git a/src/main/java/binnie/core/util/ItemStackSet.java b/src/main/java/binnie/core/util/ItemStackSet.java index 03815c9d..ae93c476 100644 --- a/src/main/java/binnie/core/util/ItemStackSet.java +++ b/src/main/java/binnie/core/util/ItemStackSet.java @@ -22,7 +22,7 @@ public class ItemStackSet implements Set { protected ItemStack getExisting(final ItemStack stack) { for (final ItemStack stack2 : this.itemStacks) { - if (stack2.isItemEqual(stack)) { + if (ItemStack.areItemsEqual(stack2, stack) && ItemStack.areItemStackTagsEqual(stack2, stack)) { return stack2; } } diff --git a/src/main/java/binnie/extratrees/machines/brewery/BreweryLogic.java b/src/main/java/binnie/extratrees/machines/brewery/BreweryLogic.java index 050ce53d..e1e31aff 100644 --- a/src/main/java/binnie/extratrees/machines/brewery/BreweryLogic.java +++ b/src/main/java/binnie/extratrees/machines/brewery/BreweryLogic.java @@ -11,9 +11,11 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fml.relauncher.Side; +import javax.annotation.Nullable; import java.util.Map; public class BreweryLogic extends ComponentProcessSetCost implements IProcess, INetwork.GuiNBT { + @Nullable public BreweryCrafting currentCrafting; public BreweryLogic(final Machine machine) { @@ -65,6 +67,7 @@ public class BreweryLogic extends ComponentProcessSetCost implements IProcess, I output = output.copy(); output.amount = 1000; this.getUtil().fillTank(BreweryMachine.TANK_OUTPUT, output); + this.currentCrafting = null; } } @@ -75,6 +78,7 @@ public class BreweryLogic extends ComponentProcessSetCost implements IProcess, I final FluidStack stack = this.getUtil().drainTank(BreweryMachine.TANK_INPUT, 1000); this.currentCrafting = this.getInputCrafting(); this.currentCrafting.inputFluid = stack; + this.getUtil().removeIngredients(new int[]{0, 1, 2, 3, 4}, BreweryMachine.SLOTS_INVENTORY); } }