|
| 1 | +package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources; |
| 2 | + |
| 3 | +import net.minecraft.client.Minecraft; |
| 4 | +import net.minecraft.client.model.geom.EntityModelSet; |
| 5 | +import net.minecraft.client.renderer.block.model.BlockModel; |
| 6 | +import net.minecraft.client.renderer.item.ClientItem; |
| 7 | +import net.minecraft.client.renderer.item.ItemModel; |
| 8 | +import net.minecraft.client.resources.model.BakedModel; |
| 9 | +import net.minecraft.client.resources.model.BlockStateModelLoader; |
| 10 | +import net.minecraft.client.resources.model.ModelManager; |
| 11 | +import net.minecraft.client.resources.model.ModelResourceLocation; |
| 12 | +import net.minecraft.client.resources.model.UnbakedModel; |
| 13 | +import net.minecraft.resources.ResourceLocation; |
| 14 | +import net.minecraft.server.packs.resources.ResourceManager; |
| 15 | +import net.minecraft.util.profiling.ProfilerFiller; |
| 16 | +import net.minecraft.world.level.block.state.BlockState; |
| 17 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 18 | +import org.embeddedt.modernfix.dynamicresources.DynamicModelProvider; |
| 19 | +import org.spongepowered.asm.mixin.Mixin; |
| 20 | +import org.spongepowered.asm.mixin.Overwrite; |
| 21 | +import org.spongepowered.asm.mixin.Shadow; |
| 22 | +import org.spongepowered.asm.mixin.Unique; |
| 23 | +import org.spongepowered.asm.mixin.injection.At; |
| 24 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 25 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 26 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 27 | + |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.CompletableFuture; |
| 30 | +import java.util.concurrent.Executor; |
| 31 | + |
| 32 | +@Mixin(ModelManager.class) |
| 33 | +@ClientOnlyMixin |
| 34 | +public class ModelManagerMixin { |
| 35 | + @Shadow private BakedModel missingModel; |
| 36 | + @Shadow private ItemModel missingItemModel; |
| 37 | + @Shadow private EntityModelSet entityModelSet; |
| 38 | + @Unique |
| 39 | + private DynamicModelProvider mfix$modelProvider; |
| 40 | + |
| 41 | + @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;")) |
| 42 | + private CompletableFuture<Map<ResourceLocation, BlockModel>> deferBlockModelLoad(ResourceManager manager, Executor executor) { |
| 43 | + return CompletableFuture.completedFuture(Map.of()); |
| 44 | + } |
| 45 | + |
| 46 | + @Redirect(method = "reload", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/model/BlockStateModelLoader;loadBlockStates(Lnet/minecraft/client/resources/model/UnbakedModel;Lnet/minecraft/server/packs/resources/ResourceManager;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;")) |
| 47 | + private CompletableFuture<BlockStateModelLoader.LoadedModels> deferBlockStateLoad(UnbakedModel unbakedModel, ResourceManager resourceManager, Executor executor) { |
| 48 | + return CompletableFuture.completedFuture(new BlockStateModelLoader.LoadedModels(Map.of())); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @author embeddedt |
| 53 | + * @reason disable map creation |
| 54 | + */ |
| 55 | + @Overwrite |
| 56 | + private static Map<BlockState, BakedModel> createBlockStateToModelDispatch(Map<ModelResourceLocation, BakedModel> map, BakedModel bakedModel) { |
| 57 | + return Map.of(); |
| 58 | + } |
| 59 | + |
| 60 | + @Inject(method = "apply", at = @At("RETURN")) |
| 61 | + private void createModelProvider(ModelManager.ReloadState reloadState, ProfilerFiller profiler, CallbackInfo ci) { |
| 62 | + this.mfix$modelProvider = new DynamicModelProvider( |
| 63 | + null, // TODO |
| 64 | + this.missingModel, |
| 65 | + this.missingItemModel, |
| 66 | + Minecraft.getInstance().getResourceManager(), |
| 67 | + this.entityModelSet, |
| 68 | + reloadState.atlasPreparations() |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @author embeddedt |
| 74 | + * @reason use dynamic model system |
| 75 | + */ |
| 76 | + @Overwrite |
| 77 | + public BakedModel getModel(ModelResourceLocation modelLocation) { |
| 78 | + if(this.mfix$modelProvider != null) { |
| 79 | + return this.mfix$modelProvider.getModel(modelLocation); |
| 80 | + } else { |
| 81 | + return this.missingModel; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @author embeddedt |
| 87 | + * @reason use dynamic model system |
| 88 | + */ |
| 89 | + @Overwrite |
| 90 | + public ItemModel getItemModel(ResourceLocation resourceLocation) { |
| 91 | + return this.mfix$modelProvider.getItemModel(resourceLocation); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @author embeddedt |
| 96 | + * @reason use dynamic model system |
| 97 | + */ |
| 98 | + @Overwrite |
| 99 | + public ClientItem.Properties getItemProperties(ResourceLocation resourceLocation) { |
| 100 | + return this.mfix$modelProvider.getClientItemProperties(resourceLocation); |
| 101 | + } |
| 102 | +} |
0 commit comments