Skip to content

Commit 70fba2e

Browse files
committed
Improve dynamic model loading efficiency during model bake event
1 parent 30e91f2 commit 70fba2e

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockStateModelLoaderMixin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.ImmutableList;
44
import it.unimi.dsi.fastutil.objects.Object2IntMap;
55
import it.unimi.dsi.fastutil.objects.Object2IntMaps;
6+
import net.minecraft.client.Minecraft;
67
import net.minecraft.client.color.block.BlockColors;
78
import net.minecraft.client.resources.model.BlockStateModelLoader;
89
import net.minecraft.client.resources.model.ModelResourceLocation;
@@ -51,7 +52,8 @@ public void loadSpecificBlock(ModelResourceLocation location) {
5152
var optionalBlock = BuiltInRegistries.BLOCK.getOptional(location.id());
5253
if(optionalBlock.isPresent()) {
5354
try {
54-
filteredStates = ModelBakeryHelpers.getBlockStatesForMRL(optionalBlock.get().getStateDefinition(), location);
55+
// Only filter states if we are not in the loading overlay
56+
filteredStates = Minecraft.getInstance().getOverlay() == null ? ModelBakeryHelpers.getBlockStatesForMRL(optionalBlock.get().getStateDefinition(), location) : null;
5557
} catch(RuntimeException e) {
5658
ModernFix.LOGGER.error("Exception filtering states on {}", location, e);
5759
filteredStates = null;

common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/ModelManagerMixin.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
2020
import org.embeddedt.modernfix.duck.IExtendedModelBakery;
2121
import org.embeddedt.modernfix.duck.IExtendedModelManager;
22+
import org.embeddedt.modernfix.util.CacheUtil;
2223
import org.embeddedt.modernfix.util.LambdaMap;
2324
import org.spongepowered.asm.mixin.Mixin;
2425
import org.spongepowered.asm.mixin.Shadow;
@@ -57,12 +58,14 @@ private void injectDummyBakedRegistry(CallbackInfo ci) {
5758

5859
@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;"))
5960
private CompletableFuture<Map<ResourceLocation, BlockModel>> deferBlockModelLoad(ResourceManager manager, Executor executor) {
60-
return CompletableFuture.completedFuture(new LambdaMap<>(location -> loadSingleBlockModel(manager, location)));
61+
var cache = CacheUtil.<ResourceLocation, BlockModel>simpleCacheForLambda(location -> loadSingleBlockModel(manager, location), 100L);
62+
return CompletableFuture.completedFuture(new LambdaMap<>(location -> cache.getUnchecked(location)));
6163
}
6264

6365
@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;"))
6466
private CompletableFuture<Map<ResourceLocation, List<BlockStateModelLoader.LoadedJson>>> deferBlockStateLoad(ResourceManager manager, Executor executor) {
65-
return CompletableFuture.completedFuture(new LambdaMap<>(location -> loadSingleBlockState(manager, location)));
67+
var cache = CacheUtil.<ResourceLocation, List<BlockStateModelLoader.LoadedJson>>simpleCacheForLambda(location -> loadSingleBlockState(manager, location), 100L);
68+
return CompletableFuture.completedFuture(new LambdaMap<>(location -> cache.getUnchecked(location)));
6669
}
6770

6871
@Redirect(method = "loadModels", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/StateDefinition;getPossibleStates()Lcom/google/common/collect/ImmutableList;"))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.embeddedt.modernfix.util;
2+
3+
import com.google.common.cache.CacheBuilder;
4+
import com.google.common.cache.CacheLoader;
5+
import com.google.common.cache.LoadingCache;
6+
7+
import java.util.function.Function;
8+
9+
public class CacheUtil {
10+
public static <K, V> LoadingCache<K, V> simpleCacheForLambda(Function<K, V> function, long maxSize) {
11+
return CacheBuilder.newBuilder().maximumSize(maxSize).build(new CacheLoader<K, V>() {
12+
@Override
13+
public V load(K key) throws Exception {
14+
return function.apply(key);
15+
}
16+
});
17+
}
18+
}

0 commit comments

Comments
 (0)