diff --git a/resources/changelog/1.16.5-1.0.21.txt b/resources/changelog/1.16.5-1.0.21.txt new file mode 100644 index 00000000..c7b4ea1e --- /dev/null +++ b/resources/changelog/1.16.5-1.0.21.txt @@ -0,0 +1,10 @@ +As always, don't forget to backup your world before updating! +Requires CyclopsCore version 1.11.6 or higher. + +Fixes: +* Flush crafting output buffer in same tick + This resolves cases where the crafting result may be missed + and get stuck in a pending state if it is extracted from the + target storage too quickly. + Closes #81 + diff --git a/src/main/java/org/cyclops/integratedcrafting/part/PartTypeInterfaceCrafting.java b/src/main/java/org/cyclops/integratedcrafting/part/PartTypeInterfaceCrafting.java index 8dc76043..e0059716 100644 --- a/src/main/java/org/cyclops/integratedcrafting/part/PartTypeInterfaceCrafting.java +++ b/src/main/java/org/cyclops/integratedcrafting/part/PartTypeInterfaceCrafting.java @@ -236,16 +236,7 @@ public void update(INetwork network, IPartNetwork partNetwork, PartTarget target } // Push any pending output ingredients into the network - ListIterator> outputBufferIt = state.getInventoryOutputBuffer().listIterator(); - while (outputBufferIt.hasNext()) { - IngredientInstanceWrapper newWrapper = insertIntoNetwork(outputBufferIt.next(), - network, state.getChannelCrafting()); - if (newWrapper == null) { - outputBufferIt.remove(); - } else { - outputBufferIt.set(newWrapper); - } - } + state.flushInventoryOutputBuffer(network); // Block job ticking if there still are outputs in our crafting result buffer. if (state.getInventoryOutputBuffer().isEmpty()) { @@ -742,6 +733,11 @@ protected C wrapStorageCapability(Capability capability, Ingredient @Override public void addResult(IngredientComponent ingredientComponent, T instance) { this.getInventoryOutputBuffer().add(new IngredientInstanceWrapper<>(ingredientComponent, instance)); + + // Try to flush buffer immediately + if (this.network != null) { + this.flushInventoryOutputBuffer(this.network); + } } public void setIngredientComponentTargetSideOverride(IngredientComponent ingredientComponent, Direction side) { @@ -785,5 +781,29 @@ public void setDisableCraftingCheck(boolean disableCraftingCheck) { public boolean isDisableCraftingCheck() { return disableCraftingCheck; } + + public void flushInventoryOutputBuffer(INetwork network) { + // Try to insert each ingredient in the buffer into the network. + boolean changed = false; + ListIterator> outputBufferIt = this.getInventoryOutputBuffer().listIterator(); + while (outputBufferIt.hasNext()) { + IngredientInstanceWrapper oldWrapper = outputBufferIt.next(); + IngredientInstanceWrapper newWrapper = insertIntoNetwork(oldWrapper, + network, this.getChannelCrafting()); + if (newWrapper != oldWrapper) { + changed = true; + } + if (newWrapper == null) { + outputBufferIt.remove(); + } else { + outputBufferIt.set(newWrapper); + } + } + + // If at least one ingredient was inserted, force a sync observer update in the network. + if (changed) { + CraftingHelpers.beforeCalculateCraftingJobs(network, getChannelCrafting()); + } + } } }