From 4b498082285fe83242229e7029603ee1ed16bcfb Mon Sep 17 00:00:00 2001 From: lordIcocain <62835225+lordIcocain@users.noreply.github.com> Date: Thu, 26 Dec 2024 23:45:40 +0200 Subject: [PATCH] Improve missing mode. (#627) Co-authored-by: Martin Robertz --- .../api/networking/crafting/ICraftingJob.java | 5 +++ .../appeng/crafting/v2/CraftingJobV2.java | 5 +++ .../resolvers/IgnoreMissingItemResolver.java | 2 +- .../appeng/me/cache/CraftingGridCache.java | 1 + .../implementations/CraftingCPUCluster.java | 42 +++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/main/java/appeng/api/networking/crafting/ICraftingJob.java b/src/main/java/appeng/api/networking/crafting/ICraftingJob.java index e8100269a22..22fb33eab47 100644 --- a/src/main/java/appeng/api/networking/crafting/ICraftingJob.java +++ b/src/main/java/appeng/api/networking/crafting/ICraftingJob.java @@ -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; @@ -63,6 +64,10 @@ default boolean supportsCPUCluster(final ICraftingCPU cluster) { return false; } + default CraftingMode getCraftingMode() { + return null; + } + /** * Begins crafting on a CPU cluster */ diff --git a/src/main/java/appeng/crafting/v2/CraftingJobV2.java b/src/main/java/appeng/crafting/v2/CraftingJobV2.java index 980272bed29..c76f06a99d5 100644 --- a/src/main/java/appeng/crafting/v2/CraftingJobV2.java +++ b/src/main/java/appeng/crafting/v2/CraftingJobV2.java @@ -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); diff --git a/src/main/java/appeng/crafting/v2/resolvers/IgnoreMissingItemResolver.java b/src/main/java/appeng/crafting/v2/resolvers/IgnoreMissingItemResolver.java index bd7c2a67e7b..96d336e25c5 100644 --- a/src/main/java/appeng/crafting/v2/resolvers/IgnoreMissingItemResolver.java +++ b/src/main/java/appeng/crafting/v2/resolvers/IgnoreMissingItemResolver.java @@ -80,7 +80,7 @@ public void populatePlan(IItemList 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 } diff --git a/src/main/java/appeng/me/cache/CraftingGridCache.java b/src/main/java/appeng/me/cache/CraftingGridCache.java index 1f3af1c435d..d73c5d823b7 100644 --- a/src/main/java/appeng/me/cache/CraftingGridCache.java +++ b/src/main/java/appeng/me/cache/CraftingGridCache.java @@ -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); } } diff --git a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java index a37e7f694c2..d5f4916735e 100644 --- a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java +++ b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java @@ -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; @@ -138,6 +140,7 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU { private IAEItemStack finalOutput; private boolean waiting = false; private IItemList waitingFor = AEApi.instance().storage().createItemList(); + private IItemList waitingForMissing = AEApi.instance().storage().createItemList(); private long availableStorage = 0; private long usedStorage = 0; private MachineSource machineSrc = null; @@ -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> unreadNotifications = new HashMap<>(); @@ -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? { @@ -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(); @@ -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; @@ -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(); @@ -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(); @@ -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); } @@ -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)); @@ -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()); @@ -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) { @@ -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;