Skip to content

Commit

Permalink
Fix Ore Dict Ingredients When Changing OreDicts with CT & GS (GregTec…
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit authored May 7, 2024
1 parent 8c2f0c6 commit e0a2465
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++;
}
}
10 changes: 10 additions & 0 deletions src/main/java/gregtech/common/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gregtech/integration/groovy/GroovyScriptModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -83,6 +85,14 @@ public static void onRecipeEvent(RegistryEvent.Register<IRecipe> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -296,6 +297,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() {
Expand Down

0 comments on commit e0a2465

Please sign in to comment.