Skip to content

Commit 2b42705

Browse files
committed
Deduplicate tag ingredient EmiStack lists
1 parent 9da1433 commit 2b42705

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

xplat/src/main/java/dev/emi/emi/api/stack/TagEmiIngredient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public List<EmiStack> getEmiStacks() {
7979
return stacks;
8080
}
8181

82+
@ApiStatus.Internal
83+
public void setEmiStacks(List<EmiStack> stacks) {
84+
this.stacks = stacks;
85+
}
86+
8287
@Override
8388
public long getAmount() {
8489
return amount;

xplat/src/main/java/dev/emi/emi/registry/EmiRecipes.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import java.util.function.Predicate;
99
import java.util.stream.Collectors;
1010

11+
import dev.emi.emi.api.stack.TagEmiIngredient;
12+
import dev.emi.emi.util.EmiStackListEqualityStrategy;
13+
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
14+
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
1115
import org.jetbrains.annotations.Nullable;
1216

1317
import com.google.common.collect.Iterables;
@@ -80,6 +84,20 @@ public static void clear() {
8084
}
8185
}
8286

87+
private static void deduplicateTagInputs(List<EmiRecipe> recipes) {
88+
ObjectOpenCustomHashSet<List<EmiStack>> stackLists = new ObjectOpenCustomHashSet<>(EmiStackListEqualityStrategy.INSTANCE);
89+
for (EmiRecipe recipe : recipes) {
90+
List<EmiIngredient> recipeInputs = recipe.getInputs();
91+
for (EmiIngredient recipeInput : recipeInputs) {
92+
if (recipeInput instanceof TagEmiIngredient tagIngredient) {
93+
List<EmiStack> stackList = tagIngredient.getEmiStacks();
94+
List<EmiStack> canonicalStackList = stackLists.addOrGet(stackList);
95+
tagIngredient.setEmiStacks(canonicalStackList);
96+
}
97+
}
98+
}
99+
}
100+
83101
public static void bake() {
84102
long start = System.currentTimeMillis();
85103
recipes.addAll(EmiData.recipes.stream().map(r -> r.get()).toList());
@@ -103,6 +121,7 @@ public static void bake() {
103121
}
104122
return true;
105123
}).toList();
124+
deduplicateTagInputs(filtered);
106125
Map<EmiRecipeCategory, List<EmiIngredient>> filteredWorkstations = Maps.newHashMap();
107126
for (Map.Entry<EmiRecipeCategory, List<EmiIngredient>> entry : workstations.entrySet()) {
108127
List<EmiIngredient> w = entry.getValue().stream().filter(s -> !EmiHidden.isDisabled(s)).toList();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dev.emi.emi.util;
2+
3+
import dev.emi.emi.api.stack.Comparison;
4+
import dev.emi.emi.api.stack.EmiStack;
5+
import it.unimi.dsi.fastutil.Hash;
6+
7+
import java.util.List;
8+
9+
public final class EmiStackListEqualityStrategy implements Hash.Strategy<List<EmiStack>> {
10+
public static final EmiStackListEqualityStrategy INSTANCE = new EmiStackListEqualityStrategy();
11+
12+
private EmiStackListEqualityStrategy() {}
13+
14+
@Override
15+
public int hashCode(List<EmiStack> o) {
16+
return o.hashCode();
17+
}
18+
19+
private boolean stacksIdentical(EmiStack a, EmiStack b) {
20+
if (a == b) {
21+
return true;
22+
}
23+
if (a == null || b == null) {
24+
return false;
25+
}
26+
// Check regular properties like item/components first
27+
if(!a.isEqual(b, Comparison.compareComponents())) {
28+
return false;
29+
}
30+
// We only need to be more strict when comparing two stacks that are not empty
31+
if(a.isEmpty()) {
32+
return true;
33+
}
34+
return stacksIdentical(a.getRemainder(), b.getRemainder()) &&
35+
a.getChance() == b.getChance() &&
36+
a.getAmount() == b.getAmount();
37+
}
38+
39+
@Override
40+
public boolean equals(List<EmiStack> a, List<EmiStack> b) {
41+
if(a == b) {
42+
return true;
43+
}
44+
if(a == null || b == null) {
45+
return false;
46+
}
47+
if (a.size() != b.size()) {
48+
return false;
49+
}
50+
int size = a.size();
51+
for (int i = 0; i < size; i++) {
52+
if (!stacksIdentical(a.get(i), b.get(i))) {
53+
return false;
54+
}
55+
}
56+
return true;
57+
}
58+
}

0 commit comments

Comments
 (0)