Skip to content

Commit

Permalink
Improve missing mode. (#627)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <dream-master@gmx.net>
  • Loading branch information
lordIcocain and Dream-Master authored Dec 26, 2024
1 parent 35c33df commit 4b49808
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.concurrent.Future;

import appeng.api.config.CraftingMode;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
Expand Down Expand Up @@ -63,6 +64,10 @@ default boolean supportsCPUCluster(final ICraftingCPU cluster) {
return false;
}

default CraftingMode getCraftingMode() {
return null;
}

/**
* Begins crafting on a CPU cluster
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/appeng/crafting/v2/CraftingJobV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public static CraftingJobV2 deserialize(World world, ByteBuf buffer) {
return job;
}

@Override
public CraftingMode getCraftingMode() {
return this.originalRequest.craftingMode;
}

public ByteBuf serialize() {
try {
final CraftingTreeSerializer serializer = new CraftingTreeSerializer(context.world);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void populatePlan(IItemList<IAEItemStack> targetPlan) {
@Override
public void startOnCpu(CraftingContext context, CraftingCPUCluster cpuCluster,
MECraftingInventory craftingInv) {
cpuCluster.addEmitable(this.request.stack.copy());
cpuCluster.addEmitable(this.request.stack.copy().setStackSize(fulfilled));
// It's just called that, and it has little to do with the level emitter
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/appeng/me/cache/CraftingGridCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void onUpdateTick() {
this.craftingLinks.values().removeIf(craftingLinkNexus -> craftingLinkNexus.isDead(this.grid, this));

for (final CraftingCPUCluster cpu : this.craftingCPUClusters) {
cpu.tryExtractItems();
cpu.updateCraftingLogic(this.grid, this.energyGrid, this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@
import net.minecraft.world.WorldServer;

import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.logging.log4j.Level;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.CraftingMode;
import appeng.api.config.FuzzyMode;
import appeng.api.config.PowerMultiplier;
import appeng.api.config.Upgrades;
Expand Down Expand Up @@ -138,6 +140,7 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU {
private IAEItemStack finalOutput;
private boolean waiting = false;
private IItemList<IAEItemStack> waitingFor = AEApi.instance().storage().createItemList();
private IItemList<IAEItemStack> waitingForMissing = AEApi.instance().storage().createItemList();
private long availableStorage = 0;
private long usedStorage = 0;
private MachineSource machineSrc = null;
Expand All @@ -152,6 +155,8 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU {
private long startItemCount;
private long remainingItemCount;
private long numsOfOutput;
private int countToTryExtractItems;
private boolean isMissingMode;

private final Map<String, List<CraftNotification>> unreadNotifications = new HashMap<>();

Expand Down Expand Up @@ -335,6 +340,7 @@ public IAEStack injectItems(final IAEStack input, final Actionable type, final B

final IAEItemStack what = (IAEItemStack) input.copy();
final IAEItemStack is = this.waitingFor.findPrecise(what);
final IAEItemStack ism = this.waitingForMissing.findPrecise(what);

if (type == Actionable.SIMULATE) // causes crafting to lock up?
{
Expand Down Expand Up @@ -375,6 +381,7 @@ public IAEStack injectItems(final IAEStack input, final Actionable type, final B

if (is.getStackSize() >= what.getStackSize()) {
is.decStackSize(what.getStackSize());
if (ism != null) ism.decStackSize(what.getStackSize());

this.updateElapsedTime(what);
this.markDirty();
Expand Down Expand Up @@ -411,6 +418,7 @@ public IAEStack injectItems(final IAEStack input, final Actionable type, final B
what.decStackSize(is.getStackSize());

is.setStackSize(0);
if (ism != null) ism.setStackSize(0);

if (Objects.equals(finalOutput, insert)) {
IAEStack leftover = input;
Expand Down Expand Up @@ -956,6 +964,7 @@ public ICraftingLink submitJob(final IGrid g, final ICraftingJob job, final Base
this.isComplete = false;
this.usedStorage = job.getByteTotal();
this.numsOfOutput = job.getOutput().getStackSize();
this.isMissingMode = job.getCraftingMode() == CraftingMode.IGNORE_MISSING;
this.markDirty();

this.updateCPU();
Expand Down Expand Up @@ -1046,6 +1055,7 @@ public ICraftingLink mergeJob(final IGrid g, final ICraftingJob job, final BaseA
this.finalOutput.add(job.getOutput());
this.usedStorage += job.getByteTotal();
this.numsOfOutput += job.getOutput().getStackSize();
this.isMissingMode = job.getCraftingMode() == CraftingMode.IGNORE_MISSING;

this.prepareStepCount();
this.markDirty();
Expand Down Expand Up @@ -1179,6 +1189,7 @@ public void addStorage(final IAEItemStack extractItems) {

public void addEmitable(final IAEItemStack i) {
this.waitingFor.add(i);
this.waitingForMissing.add(i);
this.postCraftingStatusChange(i);
}

Expand Down Expand Up @@ -1251,6 +1262,7 @@ public void writeToNBT(final NBTTagCompound data) {
data.setBoolean("isComplete", this.isComplete);
data.setLong("usedStorage", this.usedStorage);
data.setLong("numsOfOutput", this.numsOfOutput);
data.setBoolean("isMissingMode", this.isMissingMode);
try {
data.setTag("craftCompleteListeners", persistListeners(1, craftCompleteListeners));
data.setTag("onCancelListeners", persistListeners(0, craftCancelListeners));
Expand Down Expand Up @@ -1300,6 +1312,7 @@ public void writeToNBT(final NBTTagCompound data) {
data.setTag("tasks", list);

data.setTag("waitingFor", this.writeList(this.waitingFor));
data.setTag("waitingForMissing", this.writeList(this.waitingForMissing));

data.setLong("elapsedTime", this.getElapsedTime());
data.setLong("startItemCount", this.getStartItemCount());
Expand Down Expand Up @@ -1400,12 +1413,14 @@ public void readFromNBT(final NBTTagCompound data) {
for (final IAEItemStack is : this.waitingFor) {
this.postCraftingStatusChange(is.copy());
}
this.waitingForMissing = this.readList((NBTTagList) data.getTag("waitingForMissing"));

this.lastTime = System.nanoTime();
this.elapsedTime = data.getLong("elapsedTime");
this.startItemCount = data.getLong("startItemCount");
this.remainingItemCount = data.getLong("remainingItemCount");
this.numsOfOutput = data.getLong("numsOfOutput");
this.isMissingMode = data.getBoolean("isMissingMode");

NBTBase tag = data.getTag("playerNameList");
if (tag instanceof NBTTagList ntl) {
Expand Down Expand Up @@ -1587,6 +1602,33 @@ public int getRemainingOperations() {
}
}

public void tryExtractItems() {
if (!isMissingMode || this.waitingForMissing.isEmpty()) return;
if (countToTryExtractItems > 1200) {
countToTryExtractItems = 0;
for (IAEItemStack waitingForItem : this.waitingForMissing) {
final IGrid grid = this.getGrid();
if (grid != null) {
final IStorageGrid pg = grid.getCache(IStorageGrid.class);
if (pg != null) {
IAEItemStack extractedItems = pg.getItemInventory()
.extractItems(waitingForItem, Actionable.MODULATE, this.machineSrc);
if (extractedItems != null) {
IAEStack notInjected = injectItems(extractedItems, Actionable.MODULATE, this.machineSrc);
if (notInjected != null) { // not sure if this even need, but still
AELog.logSimple(Level.INFO, "MISSING MODE OVERFLOW! TELL DEVS ASAP!");
pg.getItemInventory()
.injectItems((IAEItemStack) notInjected, Actionable.MODULATE, this.machineSrc);
}
}
}
}
}
} else {
countToTryExtractItems++;
}
}

private static class TaskProgress {

private long value;
Expand Down

0 comments on commit 4b49808

Please sign in to comment.