Skip to content

Commit 3fd3fce

Browse files
committed
Make deduplicator's hash function also use identity for hashing
1 parent d47e412 commit 3fd3fce

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

neoforge/src/main/java/org/embeddedt/modernfix/neoforge/recipe/IngredientValueDeduplicator.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.embeddedt.modernfix.neoforge.recipe;
22

3+
import com.google.common.math.IntMath;
34
import it.unimi.dsi.fastutil.Hash;
45
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
56
import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
67
import net.minecraft.world.item.ItemStack;
7-
import net.minecraft.world.item.ItemStackLinkedSet;
88
import net.minecraft.world.item.crafting.Ingredient;
99
import org.embeddedt.modernfix.neoforge.mixin.perf.ingredient_item_deduplication.PatchedDataComponentMapAccessor;
1010

@@ -15,7 +15,23 @@ public class IngredientValueDeduplicator {
1515
private static final ObjectOpenCustomHashSet<Ingredient.ItemValue> VALUES = new ObjectOpenCustomHashSet<>(new Hash.Strategy<>() {
1616
@Override
1717
public int hashCode(Ingredient.ItemValue o) {
18-
return o == null ? 0 : ItemStackLinkedSet.TYPE_AND_TAG.hashCode(o.item());
18+
if (o == null) {
19+
return 0;
20+
}
21+
var stack = o.item();
22+
int hash = 31 * stack.getItem().hashCode();
23+
if (stack.getComponents() instanceof PatchedDataComponentMapAccessor comps) {
24+
var patch = comps.mfix$getPatch();
25+
for (var entry : Reference2ObjectMaps.fastIterable(patch)) {
26+
int keyHash = System.identityHashCode(entry.getKey()) & 0xff;
27+
var value = entry.getValue();
28+
int valueHash = value.isPresent() ? System.identityHashCode(value.get()) : 0;
29+
hash += IntMath.pow(31, keyHash) * valueHash;
30+
}
31+
} else {
32+
hash += System.identityHashCode(stack.getComponents());
33+
}
34+
return hash;
1935
}
2036

2137
private boolean areComponentsSame(ItemStack a, ItemStack b) {

0 commit comments

Comments
 (0)