Skip to content

Commit

Permalink
Fix crafting job progress bar exceeding 100% with container items (#8085
Browse files Browse the repository at this point in the history
)

Alternative to
#8005.
We simply add container items to the total number of items expected for
the job.

Fixes #7468. Fixes #7420.

---------

Co-authored-by: Mithi83 <[email protected]>
(cherry picked from commit ef161ec)
  • Loading branch information
Technici4n authored and shartte committed Dec 7, 2024
1 parent 68dc628 commit 41f89e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public static KeyCounter[] extractPatternInputs(
IPatternDetails details,
ICraftingInventory sourceInv,
Level level,
KeyCounter expectedOutputs) {
KeyCounter expectedOutputs,
KeyCounter expectedContainerItems) {

// Extract inputs into the container.
var inputs = details.getInputs();
@SuppressWarnings("unchecked")
KeyCounter[] inputHolder = new KeyCounter[inputs.length];
boolean found = true;

Expand All @@ -120,7 +120,7 @@ public static KeyCounter[] extractPatternInputs(
// Container items!
var containerItem = inputs[x].getRemainingKey(template.key());
if (containerItem != null) {
expectedOutputs.add(containerItem, extracted);
expectedContainerItems.add(containerItem, extracted);
}

remainingMultiplier -= extracted;
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/appeng/crafting/execution/CraftingCpuLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ public int executeCrafting(int maxPatterns, CraftingService craftingService, IEn

var details = task.getKey();
var expectedOutputs = new KeyCounter();
var expectedContainerItems = new KeyCounter();
// Contains the inputs for the pattern.
@Nullable
var craftingContainer = CraftingCpuHelper.extractPatternInputs(
details, inventory, level, expectedOutputs);
details, inventory, level, expectedOutputs, expectedContainerItems);

// Try to push to each provider.
for (var provider : craftingService.getProviders(details)) {
Expand All @@ -216,6 +217,11 @@ public int executeCrafting(int maxPatterns, CraftingService craftingService, IEn
job.waitingFor.insert(expectedOutput.getKey(), expectedOutput.getLongValue(),
Actionable.MODULATE);
}
for (var expectedContainerItem : expectedContainerItems) {
job.waitingFor.insert(expectedContainerItem.getKey(), expectedContainerItem.getLongValue(),
Actionable.MODULATE);
job.timeTracker.addMaxItems(expectedContainerItem.getLongValue());
}

cluster.markDirty();

Expand All @@ -231,8 +237,9 @@ public int executeCrafting(int maxPatterns, CraftingService craftingService, IEn

// Prepare next inputs.
expectedOutputs.reset();
expectedContainerItems.reset();
craftingContainer = CraftingCpuHelper.extractPatternInputs(details, inventory,
level, expectedOutputs);
level, expectedOutputs, expectedContainerItems);
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/appeng/crafting/execution/ElapsedTimeTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class ElapsedTimeTracker {

private long lastTime = System.nanoTime();
private long elapsedTime = 0;
private final long startItemCount;
// TODO 1.21 rename: "start item count" is not a good name:
// this item count can increase when container items are scheduled.
private long startItemCount;
private long remainingItemCount;

public ElapsedTimeTracker(long startItemCount) {
Expand All @@ -49,13 +51,23 @@ public CompoundTag writeToNBT() {
return data;
}

void decrementItems(long itemDiff) {
private void updateTime() {
long currentTime = System.nanoTime();
this.elapsedTime = this.elapsedTime + (currentTime - this.lastTime);
this.lastTime = currentTime;
}

void decrementItems(long itemDiff) {
updateTime();
this.remainingItemCount -= itemDiff;
}

void addMaxItems(long itemDiff) {
updateTime();
this.startItemCount += itemDiff;
this.remainingItemCount += itemDiff;
}

public long getElapsedTime() {
if (remainingItemCount > 0) {
return this.elapsedTime + (System.nanoTime() - this.lastTime);
Expand Down

0 comments on commit 41f89e1

Please sign in to comment.