Skip to content

Commit

Permalink
Flush crafting output buffer in same tick
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rubensworks committed Apr 13, 2022
1 parent 5afb2b6 commit febd15d
Showing 1 changed file with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,7 @@ public void update(INetwork network, IPartNetwork partNetwork, PartTarget target
}

// Push any pending output ingredients into the network
ListIterator<IngredientInstanceWrapper<?, ?>> 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()) {
Expand Down Expand Up @@ -744,6 +735,11 @@ protected <C, T, M> C wrapStorageCapability(Capability<C> capability, Ingredient
@Override
public <T, M> void addResult(IngredientComponent<T, M> 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) {
Expand Down Expand Up @@ -787,5 +783,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<IngredientInstanceWrapper<?, ?>> 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());
}
}
}
}

0 comments on commit febd15d

Please sign in to comment.