From bc2e84bacc4cd620af9779b214eca62ad79350c4 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Thu, 21 Dec 2023 16:50:08 -0500 Subject: [PATCH] Cache item override model instance --- .../dynamicresources/ItemOverrideBakedModel.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/dynamicresources/ItemOverrideBakedModel.java b/common/src/main/java/org/embeddedt/modernfix/dynamicresources/ItemOverrideBakedModel.java index 79b05c961..0c8aae1e5 100644 --- a/common/src/main/java/org/embeddedt/modernfix/dynamicresources/ItemOverrideBakedModel.java +++ b/common/src/main/java/org/embeddedt/modernfix/dynamicresources/ItemOverrideBakedModel.java @@ -10,28 +10,32 @@ import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.Nullable; +import java.lang.ref.WeakReference; import java.util.List; -import java.util.Map; import java.util.Random; -import java.util.concurrent.ConcurrentHashMap; /** * Delegate model that stores the location of an actual baked model, for use in ItemOverrides. */ public class ItemOverrideBakedModel implements BakedModel { - private static final Map OVERRIDE_MODELS = new ConcurrentHashMap<>(); public final ResourceLocation realLocation; + private WeakReference realModel = new WeakReference<>(null); private ItemOverrideBakedModel(ResourceLocation realLocation) { this.realLocation = realLocation; } public static ItemOverrideBakedModel of(ResourceLocation realLocation) { - return OVERRIDE_MODELS.computeIfAbsent(realLocation, ItemOverrideBakedModel::new); + return new ItemOverrideBakedModel(realLocation); } public BakedModel getRealModel() { - return DynamicBakedModelProvider.currentInstance.get(realLocation); + BakedModel m = realModel.get(); + if(m == null) { + m = DynamicBakedModelProvider.currentInstance.get(realLocation); + realModel = new WeakReference<>(m); + } + return m; } @Override