Avoid redundant stock lookups in ME Level Maintainer (#474)

This commit is contained in:
Elios authored and GitHub committed 2026-10-04 08:23:01 +00:00
1 parent e670a0c3c0
commit ff129e46d9
2 files changed
+53 -27

No files matched your search

+1 -1
View File
@@ -35,7 +35,7 @@
*/
dependencies {
api('com.github.GTNewHorizons:NotEnoughItems:2.8.121-GTNH:dev')
api('com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-1040-GTNH:dev')
api('com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-1078-GTNH:dev')
api('com.github.GTNewHorizons:waila:1.19.31:dev')
api("com.github.GTNewHorizons:GTNHLib:0.11.37:dev")
@@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableSet;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.CraftingAllow;
import appeng.api.config.CraftingMode;
import appeng.api.config.PowerMultiplier;
import appeng.api.features.ILevelViewable;
@@ -52,13 +53,13 @@ import appeng.api.storage.data.IAEStack;
import appeng.core.AELog;
import appeng.me.GridAccessException;
import appeng.me.cache.CraftingGridCache;
import appeng.me.cluster.implementations.CraftingCPUCluster;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkTile;
import appeng.tile.inventory.IAEAppEngInventory;
import appeng.tile.inventory.IAEStackInventory;
import appeng.tile.inventory.InvOperation;
import appeng.util.IterationCounter;
import appeng.util.Platform;
import appeng.util.SettingsFrom;
import appeng.util.item.AEItemStack;
@@ -178,7 +179,7 @@ public class TileLevelMaintainer extends AENetworkTile
// crafting tasks that cannot be successfully submitted.
boolean allBusy = true;
for (final ICraftingCPU cpu : craftingGrid.getCpus()) {
if (!cpu.isBusy()) {
if (canAcceptMachineJob(cpu)) {
allBusy = false;
break;
}
@@ -204,40 +205,50 @@ public class TileLevelMaintainer extends AENetworkTile
continue;
}
if (!this.isDone(i)) {
continue;
}
IAEStack<?> craftItem = requests[i].stack.copy();
craftItem.setStackSize(batchSize);
final boolean isCraftable = isCraftable(craftingGrid, craftItem);
if (this.requests[i].state != LevelState.Idle) {
this.updateState(i, LevelState.Idle);
}
if (this.requests[i].link != null) {
this.updateLink(i, null);
}
if (!isCraftable) {
updateState(i, LevelState.NotFound);
}
// A free CPU is required before it is worth looking at the stock:
if (allBusy || !isCraftable) {
continue;
}
// Single hash lookup on the emitable mediums, so it is cheaper
// than reading the stock and can run first.
if (craftingGrid.canEmitFor(craftItem)) {
continue;
}
IMEMonitor monitor = getProxy().getStorage().getMEMonitor(craftItem.getStackType());
if (monitor == null) continue;
IAEStack<?> stackInStorage = monitor.getAvailableItem(craftItem, IterationCounter.fetchNewId());
@SuppressWarnings({ "rawtypes", "unchecked" })
IAEStack<?> stackInStorage = monitor.getStorageList().findPrecise(craftItem);
long stackSize = stackInStorage == null ? 0 : stackInStorage.getStackSize();
boolean isDone = this.isDone(i);
boolean isCraftable = stackInStorage != null && stackInStorage.isCraftable();
boolean shouldCraft = isCraftable && stackSize < quantity;
if (isDone) {
if (this.requests[i].state != LevelState.Idle) {
this.updateState(i, LevelState.Idle);
}
if (this.requests[i].link != null) {
this.updateLink(i, null);
}
if (!isCraftable) {
updateState(i, LevelState.NotFound);
}
}
if (allBusy || !isDone || !shouldCraft) {
if (stackSize >= quantity) {
continue;
}
if (craftingGrid.canEmitFor(stackInStorage)) {
continue;
}
if (craftingGrid.isRequesting(stackInStorage)) {
// Walking every CPU to see whether the item is already being
// made is comparatively expensive
if (craftingGrid.isRequesting(craftItem)) {
continue;
}
@@ -304,6 +315,21 @@ public class TileLevelMaintainer extends AENetworkTile
return TickRateModulation.SAME;
}
private static boolean isCraftable(final ICraftingGrid craftingGrid, final IAEStack<?> stack) {
if (stack == null) return false;
return craftingGrid.canEmitFor(stack) || craftingGrid.getCraftingMultiPatterns().containsKey(stack);
}
private static boolean canAcceptMachineJob(final ICraftingCPU cpu) {
if (cpu.isBusy()) {
return false;
}
if (!(cpu instanceof CraftingCPUCluster cluster)) {
return true;
}
return cluster.isActive() && cluster.getCraftingAllowMode() != CraftingAllow.ONLY_PLAYER;
}
@Override
public void saveChanges() {
super.saveChanges();