diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/block/DynamicModelCache.java b/src/main/java/com/gtnewhorizon/gtnhlib/block/ThreadsafeCache.java similarity index 71% rename from src/main/java/com/gtnewhorizon/gtnhlib/block/DynamicModelCache.java rename to src/main/java/com/gtnewhorizon/gtnhlib/block/ThreadsafeCache.java index 8a0dca48..03e975d8 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/block/DynamicModelCache.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/block/ThreadsafeCache.java @@ -1,23 +1,20 @@ package com.gtnewhorizon.gtnhlib.block; -import java.util.concurrent.locks.StampedLock; - -import com.gtnewhorizon.gtnhlib.client.model.BakedModel; - import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.objects.Reference2ReferenceLinkedOpenHashMap; +import java.util.concurrent.locks.StampedLock; /** * Borrowed from ModernFix, because 7.10 can't embed models in blocks easily */ -public class DynamicModelCache { +public class ThreadsafeCache { - private final Reference2ReferenceLinkedOpenHashMap cache = new Reference2ReferenceLinkedOpenHashMap<>(); + private final Reference2ReferenceLinkedOpenHashMap cache = new Reference2ReferenceLinkedOpenHashMap<>(); private final StampedLock lock = new StampedLock(); - private final Function modelRetriever; + private final Function modelRetriever; private final boolean allowNulls; - public DynamicModelCache(Function modelRetriever, boolean allowNulls) { + public ThreadsafeCache(Function modelRetriever, boolean allowNulls) { this.modelRetriever = modelRetriever; this.allowNulls = allowNulls; } @@ -40,7 +37,7 @@ public class DynamicModelCache { } } - private BakedModel getModelFromCache(K state) { + private V getModelFromCache(K state) { long stamp = lock.readLock(); try { return cache.get(state); @@ -49,8 +46,8 @@ public class DynamicModelCache { } } - private BakedModel cacheModel(K state) { - BakedModel model = modelRetriever.apply(state); + private V cacheModel(K state) { + V model = modelRetriever.apply(state); // Lock and modify our local, faster cache long stamp = lock.writeLock(); @@ -68,8 +65,8 @@ public class DynamicModelCache { return model; } - public BakedModel get(K key) { - BakedModel model = getModelFromCache(key); + public V get(K key) { + V model = getModelFromCache(key); if (model == null && (!allowNulls || needToPopulate(key))) { model = cacheModel(key); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/impl/ModelRegistry.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/impl/ModelRegistry.java index 48414f16..ba93407b 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/impl/ModelRegistry.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/impl/ModelRegistry.java @@ -10,7 +10,7 @@ import net.minecraft.util.ResourceLocation; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.gtnewhorizon.gtnhlib.block.BlockState; -import com.gtnewhorizon.gtnhlib.block.DynamicModelCache; +import com.gtnewhorizon.gtnhlib.block.ThreadsafeCache; import com.gtnewhorizon.gtnhlib.client.model.BakedModel; import com.gtnewhorizon.gtnhlib.client.model.state.StateDeserializer; import com.gtnewhorizon.gtnhlib.client.model.state.StateModelMap; @@ -19,7 +19,7 @@ public class ModelRegistry { private static final Gson GSON = new GsonBuilder().registerTypeAdapter(StateModelMap.class, new StateDeserializer()) .create(); - private static final DynamicModelCache CACHE = new DynamicModelCache<>( + private static final ThreadsafeCache CACHE = new ThreadsafeCache<>( s -> bakeModel((BlockState) s), false);