diff --git a/src/main/java/org/embeddedt/modernfix/common/mixin/perf/kubejs/RecipeEventJSMixin.java b/src/main/java/org/embeddedt/modernfix/common/mixin/perf/kubejs/RecipeEventJSMixin.java index c6d39132..1125caa9 100644 --- a/src/main/java/org/embeddedt/modernfix/common/mixin/perf/kubejs/RecipeEventJSMixin.java +++ b/src/main/java/org/embeddedt/modernfix/common/mixin/perf/kubejs/RecipeEventJSMixin.java @@ -1,12 +1,20 @@ package org.embeddedt.modernfix.common.mixin.perf.kubejs; +import com.google.gson.JsonElement; +import dev.latvian.mods.kubejs.recipe.RecipeJS; import dev.latvian.mods.kubejs.recipe.RecipesEventJS; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.crafting.Recipe; +import net.minecraft.world.item.crafting.RecipeManager; import org.embeddedt.modernfix.ModernFix; +import org.embeddedt.modernfix.annotation.FeatureLevel; import org.embeddedt.modernfix.annotation.RequiresMod; +import org.embeddedt.modernfix.core.config.ModernFixEarlyConfig; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -49,4 +57,30 @@ public class RecipeEventJSMixin { } } } + + /** + * @author embeddedt + * @reason once datapackRecipeMap is iterated, it is never referenced again, so clear it to avoid retaining + * references to the JSON objects + */ + @Inject(method = "post", at = @At(value = "NEW", target = "()Ljava/util/concurrent/ConcurrentLinkedQueue;", ordinal = 0), remap = false) + private void modernfix$clearDatapackRecipeMap(RecipeManager recipeManager, Map datapackRecipeMap, CallbackInfo ci) { + if (ModernFixEarlyConfig.ACTIVE_FEATURE_LEVEL.isAtLeast(FeatureLevel.BETA)) { + datapackRecipeMap.clear(); + } + } + + /** + * @author embeddedt + * @reason As we start materializing the final recipe objects, null out the JSON references so we avoid having + * to keep both in memory at the same time + */ + @Inject(method = "createRecipe", at = @At("RETURN"), remap = false) + private void modernfix$clearJson(RecipeJS r, CallbackInfoReturnable> cir) { + if (!ModernFixEarlyConfig.ACTIVE_FEATURE_LEVEL.isAtLeast(FeatureLevel.BETA)) { + return; + } + r.json = null; + r.originalJson = null; + } }