Skip to content

Commit

Permalink
Merge remote-tracking branch 'asdflj/master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Dream-Master committed Dec 24, 2022
2 parents f44808a + e27c73b commit b8081f0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 33 deletions.
39 changes: 27 additions & 12 deletions src/main/java/appeng/crafting/CraftingTreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAEItemStack> 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;
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/appeng/me/cache/CraftingGridCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public ImmutableCollection<ICraftingPatternDetails> getCraftingFor(
final ICraftingPatternDetails details,
final int slotIndex,
final World world) {
final ImmutableList<ICraftingPatternDetails> res;
ImmutableList<ICraftingPatternDetails> res;
boolean normalMode = false;
if (details != null && details.canSubstitute()) {
final ImmutableList<ICraftingPatternDetails> substitutions = this.craftableItemSubstitutes.get(whatToCraft);
Expand All @@ -426,6 +426,15 @@ public ImmutableCollection<ICraftingPatternDetails> getCraftingFor(
} else {
res = substitutions;
}
} else if (details == null) {
final ImmutableList<ICraftingPatternDetails> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
40 changes: 34 additions & 6 deletions src/main/java/appeng/util/item/OreListMultiMap.java
Original file line number Diff line number Diff line change
@@ -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<T> {
private ImmutableListMultimap<Integer, T> map;
private final Map<Integer, Collection<ICraftingPatternDetails>> patternHashMap = new HashMap<>();
private ImmutableListMultimap<Integer, T> patternMap;
private ImmutableListMultimap.Builder<Integer, T> builder;

private static Collection<Integer> getAEEquivalents(IAEItemStack stack) {
Expand All @@ -26,15 +28,33 @@ private static Collection<Integer> 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<ICraftingPatternDetails> tmp = patternHashMap.getOrDefault(
((AEItemStack) key).getDefinition().getMyHash(), null);
if (tmp == null) {
ArrayList<ICraftingPatternDetails> 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<Integer, Collection<ICraftingPatternDetails>> collection : this.patternHashMap.entrySet()) {
for (ICraftingPatternDetails details : collection.getValue()) {
builder.put(collection.getKey(), (T) details);
}
}
patternMap = builder.build();
}

public ImmutableList<T> get(IAEItemStack key) {
Expand All @@ -51,8 +71,16 @@ else if (ids.size() == 1) {
}
}

public ImmutableList<ICraftingPatternDetails> getBeSubstitutePattern(IAEItemStack ias) {
if (((AEItemStack) ias).getDefinition() == null) return ImmutableList.of();
return (ImmutableList<ICraftingPatternDetails>)
this.patternMap.get(((AEItemStack) ias).getDefinition().getMyHash());
}

public void clear() {
map = null;
patternMap = null;
patternHashMap.clear();
builder = new Builder<>();
}
}

0 comments on commit b8081f0

Please sign in to comment.