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 3552335 commit 38df5c6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
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 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 @@ -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

0 comments on commit 38df5c6

Please sign in to comment.