diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java index aa773429..673df9dc 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java @@ -3,6 +3,7 @@ package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources; import com.google.common.collect.ImmutableList; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntMaps; +import net.minecraft.client.Minecraft; import net.minecraft.client.color.block.BlockColors; import net.minecraft.client.resources.model.BlockStateModelLoader; import net.minecraft.client.resources.model.ModelResourceLocation; @@ -51,7 +52,8 @@ public abstract class BlockStateModelLoaderMixin implements IBlockStateModelLoad var optionalBlock = BuiltInRegistries.BLOCK.getOptional(location.id()); if(optionalBlock.isPresent()) { try { - filteredStates = ModelBakeryHelpers.getBlockStatesForMRL(optionalBlock.get().getStateDefinition(), location); + // Only filter states if we are not in the loading overlay + filteredStates = Minecraft.getInstance().getOverlay() == null ? ModelBakeryHelpers.getBlockStatesForMRL(optionalBlock.get().getStateDefinition(), location) : null; } catch(RuntimeException e) { ModernFix.LOGGER.error("Exception filtering states on {}", location, e); filteredStates = null; diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelManagerMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelManagerMixin.java index 984ec5fc..b0e6379e 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelManagerMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelManagerMixin.java @@ -19,6 +19,7 @@ import org.embeddedt.modernfix.ModernFix; import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.duck.IExtendedModelBakery; import org.embeddedt.modernfix.duck.IExtendedModelManager; +import org.embeddedt.modernfix.util.CacheUtil; import org.embeddedt.modernfix.util.LambdaMap; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -57,12 +58,14 @@ public class ModelManagerMixin implements IExtendedModelManager { @Redirect(method = "reload", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/model/ModelManager;loadBlockModels(Lnet/minecraft/server/packs/resources/ResourceManager;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;")) private CompletableFuture> deferBlockModelLoad(ResourceManager manager, Executor executor) { - return CompletableFuture.completedFuture(new LambdaMap<>(location -> loadSingleBlockModel(manager, location))); + var cache = CacheUtil.simpleCacheForLambda(location -> loadSingleBlockModel(manager, location), 100L); + return CompletableFuture.completedFuture(new LambdaMap<>(location -> cache.getUnchecked(location))); } @Redirect(method = "reload", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/model/ModelManager;loadBlockStates(Lnet/minecraft/server/packs/resources/ResourceManager;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;")) private CompletableFuture>> deferBlockStateLoad(ResourceManager manager, Executor executor) { - return CompletableFuture.completedFuture(new LambdaMap<>(location -> loadSingleBlockState(manager, location))); + var cache = CacheUtil.>simpleCacheForLambda(location -> loadSingleBlockState(manager, location), 100L); + return CompletableFuture.completedFuture(new LambdaMap<>(location -> cache.getUnchecked(location))); } @Redirect(method = "loadModels", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/StateDefinition;getPossibleStates()Lcom/google/common/collect/ImmutableList;")) diff --git a/common/src/main/java/org/embeddedt/modernfix/util/CacheUtil.java b/common/src/main/java/org/embeddedt/modernfix/util/CacheUtil.java new file mode 100644 index 00000000..fd085826 --- /dev/null +++ b/common/src/main/java/org/embeddedt/modernfix/util/CacheUtil.java @@ -0,0 +1,18 @@ +package org.embeddedt.modernfix.util; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + +import java.util.function.Function; + +public class CacheUtil { + public static LoadingCache simpleCacheForLambda(Function function, long maxSize) { + return CacheBuilder.newBuilder().maximumSize(maxSize).build(new CacheLoader() { + @Override + public V load(K key) throws Exception { + return function.apply(key); + } + }); + } +}