diff options
author | tth05 <36999320+tth05@users.noreply.github.com> | 2024-01-20 23:00:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-20 23:00:50 +0100 |
commit | 2a3abcc6b8859b11e13c9d95d7426590edf9fefd (patch) | |
tree | 96b9ba00d09a0390e537abfa47969a1824f7ac9b /src/main/java/gregtech/api/objects | |
parent | 8aedb43274634bb4df44b67da7a7fe98a33ecf55 (diff) | |
download | GT5-Unofficial-2a3abcc6b8859b11e13c9d95d7426590edf9fefd.tar.gz GT5-Unofficial-2a3abcc6b8859b11e13c9d95d7426590edf9fefd.tar.bz2 GT5-Unofficial-2a3abcc6b8859b11e13c9d95d7426590edf9fefd.zip |
Remove all usages of GT_ItemStack2 as Set/Map keys by using fastutil (#2465)
* Replace Maps in GT_OreDictUnificator with fastutil implementations with custom hasher
Removes the need to allocate a GT_ItemStack2 every time these Maps are accessed
* Replace local HashMaps with Reference2LongArrayMaps in GT_Recipe and fix if condition
* Remove GT_ItemStack2 usage from OrePrefixes
* Update GTNHLib requirement in @Mod annotation
* Don't modify stack argument when re-trying `setItemStack2DataMap` access with wildcard value
Diffstat (limited to 'src/main/java/gregtech/api/objects')
-rw-r--r-- | src/main/java/gregtech/api/objects/GT_ItemStack.java | 38 | ||||
-rw-r--r-- | src/main/java/gregtech/api/objects/GT_ItemStack2.java | 3 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/objects/GT_ItemStack.java b/src/main/java/gregtech/api/objects/GT_ItemStack.java index 210f807c5d..492655740d 100644 --- a/src/main/java/gregtech/api/objects/GT_ItemStack.java +++ b/src/main/java/gregtech/api/objects/GT_ItemStack.java @@ -7,6 +7,7 @@ import net.minecraft.item.ItemStack; import gregtech.api.enums.GT_Values; import gregtech.api.util.GT_Utility; import gregtech.api.util.item.ItemHolder; +import it.unimi.dsi.fastutil.Hash; /** * An optimization of {@link ItemStack} to have a better {@code hashcode} and {@code equals} in order to improve @@ -14,6 +15,25 @@ import gregtech.api.util.item.ItemHolder; */ public class GT_ItemStack extends ItemHolder { + /** + * A better {@link Hash.Strategy} for {@link ItemStack}. Implementation originally from {@code GT_ItemStack2}. + */ + public static final Hash.Strategy<ItemStack> ITEMSTACK_HASH_STRATEGY2 = new Hash.Strategy<>() { + + @Override + public int hashCode(ItemStack o) { + return o.getItem() + .hashCode() * 38197 + Items.feather.getDamage(o); + } + + @Override + public boolean equals(ItemStack a, ItemStack b) { + if (a == b) return true; + if (a == null || b == null) return false; + return a.getItem() == b.getItem() && Items.feather.getDamage(a) == Items.feather.getDamage(b); + } + }; + public final Item mItem; public final byte mStackSize; public final short mMetaData; @@ -66,4 +86,22 @@ public class GT_ItemStack extends ItemHolder { public int hashCode() { return GT_Utility.stackToInt(toStack()); } + + /** + * @see #internalCopyStack(ItemStack, boolean) + */ + public static ItemStack internalCopyStack(ItemStack aStack) { + return internalCopyStack(aStack, false); + } + + /** + * Replicates the copy behavior of {@link #toStack()} but for normal {@link ItemStack}s. + * + * @param aStack the stack to copy + * @param wildcard whether to use wildcard damage value + * @return a copy of the stack with stack size 1 and no NBT + */ + public static ItemStack internalCopyStack(ItemStack aStack, boolean wildcard) { + return new ItemStack(aStack.getItem(), 1, wildcard ? GT_Values.W : Items.feather.getDamage(aStack)); + } } diff --git a/src/main/java/gregtech/api/objects/GT_ItemStack2.java b/src/main/java/gregtech/api/objects/GT_ItemStack2.java index b137e78829..aa93876830 100644 --- a/src/main/java/gregtech/api/objects/GT_ItemStack2.java +++ b/src/main/java/gregtech/api/objects/GT_ItemStack2.java @@ -7,7 +7,10 @@ import net.minecraft.item.ItemStack; * GT_ItemStack, but with a better hashCode(). Due to this change, it should not be placed in the same hash based data * structure with GT_ItemStack. It also shouldn't be used to construct search query into a hash based data structure * that contains GT_ItemStack. + * + * @deprecated See {@link GT_ItemStack#ITEMSTACK_HASH_STRATEGY2} */ +@Deprecated public class GT_ItemStack2 extends GT_ItemStack { public GT_ItemStack2(Item aItem, long aStackSize, long aMetaData) { |