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 4b88ffc
Show file tree
Hide file tree
Showing 3 changed files with 16 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 @@ -22,6 +22,7 @@
import java.util.UUID;
import java.util.function.Consumer;

import appeng.api.stacks.AEKeyType;
import com.google.common.base.Preconditions;

import org.jetbrains.annotations.Nullable;
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
17 changes: 10 additions & 7 deletions src/main/java/appeng/crafting/execution/ElapsedTimeTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package appeng.crafting.execution;

import appeng.api.stacks.AEKeyType;
import net.minecraft.nbt.CompoundTag;

public class ElapsedTimeTracker {
Expand All @@ -30,9 +31,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 +52,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 +68,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 4b88ffc

Please sign in to comment.