diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ModelBakeryMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ModelBakeryMixin.java index 68ab7f10..6f3ffe75 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ModelBakeryMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/dynamic_resources/ModelBakeryMixin.java @@ -62,7 +62,8 @@ import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; -@Mixin(ModelBakery.class) +/* high priority so that our injectors are added before other mods' */ +@Mixin(value = ModelBakery.class, priority = 600) public abstract class ModelBakeryMixin { private static final boolean debugDynamicModelLoading = Boolean.getBoolean("modernfix.debugDynamicModelLoading"); @@ -100,6 +101,8 @@ public abstract class ModelBakeryMixin { @Shadow @Final public static BlockModel BLOCK_ENTITY_MARKER; + @Shadow public abstract UnbakedModel getModel(ResourceLocation modelLocation); + private Cache, BakedModel> loadedBakedModels; private Cache loadedModels; @@ -162,8 +165,9 @@ public abstract class ModelBakeryMixin { * @reason don't actually load the model. instead, keep track of if we need to load a blockstate or a model, * and save the info into the two lists */ - @Overwrite - private void loadTopLevel(ModelResourceLocation location) { + @Inject(method = "loadTopLevel", at = @At("HEAD"), cancellable = true) + private void addTopLevelFile(ModelResourceLocation location, CallbackInfo ci) { + ci.cancel(); if(Objects.equals(location.getVariant(), "inventory")) { modelFiles.add(new ResourceLocation(location.getNamespace(), "item/" + location.getPath())); } else { @@ -338,13 +342,15 @@ public abstract class ModelBakeryMixin { * @author embeddedt * @reason synchronize */ - @Overwrite - public UnbakedModel getModel(ResourceLocation modelLocation) { - if(modelLocation.equals(MISSING_MODEL_LOCATION)) - return missingModel; + @Inject(method = "getModel", at = @At("HEAD"), cancellable = true) + public void getOrLoadModelDynamic(ResourceLocation modelLocation, CallbackInfoReturnable cir) { + if(modelLocation.equals(MISSING_MODEL_LOCATION)) { + cir.setReturnValue(missingModel); + return; + } UnbakedModel existing = this.unbakedCache.get(modelLocation); if (existing != null) { - return existing; + cir.setReturnValue(existing); } else { synchronized(this) { if (this.loadingStack.contains(modelLocation)) { @@ -381,7 +387,7 @@ public abstract class ModelBakeryMixin { UnbakedModel result = smallLoadingCache.getOrDefault(modelLocation, iunbakedmodel); // We are done with loading, so clear this cache to allow GC of any unneeded models smallLoadingCache.clear(); - return result; + cir.setReturnValue(result); } } } @@ -430,12 +436,12 @@ public abstract class ModelBakeryMixin { return ImmutableList.copyOf(finalList); } - @Overwrite(remap = false) - public BakedModel bake(ResourceLocation arg, ModelState arg2, Function textureGetter) { + @Inject(method = "bake(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/client/resources/model/ModelState;Ljava/util/function/Function;)Lnet/minecraft/client/resources/model/BakedModel;", at = @At("HEAD"), cancellable = true, remap = false) + public void getOrLoadBakedModelDynamic(ResourceLocation arg, ModelState arg2, Function textureGetter, CallbackInfoReturnable cir) { Triple triple = Triple.of(arg, arg2.getRotation(), arg2.isUvLocked()); BakedModel existing = this.bakedCache.get(triple); if (existing != null) { - return existing; + cir.setReturnValue(existing); } else if (this.atlasSet == null) { throw new IllegalStateException("bake called too early"); } else { @@ -457,7 +463,7 @@ public abstract class ModelBakeryMixin { DynamicModelBakeEvent event = new DynamicModelBakeEvent(arg, iunbakedmodel, ibakedmodel, (ForgeModelBakery)(Object)this); MinecraftForge.EVENT_BUS.post(event); this.bakedCache.put(triple, event.getModel()); - return event.getModel(); + cir.setReturnValue(event.getModel()); } } }