Skip to content

Commit 892d7bb

Browse files
committed
properly intern accounted stacks
1 parent 487867c commit 892d7bb

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/main/java/org/spongepowered/common/item/recipe/book/SpongeStackedItemContents.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
*/
2525
package org.spongepowered.common.item.recipe.book;
2626

27-
import it.unimi.dsi.fastutil.ints.Int2ReferenceMap;
28-
import it.unimi.dsi.fastutil.ints.Int2ReferenceOpenHashMap;
27+
import it.unimi.dsi.fastutil.Hash;
28+
import it.unimi.dsi.fastutil.objects.Object2ReferenceMap;
29+
import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenCustomHashMap;
2930
import net.minecraft.core.Holder;
3031
import net.minecraft.world.entity.player.StackedContents;
3132
import net.minecraft.world.entity.player.StackedItemContents;
@@ -37,18 +38,31 @@
3738
import org.spongepowered.common.bridge.world.item.crafting.PlacementInfoBridge;
3839

3940
import java.util.List;
41+
import java.util.function.Function;
4042

4143
public class SpongeStackedItemContents extends StackedItemContents {
4244

43-
private final Int2ReferenceMap<ItemStack> uniqueStackMap = new Int2ReferenceOpenHashMap<>();
45+
private static final Hash.Strategy<ItemStack> STACK_HASH_STRATEGY = new Hash.Strategy<>() {
46+
@Override
47+
public int hashCode(final ItemStack o) {
48+
return ItemStack.hashItemAndComponents(o);
49+
}
50+
51+
@Override
52+
public boolean equals(final @Nullable ItemStack a, final @Nullable ItemStack b) {
53+
return (a == b) || (a != null && b != null && ItemStack.isSameItemSameComponents(a, b));
54+
}
55+
};
56+
57+
private final Object2ReferenceMap<ItemStack, ItemStack> stackInterner = new Object2ReferenceOpenCustomHashMap<>(STACK_HASH_STRATEGY);
4458
private final StackedContents<ItemStack> stackedContents = new StackedContents<>();
4559

4660
@Override
4761
public void accountStack(final ItemStack stack, final int maxStackSize) {
4862
if (!stack.isEmpty()) {
49-
final ItemStack stackToAccount = this.uniqueStackMap.computeIfAbsent(
50-
ItemStack.hashItemAndComponents(stack),
51-
$ -> stack);
63+
// StackedContents works on Reference2IntMap, so if we meet stack which "same" copy
64+
// has already been accounted we would need to provide the stack that was met first.
65+
final ItemStack stackToAccount = this.stackInterner.computeIfAbsent(stack, Function.identity());
5266
this.stackedContents.account(stackToAccount, Math.min(stack.getCount(), maxStackSize));
5367
}
5468
}
@@ -88,7 +102,7 @@ public int getBiggestCraftableStack(
88102

89103
@Override
90104
public void clear() {
91-
this.uniqueStackMap.clear();
105+
this.stackInterner.clear();
92106
this.stackedContents.clear();
93107
}
94108

0 commit comments

Comments
 (0)