11package org .embeddedt .modernfix .common .mixin .perf .dynamic_resources ;
22
33import com .google .common .collect .ImmutableList ;
4+ import com .google .common .collect .Maps ;
5+ import com .llamalad7 .mixinextras .sugar .Local ;
46import net .minecraft .client .renderer .block .model .BlockModel ;
57import net .minecraft .client .resources .model .BakedModel ;
68import net .minecraft .client .resources .model .ModelBakery ;
79import net .minecraft .client .resources .model .ModelManager ;
810import net .minecraft .resources .ResourceLocation ;
11+ import net .minecraft .server .packs .resources .Resource ;
912import net .minecraft .server .packs .resources .ResourceManager ;
1013import net .minecraft .util .GsonHelper ;
1114import net .minecraft .world .level .block .Block ;
1922import org .spongepowered .asm .mixin .Shadow ;
2023import org .spongepowered .asm .mixin .injection .At ;
2124import org .spongepowered .asm .mixin .injection .Inject ;
25+ import org .spongepowered .asm .mixin .injection .ModifyArg ;
2226import org .spongepowered .asm .mixin .injection .Redirect ;
2327import org .spongepowered .asm .mixin .injection .callback .CallbackInfo ;
2428
2832import java .util .List ;
2933import java .util .Map ;
3034import java .util .Objects ;
35+ import java .util .Set ;
3136import java .util .concurrent .CompletableFuture ;
37+ import java .util .concurrent .CompletionStage ;
3238import java .util .concurrent .Executor ;
39+ import java .util .function .Function ;
3340import java .util .stream .Collectors ;
3441
3542@ Mixin (ModelManager .class )
@@ -44,10 +51,12 @@ private void injectDummyBakedRegistry(CallbackInfo ci) {
4451 }
4552 }
4653
47- @ 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;" ))
48- private CompletableFuture <Map <ResourceLocation , BlockModel >> deferBlockModelLoad (ResourceManager manager , Executor executor ) {
49- var cache = CacheUtil .<ResourceLocation , BlockModel >simpleCacheForLambda (location -> loadSingleBlockModel (manager , location ), 100L );
50- return CompletableFuture .completedFuture (new LambdaMap <>(location -> cache .getUnchecked (location )));
54+ @ ModifyArg (method = "loadBlockModels" , at = @ At (value = "INVOKE" , target = "Ljava/util/concurrent/CompletableFuture;thenCompose(Ljava/util/function/Function;)Ljava/util/concurrent/CompletableFuture;" , ordinal = 0 ), index = 0 )
55+ private static Function <Map <ResourceLocation , Resource >, ? extends CompletionStage <Map <ResourceLocation , BlockModel >>> deferBlockModelLoad (Function <Map <ResourceLocation , Resource >, ? extends CompletionStage <Map <ResourceLocation , BlockModel >>> fn , @ Local (ordinal = 0 , argsOnly = true ) ResourceManager manager ) {
56+ return resourceMap -> {
57+ var cache = CacheUtil .<ResourceLocation , BlockModel >simpleCacheForLambda (location -> loadSingleBlockModel (manager , location ), 100L );
58+ return CompletableFuture .completedFuture (Maps .asMap (Set .copyOf (resourceMap .keySet ()), location -> cache .getUnchecked (location )));
59+ };
5160 }
5261
5362 @ 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;" ))
@@ -61,7 +70,7 @@ private ImmutableList<BlockState> skipCollection(StateDefinition<Block, BlockSta
6170 return ImmutableList .of ();
6271 }
6372
64- private BlockModel loadSingleBlockModel (ResourceManager manager , ResourceLocation location ) {
73+ private static BlockModel loadSingleBlockModel (ResourceManager manager , ResourceLocation location ) {
6574 return manager .getResource (location ).map (resource -> {
6675 try (BufferedReader reader = resource .openAsReader ()) {
6776 return BlockModel .fromStream (reader );
0 commit comments