|
24 | 24 | */ |
25 | 25 | package org.spongepowered.common.item.recipe.book; |
26 | 26 |
|
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; |
29 | 30 | import net.minecraft.core.Holder; |
30 | 31 | import net.minecraft.world.entity.player.StackedContents; |
31 | 32 | import net.minecraft.world.entity.player.StackedItemContents; |
|
37 | 38 | import org.spongepowered.common.bridge.world.item.crafting.PlacementInfoBridge; |
38 | 39 |
|
39 | 40 | import java.util.List; |
| 41 | +import java.util.function.Function; |
40 | 42 |
|
41 | 43 | public class SpongeStackedItemContents extends StackedItemContents { |
42 | 44 |
|
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); |
44 | 58 | private final StackedContents<ItemStack> stackedContents = new StackedContents<>(); |
45 | 59 |
|
46 | 60 | @Override |
47 | 61 | public void accountStack(final ItemStack stack, final int maxStackSize) { |
48 | 62 | 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()); |
52 | 66 | this.stackedContents.account(stackToAccount, Math.min(stack.getCount(), maxStackSize)); |
53 | 67 | } |
54 | 68 | } |
@@ -88,7 +102,7 @@ public int getBiggestCraftableStack( |
88 | 102 |
|
89 | 103 | @Override |
90 | 104 | public void clear() { |
91 | | - this.uniqueStackMap.clear(); |
| 105 | + this.stackInterner.clear(); |
92 | 106 | this.stackedContents.clear(); |
93 | 107 | } |
94 | 108 |
|
|
0 commit comments