Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ElapsedTimeTracker to support AEKeyType AmountPerUnit scaling #8170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/java/appeng/crafting/execution/CraftingCpuLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import appeng.api.networking.energy.IEnergyService;
import appeng.api.networking.security.IActionSource;
import appeng.api.stacks.AEKey;
import appeng.api.stacks.AEKeyType;
import appeng.api.stacks.GenericStack;
import appeng.api.stacks.KeyCounter;
import appeng.core.AELog;
Expand Down Expand Up @@ -221,7 +222,8 @@ public int executeCrafting(int maxPatterns, CraftingService craftingService, IEn
for (var expectedContainerItem : expectedContainerItems) {
job.waitingFor.insert(expectedContainerItem.getKey(), expectedContainerItem.getLongValue(),
Actionable.MODULATE);
job.timeTracker.addMaxItems(expectedContainerItem.getLongValue());
job.timeTracker.addMaxItems(expectedContainerItem.getLongValue(),
expectedContainerItem.getKey().getType());
}

cluster.markDirty();
Expand Down Expand Up @@ -276,7 +278,7 @@ public long insert(AEKey what, long amount, Actionable type) {
}

if (type == Actionable.MODULATE) {
job.timeTracker.decrementItems(amount);
job.timeTracker.decrementItems(amount, what.getType()); // Process Fluid and Items
job.waitingFor.extract(what, amount, Actionable.MODULATE);
cluster.markDirty();
}
Expand Down Expand Up @@ -416,7 +418,7 @@ public ElapsedTimeTracker getElapsedTimeTracker() {
if (this.job != null) {
return this.job.timeTracker;
} else {
return new ElapsedTimeTracker(0);
return new ElapsedTimeTracker(0, AEKeyType.items());
}
}

Expand Down
24 changes: 14 additions & 10 deletions src/main/java/appeng/crafting/execution/ElapsedTimeTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import net.minecraft.nbt.CompoundTag;

import appeng.api.stacks.AEKeyType;

public class ElapsedTimeTracker {
private static final String NBT_ELAPSED_TIME = "elapsedTime";
private static final String NBT_START_ITEM_COUNT = "startItemCount";
Expand All @@ -32,9 +34,11 @@ public class ElapsedTimeTracker {
private long startItemCount;
private long remainingItemCount;

public ElapsedTimeTracker(long startItemCount) {
this.startItemCount = startItemCount;
this.remainingItemCount = startItemCount;
private static final int AEKEY_SCALE_FACTOR = AEKeyType.fluids().getAmountPerUnit();

public ElapsedTimeTracker(long startItemCount, AEKeyType KeyType) {
this.startItemCount = startItemCount * AEKEY_SCALE_FACTOR / KeyType.getAmountPerUnit();
this.remainingItemCount = startItemCount * AEKEY_SCALE_FACTOR / KeyType.getAmountPerUnit();
}

public ElapsedTimeTracker(CompoundTag data) {
Expand All @@ -57,15 +61,15 @@ private void updateTime() {
this.lastTime = currentTime;
}

void decrementItems(long itemDiff) {
void decrementItems(long itemDiff, AEKeyType KeyType) {
updateTime();
this.remainingItemCount -= itemDiff;
this.remainingItemCount -= itemDiff * AEKEY_SCALE_FACTOR / KeyType.getAmountPerUnit();
}

void addMaxItems(long itemDiff) {
void addMaxItems(long itemDiff, AEKeyType KeyType) {
updateTime();
this.startItemCount += itemDiff;
this.remainingItemCount += itemDiff;
this.startItemCount += itemDiff * AEKEY_SCALE_FACTOR / KeyType.getAmountPerUnit();
this.remainingItemCount += itemDiff * AEKEY_SCALE_FACTOR / KeyType.getAmountPerUnit();
}

public long getElapsedTime() {
Expand All @@ -77,10 +81,10 @@ public long getElapsedTime() {
}

public long getRemainingItemCount() {
return this.remainingItemCount;
return (this.remainingItemCount + AEKEY_SCALE_FACTOR / 2) / AEKEY_SCALE_FACTOR;
}

public long getStartItemCount() {
return this.startItemCount;
return (this.remainingItemCount + AEKEY_SCALE_FACTOR / 2) / AEKEY_SCALE_FACTOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import appeng.api.networking.crafting.ICraftingPlan;
import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.AEKey;
import appeng.api.stacks.AEKeyType;
import appeng.api.stacks.GenericStack;
import appeng.crafting.CraftingLink;
import appeng.crafting.inv.ListCraftingInventory;
Expand Down Expand Up @@ -79,10 +80,10 @@ interface CraftingDifferenceListener {
for (var entry : plan.patternTimes().entrySet()) {
tasks.computeIfAbsent(entry.getKey(), p -> new TaskProgress()).value += entry.getValue();
for (var output : entry.getKey().getOutputs()) {
totalPending += output.amount() * entry.getValue();
totalPending += output.amount() * entry.getValue() * output.what().getAmountPerUnit();
}
}
this.timeTracker = new ElapsedTimeTracker(totalPending);
this.timeTracker = new ElapsedTimeTracker(totalPending, AEKeyType.items());
this.link = link;
this.playerId = playerId;
}
Expand Down