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/util/GT_Utility.java | |
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/util/GT_Utility.java')
-rw-r--r-- | src/main/java/gregtech/api/util/GT_Utility.java | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java index d41e2a3b04..7959789532 100644 --- a/src/main/java/gregtech/api/util/GT_Utility.java +++ b/src/main/java/gregtech/api/util/GT_Utility.java @@ -3137,6 +3137,13 @@ public class GT_Utility { return aList[aIndex]; } + public static boolean isStackInStackSet(ItemStack aStack, Set<ItemStack> aSet) { + if (aStack == null) return false; + if (aSet.contains(aStack)) return true; + + return aSet.contains(GT_ItemStack.internalCopyStack(aStack, true)); + } + public static boolean isStackInList(ItemStack aStack, Collection<GT_ItemStack> aList) { if (aStack == null) { return false; @@ -3165,7 +3172,25 @@ public class GT_Utility { * re-maps all Keys of a Map after the Keys were weakened. */ public static <X, Y> Map<X, Y> reMap(Map<X, Y> aMap) { - Map<X, Y> tMap = new HashMap<>(aMap); + Map<X, Y> tMap = null; + // We try to clone the Map first (most Maps are Cloneable) in order to retain as much state of the Map as + // possible when rehashing. For example, "Custom" HashMaps from fastutil may have a custom hash function which + // would not be used to rehash if we just create a new HashMap. + if (aMap instanceof Cloneable) { + try { + tMap = (Map<X, Y>) aMap.getClass() + .getMethod("clone") + .invoke(aMap); + } catch (Throwable e) { + GT_Log.err.println("Failed to clone Map of type " + aMap.getClass()); + e.printStackTrace(GT_Log.err); + } + } + + if (tMap == null) { + tMap = new HashMap<>(aMap); + } + aMap.clear(); aMap.putAll(tMap); return aMap; |