From e27c73bbaff5fbc8cd41c925c7a8fccf3ebcc16a Mon Sep 17 00:00:00 2001 From: asdflj <854865755@qq.com> Date: Wed, 21 Dec 2022 20:34:15 +0800 Subject: [PATCH] is all done? --- .../appeng/crafting/CraftingTreeNode.java | 39 +++++++++++----- .../appeng/me/cache/CraftingGridCache.java | 11 ++++- .../implementations/CraftingCPUCluster.java | 44 +++++++++++++------ .../appeng/util/item/OreListMultiMap.java | 40 ++++++++++++++--- 4 files changed, 101 insertions(+), 33 deletions(-) diff --git a/src/main/java/appeng/crafting/CraftingTreeNode.java b/src/main/java/appeng/crafting/CraftingTreeNode.java index 976fdddf099..847593a8589 100644 --- a/src/main/java/appeng/crafting/CraftingTreeNode.java +++ b/src/main/java/appeng/crafting/CraftingTreeNode.java @@ -158,23 +158,38 @@ IAEItemStack request(final MECraftingInventory inv, long l, final BaseActionSour } } } else { - final IAEItemStack available = inv.extractItems(this.what, Actionable.MODULATE, src); + final Collection itemList; + if (this.parent != null && this.parent.details.canSubstitute()) { + itemList = inv.getItemList().findFuzzy(this.what, FuzzyMode.IGNORE_ALL); + } else { + itemList = Lists.newArrayList(); - if (available != null) { - if (!this.exhausted) { - final IAEItemStack is = this.job.checkUse(available); + final IAEItemStack item = inv.getItemList().findPrecise(this.what); - if (is != null) { - thingsUsed.add(is.copy()); - this.used.add(is); - } + if (item != null) { + itemList.add(item); } + } + for (IAEItemStack ias : itemList) { + IAEItemStack tmp = ias.copy(); + tmp.setStackSize(what.getStackSize()); + final IAEItemStack available = inv.extractItems(tmp, Actionable.MODULATE, src); + if (available != null) { + if (!this.exhausted) { + final IAEItemStack is = this.job.checkUse(available); - this.bytes += available.getStackSize(); - l -= available.getStackSize(); + if (is != null) { + thingsUsed.add(is.copy()); + this.used.add(is); + } + } - if (l == 0) { - return available; + this.bytes += available.getStackSize(); + l -= available.getStackSize(); + + if (l == 0) { + return available; + } } } } diff --git a/src/main/java/appeng/me/cache/CraftingGridCache.java b/src/main/java/appeng/me/cache/CraftingGridCache.java index 465453d0653..b81fc4cf68a 100644 --- a/src/main/java/appeng/me/cache/CraftingGridCache.java +++ b/src/main/java/appeng/me/cache/CraftingGridCache.java @@ -416,7 +416,7 @@ public ImmutableCollection getCraftingFor( final ICraftingPatternDetails details, final int slotIndex, final World world) { - final ImmutableList res; + ImmutableList res; boolean normalMode = false; if (details != null && details.canSubstitute()) { final ImmutableList substitutions = this.craftableItemSubstitutes.get(whatToCraft); @@ -426,6 +426,15 @@ public ImmutableCollection getCraftingFor( } else { res = substitutions; } + } else if (details == null) { + final ImmutableList substitutions = + this.craftableItemSubstitutes.getBeSubstitutePattern(whatToCraft); + if (substitutions.isEmpty()) { + res = this.craftableItems.get(whatToCraft); + normalMode = true; + } else { + res = this.craftableItems.get(whatToCraft); + } } else { res = this.craftableItems.get(whatToCraft); } diff --git a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java index 2a3f1e0d001..fdf46c08e39 100644 --- a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java +++ b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java @@ -432,12 +432,21 @@ private boolean canCraft(final ICraftingPatternDetails details, final IAEItemSta return false; } } else { - final IAEItemStack ais = this.inventory.extractItems(g.copy(), Actionable.SIMULATE, this.machineSrc); - final ItemStack is = ais == null ? null : ais.getItemStack(); + boolean found = false; + for (IAEItemStack fuzz : this.inventory.getItemList().findFuzzy(g, FuzzyMode.IGNORE_ALL)) { + if (fuzz.getStackSize() <= 0) continue; + fuzz = fuzz.copy(); + fuzz.setStackSize(g.getStackSize()); + final IAEItemStack ais = this.inventory.extractItems(fuzz, Actionable.SIMULATE, this.machineSrc); + if (ais == null || ais.getStackSize() == 0) continue; + final ItemStack is = ais.getItemStack(); - if (is == null || is.stackSize < g.getStackSize()) { - return false; + if (is.stackSize == g.getStackSize()) { + found = true; + break; + } } + if (!found) return false; } } @@ -589,16 +598,23 @@ private void executeCrafting(final IEnergyGrid eg, final CraftingGridCache cc) { } } } else { - final IAEItemStack ais = this.inventory.extractItems( - input[x].copy(), Actionable.MODULATE, this.machineSrc); - final ItemStack is = ais == null ? null : ais.getItemStack(); - - if (is != null) { - this.postChange(input[x], this.machineSrc); - ic.setInventorySlotContents(x, is); - if (is.stackSize == input[x].getStackSize()) { - found = true; - continue; + for (IAEItemStack fuzz : this.inventory + .getItemList() + .findFuzzy(input[x], FuzzyMode.IGNORE_ALL)) { + if (fuzz.getStackSize() == 0) continue; + fuzz = fuzz.copy(); + fuzz.setStackSize(input[x].getStackSize()); + final IAEItemStack ais = this.inventory.extractItems( + fuzz, Actionable.MODULATE, this.machineSrc); + final ItemStack is = ais == null ? null : ais.getItemStack(); + + if (is != null) { + this.postChange(fuzz, this.machineSrc); + ic.setInventorySlotContents(x, is); + if (is.stackSize == input[x].getStackSize()) { + found = true; + break; + } } } } diff --git a/src/main/java/appeng/util/item/OreListMultiMap.java b/src/main/java/appeng/util/item/OreListMultiMap.java index 5fce84355e5..3e844942aad 100644 --- a/src/main/java/appeng/util/item/OreListMultiMap.java +++ b/src/main/java/appeng/util/item/OreListMultiMap.java @@ -1,15 +1,17 @@ package appeng.util.item; +import appeng.api.networking.crafting.ICraftingPatternDetails; import appeng.api.storage.data.IAEItemStack; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableListMultimap.Builder; -import java.util.Collection; -import java.util.Collections; -import java.util.Set; +import java.util.*; +import java.util.Map.Entry; public class OreListMultiMap { private ImmutableListMultimap map; + private final Map> patternHashMap = new HashMap<>(); + private ImmutableListMultimap patternMap; private ImmutableListMultimap.Builder builder; private static Collection getAEEquivalents(IAEItemStack stack) { @@ -26,15 +28,33 @@ private static Collection getAEEquivalents(IAEItemStack stack) { return ids; } - public void put(IAEItemStack key, T val) { + public void put(IAEItemStack key, ICraftingPatternDetails val) { + if (((AEItemStack) key).getDefinition() != null) { + Collection tmp = patternHashMap.getOrDefault( + ((AEItemStack) key).getDefinition().getMyHash(), null); + if (tmp == null) { + ArrayList list = new ArrayList<>(); + list.add(val); + patternHashMap.put(((AEItemStack) key).getDefinition().getMyHash(), list); + } else { + tmp.add(val); + } + } + for (Integer realKey : getAEEquivalents(key)) { - builder.put(realKey, val); + builder.put(realKey, (T) val); } } public void freeze() { map = builder.build(); - builder = null; + builder = new Builder<>(); + for (Entry> collection : this.patternHashMap.entrySet()) { + for (ICraftingPatternDetails details : collection.getValue()) { + builder.put(collection.getKey(), (T) details); + } + } + patternMap = builder.build(); } public ImmutableList get(IAEItemStack key) { @@ -51,8 +71,16 @@ else if (ids.size() == 1) { } } + public ImmutableList getBeSubstitutePattern(IAEItemStack ias) { + if (((AEItemStack) ias).getDefinition() == null) return ImmutableList.of(); + return (ImmutableList) + this.patternMap.get(((AEItemStack) ias).getDefinition().getMyHash()); + } + public void clear() { map = null; + patternMap = null; + patternHashMap.clear(); builder = new Builder<>(); } }