From 1d3044093b804c0c94b7e32a440e7ebf995fd0b3 Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Tue, 7 May 2024 11:30:46 +1000 Subject: [PATCH] Fix Ore Dict Ingredients When Changing OreDicts with CT & GS (#2456) (cherry picked from commit e0a246503cd14f012f0f19c4899e3cc4a256de4d) --- .../recipes/ingredients/GTRecipeOreInput.java | 24 ++++++++++++++++--- .../java/gregtech/common/CommonProxy.java | 10 ++++++++ .../groovy/GroovyScriptModule.java | 10 ++++++++ .../jei/JustEnoughItemsModule.java | 4 ++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/java/gregtech/api/recipes/ingredients/GTRecipeOreInput.java b/src/main/java/gregtech/api/recipes/ingredients/GTRecipeOreInput.java index e36d9e39370..a742a23c819 100644 --- a/src/main/java/gregtech/api/recipes/ingredients/GTRecipeOreInput.java +++ b/src/main/java/gregtech/api/recipes/ingredients/GTRecipeOreInput.java @@ -7,12 +7,19 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.oredict.OreDictionary; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.Objects; public class GTRecipeOreInput extends GTRecipeInput { + // Standard forces a refresh of the input stack cache + // Used in GroovyScript Reload, and to avoid race conditions with CraftTweaker. + // Short.MAX_VALUE should be enough for the amount of times this is loaded + // (1 without GroovyScript Reload, Amount of GroovyScript Reloads otherwise) + private static short STANDARD = 0; + private short currentStandard; private final int ore; private ItemStack[] inputStacks; @@ -88,12 +95,15 @@ public GTRecipeInput copyWithAmount(int amount) { return copy; } - // The items returned here are not updated after its first call, so they are not suitable for use while recipes are - // being processed and + // The items returned here are not updated after its first call, unless standard is changed, + // so they are not suitable for use while recipes are being processed and // the OreDicts being modified. @Override public ItemStack[] getInputStacks() { - if (this.inputStacks == null) { + // Standard forces a refresh of the input stack cache. + // Used in GroovyScript Reload, and upon Load Complete to fix unreliable behaviour with CT and GS scripts. + if (inputStacks == null || currentStandard != STANDARD) { + currentStandard = STANDARD; inputStacks = (OreDictionary.getOres(OreDictionary.getOreName(ore)).stream().map(is -> { is = is.copy(); is.setCount(this.amount); @@ -163,4 +173,12 @@ public String toString() { // noinspection StringConcatenationMissingWhitespace return amount + "x" + OreDictionary.getOreName(ore); } + + /** + * Forces a Refresh of every GTRecipeOreInput's Stack Cache. + */ + @ApiStatus.Internal + public static void refreshStackCache() { + STANDARD++; + } } diff --git a/src/main/java/gregtech/common/CommonProxy.java b/src/main/java/gregtech/common/CommonProxy.java index b9927847cd4..3c474d5edc9 100644 --- a/src/main/java/gregtech/common/CommonProxy.java +++ b/src/main/java/gregtech/common/CommonProxy.java @@ -8,6 +8,7 @@ import gregtech.api.items.toolitem.IGTTool; import gregtech.api.recipes.GTRecipeInputCache; import gregtech.api.recipes.ModHandler; +import gregtech.api.recipes.ingredients.GTRecipeOreInput; import gregtech.api.recipes.recipeproperties.FusionEUToStartProperty; import gregtech.api.terminal.TerminalRegistry; import gregtech.api.unification.material.Material; @@ -33,10 +34,12 @@ import gregtech.common.pipelike.laser.ItemBlockLaserPipe; import gregtech.common.pipelike.optical.BlockOpticalPipe; import gregtech.common.pipelike.optical.ItemBlockOpticalPipe; +import gregtech.integration.groovy.GroovyScriptModule; import gregtech.loaders.MaterialInfoLoader; import gregtech.loaders.OreDictionaryLoader; import gregtech.loaders.recipe.CraftingComponent; import gregtech.loaders.recipe.GTRecipeManager; +import gregtech.modules.GregTechModules; import net.minecraft.block.Block; import net.minecraft.item.Item; @@ -399,6 +402,13 @@ public void onPostLoad() { public void onLoadComplete() { GTRecipeInputCache.disableCache(); + + // If JEI and GS is not loaded, refresh ore dict ingredients + // Not needed if JEI is loaded, as done in the JEI plugin (and this runs after that) + // Not needed if GS is loaded, as done after script loads (and this runs after that) + if (!GregTechAPI.moduleManager.isModuleEnabled(GregTechModules.MODULE_JEI) && + !GroovyScriptModule.isCurrentlyRunning()) + GTRecipeOreInput.refreshStackCache(); } public boolean isFancyGraphics() { diff --git a/src/main/java/gregtech/integration/groovy/GroovyScriptModule.java b/src/main/java/gregtech/integration/groovy/GroovyScriptModule.java index 62891a1f132..bbeab52bf0c 100644 --- a/src/main/java/gregtech/integration/groovy/GroovyScriptModule.java +++ b/src/main/java/gregtech/integration/groovy/GroovyScriptModule.java @@ -8,6 +8,7 @@ import gregtech.api.modules.GregTechModule; import gregtech.api.recipes.RecipeBuilder; import gregtech.api.recipes.RecipeMap; +import gregtech.api.recipes.ingredients.GTRecipeOreInput; import gregtech.api.unification.Element; import gregtech.api.unification.Elements; import gregtech.api.unification.material.Material; @@ -40,6 +41,7 @@ import com.cleanroommc.groovyscript.api.Result; import com.cleanroommc.groovyscript.compat.mods.GroovyContainer; import com.cleanroommc.groovyscript.compat.mods.ModPropertyContainer; +import com.cleanroommc.groovyscript.event.ScriptRunEvent; import com.cleanroommc.groovyscript.gameobjects.GameObjectHandler; import com.cleanroommc.groovyscript.helper.EnumHelper; import com.cleanroommc.groovyscript.sandbox.expand.ExpansionHelper; @@ -83,6 +85,14 @@ public static void onRecipeEvent(RegistryEvent.Register event) { GroovyScriptModule.loadMetaItemBracketHandler(); } + @SubscribeEvent + @Optional.Method(modid = Mods.Names.GROOVY_SCRIPT) + public static void afterScriptLoad(ScriptRunEvent.Post event) { + // Not Needed if JEI Module is enabled + if (!GregTechAPI.moduleManager.isModuleEnabled(GregTechModules.MODULE_JEI)) + GTRecipeOreInput.refreshStackCache(); + } + public static boolean isCurrentlyRunning() { return GregTechAPI.moduleManager.isModuleEnabled(GregTechModules.MODULE_GRS) && GroovyScript.getSandbox().isRunning(); diff --git a/src/main/java/gregtech/integration/jei/JustEnoughItemsModule.java b/src/main/java/gregtech/integration/jei/JustEnoughItemsModule.java index 35e3f525ec3..04a4adbe512 100644 --- a/src/main/java/gregtech/integration/jei/JustEnoughItemsModule.java +++ b/src/main/java/gregtech/integration/jei/JustEnoughItemsModule.java @@ -15,6 +15,7 @@ import gregtech.api.recipes.RecipeMap; import gregtech.api.recipes.RecipeMaps; import gregtech.api.recipes.category.GTRecipeCategory; +import gregtech.api.recipes.ingredients.GTRecipeOreInput; import gregtech.api.recipes.ingredients.IntCircuitIngredient; import gregtech.api.recipes.machines.IScannerRecipeMap; import gregtech.api.recipes.machines.RecipeMapFurnace; @@ -279,6 +280,9 @@ public void register(IModRegistry registry) { }); registry.addIngredientInfo(new ItemStack(MetaBlocks.BRITTLE_CHARCOAL), VanillaTypes.ITEM, I18n.format("tile.brittle_charcoal.tooltip.1", I18n.format("tile.brittle_charcoal.tooltip.2"))); + + // Refresh Ore Ingredients Cache + GTRecipeOreInput.refreshStackCache(); } private void setupInputHandler() {