Skip to content

Commit fd46baa

Browse files
committed
Backport model compat fix from 1.21
ff6b687 Related: #606
1 parent 273bab7 commit fd46baa

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources;
22

33
import com.google.common.collect.ImmutableList;
4+
import com.google.common.collect.Maps;
5+
import com.llamalad7.mixinextras.sugar.Local;
46
import net.minecraft.client.renderer.block.model.BlockModel;
57
import net.minecraft.client.resources.model.BakedModel;
68
import net.minecraft.client.resources.model.ModelBakery;
79
import net.minecraft.client.resources.model.ModelManager;
810
import net.minecraft.resources.ResourceLocation;
11+
import net.minecraft.server.packs.resources.Resource;
912
import net.minecraft.server.packs.resources.ResourceManager;
1013
import net.minecraft.util.GsonHelper;
1114
import net.minecraft.world.level.block.Block;
@@ -19,6 +22,7 @@
1922
import org.spongepowered.asm.mixin.Shadow;
2023
import org.spongepowered.asm.mixin.injection.At;
2124
import org.spongepowered.asm.mixin.injection.Inject;
25+
import org.spongepowered.asm.mixin.injection.ModifyArg;
2226
import org.spongepowered.asm.mixin.injection.Redirect;
2327
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2428

@@ -28,8 +32,11 @@
2832
import java.util.List;
2933
import java.util.Map;
3034
import java.util.Objects;
35+
import java.util.Set;
3136
import java.util.concurrent.CompletableFuture;
37+
import java.util.concurrent.CompletionStage;
3238
import java.util.concurrent.Executor;
39+
import java.util.function.Function;
3340
import 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

Comments
 (0)