From 204ebfead0b49aea933111677957d7120a5437c0 Mon Sep 17 00:00:00 2001 From: asiekierka Date: Mon, 13 Apr 2015 19:40:44 +0200 Subject: [PATCH] rewrite auto workbench algorithm to stop dupe bugs and be smarter about item usage --- .../buildcraft/factory/TileAutoWorkbench.java | 49 +++++++++++++++---- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/common/buildcraft/factory/TileAutoWorkbench.java b/common/buildcraft/factory/TileAutoWorkbench.java index 23e20583..1cbb62eb 100644 --- a/common/buildcraft/factory/TileAutoWorkbench.java +++ b/common/buildcraft/factory/TileAutoWorkbench.java @@ -56,6 +56,7 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory private InventoryCraftResult craftResult = new InventoryCraftResult(); private int[] bindings = new int[9]; + private int[] bindingCounts = new int[9]; private int update = Utils.RANDOM.nextInt(); @@ -326,6 +327,9 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory private void updateCrafting() { progress += UPDATE_TIME; + for (int i = 0; i < 9; i++) { + bindingCounts[i] = 0; + } for (int i = 0; i < 9; i++) { ItemStack comparedStack = craftMatrix.getStackInSlot(i); if (comparedStack == null || comparedStack.getItem() == null) { @@ -334,23 +338,45 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory } if (bindings[i] == -1 || !StackHelper.isMatchingItem(inputInv.getStackInSlot(bindings[i]), comparedStack, true, true)) { - // Scan for a new binding boolean found = false; for (int j = 0; j < 9; j++) { if (j == bindings[i]) { continue; } - if (StackHelper.isMatchingItem(inputInv.getStackInSlot(j), comparedStack, true, false)) { + ItemStack inputInvStack = inputInv.getStackInSlot(j); + + if (StackHelper.isMatchingItem(inputInvStack, comparedStack, true, false) + && inputInvStack.stackSize > bindingCounts[j]) { found = true; bindings[i] = j; + bindingCounts[j]++; break; } } if (!found) { + craftMatrix.isJammed = true; progress = 0; return; } + } else { + bindingCounts[bindings[i]]++; + } + } + + for (int i = 0; i < 9; i++) { + if (bindingCounts[i] > 0) { + ItemStack stack = inputInv.getStackInSlot(i); + if (stack != null && stack.stackSize < bindingCounts[i]) { + // Do not break progress yet, instead give it a chance to rebuild + // It will quit when trying to find a valid binding to "fit in" + for (int j = 0; j < 9; j++) { + if (bindings[j] == i) { + bindings[j] = -1; + } + } + return; + } } } @@ -360,16 +386,19 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory progress = 0; - ItemStack result = craftMatrix.getRecipeOutput(); - ItemStack resultInto = resultInv.getStackInSlot(0); - craftMatrix.setUseBindings(true); - craftSlot.onPickupFromSlot(getInternalPlayer().get(), result); + ItemStack result = craftMatrix.getRecipeOutput(); - if (resultInto == null) { - resultInv.setInventorySlotContents(0, result); - } else { - resultInto.stackSize += result.stackSize; + if (result != null && result.stackSize > 0) { + ItemStack resultInto = resultInv.getStackInSlot(0); + + craftSlot.onPickupFromSlot(getInternalPlayer().get(), result); + + if (resultInto == null) { + resultInv.setInventorySlotContents(0, result); + } else { + resultInto.stackSize += result.stackSize; + } } craftMatrix.setUseBindings(false);