@@ -105,6 +105,7 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
105105 private Cache <ResourceLocation , UnbakedModel > loadedModels ;
106106
107107 private HashMap <ResourceLocation , UnbakedModel > smallLoadingCache = new HashMap <>();
108+ private Map <ResourceLocation , UnbakedModel > vanillaUnbakedStorage ;
108109
109110 private boolean inTextureGatheringPass ;
110111
@@ -128,11 +129,11 @@ private void replaceTopLevelBakedModels(ProfilerFiller filler, String s) {
128129 .softValues ()
129130 .build ();
130131 // temporarily replace this map to capture models into the small loading cache
131- Map < ResourceLocation , UnbakedModel > oldMap = this .unbakedCache ;
132+ vanillaUnbakedStorage = this .unbakedCache ;
132133 this .unbakedCache = new ForwardingMap <ResourceLocation , UnbakedModel >() {
133134 @ Override
134135 protected Map <ResourceLocation , UnbakedModel > delegate () {
135- return oldMap ;
136+ return vanillaUnbakedStorage ;
136137 }
137138
138139 @ Override
@@ -320,6 +321,31 @@ public UnbakedModel put(ResourceLocation key, UnbakedModel value) {
320321 ModernFix .LOGGER .info ("Early model bake took {}" , watch );
321322 ModernFix .LOGGER .info ("{} unbaked models, {} baked models loaded permanently" , this .unbakedCache .size (), this .bakedCache .size ());
322323 this .unbakedCache = new LayeredForwardingMap <>(new Map [] { this .unbakedCache , mutableBackingMap });
324+ // prevent writes from hitting the vanilla map, which prevents GC of models
325+ Map <ResourceLocation , UnbakedModel > oldVanillaStorage = vanillaUnbakedStorage ;
326+ vanillaUnbakedStorage = new ForwardingMap <ResourceLocation , UnbakedModel >() {
327+ @ Override
328+ protected Map <ResourceLocation , UnbakedModel > delegate () {
329+ return oldVanillaStorage ;
330+ }
331+
332+ @ Override
333+ public UnbakedModel put (ResourceLocation key , UnbakedModel value ) {
334+ UnbakedModel old = get (key );
335+ if (old != null )
336+ remove (key );
337+ return old ;
338+ }
339+
340+ @ Override
341+ public void putAll (Map <? extends ResourceLocation , ? extends UnbakedModel > map ) {
342+
343+ }
344+
345+ @ Override
346+ public void clear () {
347+ }
348+ };
323349 this .bakedTopLevelModels = new DynamicBakedModelProvider ((ModelBakery )(Object )this , bakedCache );
324350 if (this .bakedMissingModel != null )
325351 ((DynamicBakedModelProvider )this .bakedTopLevelModels ).setMissingModel (this .bakedMissingModel );
0 commit comments