Skip to content

Commit

Permalink
Implement ElapsedTimeTracker to support AEKeyType AmountPerUnit scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Simba98 committed Sep 19, 2024
1 parent f923cf7 commit 2f548f4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/main/java/appeng/crafting/execution/CraftingCpuLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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 @@ -268,7 +269,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 @@ -408,7 +409,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
18 changes: 11 additions & 7 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 @@ -30,9 +32,11 @@ public class ElapsedTimeTracker {
private final 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 @@ -49,11 +53,11 @@ public CompoundTag writeToNBT() {
return data;
}

void decrementItems(long itemDiff) {
void decrementItems(long itemDiff, AEKeyType KeyType) {
long currentTime = System.nanoTime();
this.elapsedTime = this.elapsedTime + (currentTime - this.lastTime);
this.lastTime = currentTime;
this.remainingItemCount -= itemDiff;
this.remainingItemCount -= itemDiff * AEKEY_SCALE_FACTOR / KeyType.getAmountPerUnit();
}

public long getElapsedTime() {
Expand All @@ -65,10 +69,10 @@ public long getElapsedTime() {
}

public long getRemainingItemCount() {
return this.remainingItemCount;
return Math.round(((float) this.remainingItemCount) / AEKEY_SCALE_FACTOR);
}

public long getStartItemCount() {
return this.startItemCount;
return Math.round(((float) this.startItemCount) / AEKEY_SCALE_FACTOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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 @@ -78,10 +79,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

0 comments on commit 2f548f4

Please sign in to comment.