From eb0e4289b35c25de39906bcab5f669912e13e900 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Sun, 5 Jul 2020 12:23:16 +0200 Subject: [PATCH] Unify the client/server-side shared itemstack registry to avoid over-dependence on the current thread when this class is used. Otherwise if a thread is not correctly detected to be part of the server or client thread groups, a shared item stack from the wrong domain is returned. --- .../appeng/util/item/AEItemStackRegistry.java | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/main/java/appeng/util/item/AEItemStackRegistry.java b/src/main/java/appeng/util/item/AEItemStackRegistry.java index 011aaa23e..929962da1 100644 --- a/src/main/java/appeng/util/item/AEItemStackRegistry.java +++ b/src/main/java/appeng/util/item/AEItemStackRegistry.java @@ -33,20 +33,11 @@ import net.minecraft.item.ItemStack; import appeng.util.Platform; public final class AEItemStackRegistry { - private static final WeakHashMap> SERVER_REGISTRY = new WeakHashMap<>(); - private static final WeakHashMap> CLIENT_REGISTRY = new WeakHashMap<>(); + private static final WeakHashMap> REGISTRY = new WeakHashMap<>(); private AEItemStackRegistry() { } - private static WeakHashMap> registry() { - if (Platform.isClient()) { - return CLIENT_REGISTRY; - } else { - return SERVER_REGISTRY; - } - } - static synchronized AESharedItemStack getRegisteredStack(final @Nonnull ItemStack itemStack) { if (itemStack.isEmpty()) { throw new IllegalArgumentException("stack cannot be empty"); @@ -56,7 +47,7 @@ public final class AEItemStackRegistry { itemStack.setCount(1); AESharedItemStack search = new AESharedItemStack(itemStack); - WeakReference weak = registry().get(search); + WeakReference weak = REGISTRY.get(search); AESharedItemStack ret = null; if (weak != null) { @@ -65,7 +56,7 @@ public final class AEItemStackRegistry { if (ret == null) { ret = new AESharedItemStack(itemStack.copy()); - registry().put(ret, new WeakReference<>(ret)); + REGISTRY.put(ret, new WeakReference<>(ret)); } itemStack.setCount(oldStackSize);