From e0abd1af11eeaebaa99666e747a9c1e2c7f891ad Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Thu, 25 Jan 2024 15:34:22 +0800 Subject: [PATCH] Add Slightly More Flexibility With Recycling Helpers --- ...sts.groovy => recipeRecyclingTests.groovy} | 17 ++++++++++++- .../nomilabs/groovy/GroovyHelpers.java | 6 +++++ .../nomilabs/groovy/ReplaceRecipe.java | 25 +++++++++++++++---- .../com/nomiceu/nomilabs/util/LabsNames.java | 5 ++++ 4 files changed, 47 insertions(+), 6 deletions(-) rename src/main/groovy-tests/{replaceRecyclingTests.groovy => recipeRecyclingTests.groovy} (73%) diff --git a/src/main/groovy-tests/replaceRecyclingTests.groovy b/src/main/groovy-tests/recipeRecyclingTests.groovy similarity index 73% rename from src/main/groovy-tests/replaceRecyclingTests.groovy rename to src/main/groovy-tests/recipeRecyclingTests.groovy index 5c633e87..1aa6c316 100644 --- a/src/main/groovy-tests/replaceRecyclingTests.groovy +++ b/src/main/groovy-tests/recipeRecyclingTests.groovy @@ -28,4 +28,19 @@ replaceRecipeInput("electric_motor_hv", [ [metaitem('plateWroughtIron'), metaitem('plateWroughtIron'), metaitem('plateWroughtIron')], [metaitem('plateWroughtIron'), ore('toolWrench'), metaitem('plateWroughtIron')], [metaitem('plateWroughtIron'), metaitem('plateWroughtIron'), metaitem('plateWroughtIron')] -]) \ No newline at end of file +]) + +// Add a recipe with recycling, with a defined name +createRecipe("uhv_batbuf_4x", metaitem('battery_buffer.uhv.4'), [ + [null, null, null], + [null, metaitem('battery_buffer.uv.4'), null], + [null, null, null]]) + +// Add a recipe with recycling, without a defined name +createRecipe(metaitem('battery_buffer.uhv.8'), [ + [null, null, null], + [null, metaitem('battery_buffer.uv.8'), null], + [null, null, null]]) + +// Add / Change recycling to a stack +changeStackRecycling(metaitem('battery_buffer.uhv.16'), [metaitem('battery_buffer.uv.16'), metaitem('charger.uv')]) \ No newline at end of file diff --git a/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java b/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java index 9b328fc3..d86f333b 100644 --- a/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java +++ b/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java @@ -119,6 +119,12 @@ public static void replaceRecipeInput(String name, List> input else replaceRecipeInput(GTUtility.gregtechId(name), inputs); } + public static void createRecipe(String name, ItemStack output, List> inputs) { + ReplaceRecipe.createRecipe(name, output, inputs); + } + public static void createRecipe(ItemStack output, List> inputs) { + ReplaceRecipe.createRecipe(output, inputs); + } public static void changeStackRecycling(ItemStack output, List ingredients) { ReplaceRecipe.changeStackRecycling(output, ingredients); } diff --git a/src/main/java/com/nomiceu/nomilabs/groovy/ReplaceRecipe.java b/src/main/java/com/nomiceu/nomilabs/groovy/ReplaceRecipe.java index 901c0709..034342e1 100644 --- a/src/main/java/com/nomiceu/nomilabs/groovy/ReplaceRecipe.java +++ b/src/main/java/com/nomiceu/nomilabs/groovy/ReplaceRecipe.java @@ -24,6 +24,7 @@ import net.minecraftforge.common.crafting.IShapedRecipe; import net.minecraftforge.fml.common.registry.ForgeRegistries; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; @@ -53,7 +54,7 @@ private static void removeRecipesInCategory(RecipeMap recipeMap, GTRecipeCate public static void replaceRecipeShaped(ResourceLocation name, ItemStack output, List> inputs) { validate(name, output, true); crafting.remove(name); - crafting.addShaped(LabsNames.makeLabsName(name.getPath()), output, inputs); + crafting.addShaped(LabsNames.makeGroovyName(name.getPath()), output, inputs); registerRecycling(output, inputs); } @@ -63,7 +64,7 @@ public static void replaceRecipeOutput(ResourceLocation name, ItemStack newOutpu var newCount = newOutput.getCount(); crafting.remove(name); - ReloadableRegistryManager.addRegistryEntry(ForgeRegistries.RECIPES, LabsNames.makeLabsName(name.getPath()), new PartialRecipe(originalRecipe, newOutput.copy())); + ReloadableRegistryManager.addRegistryEntry(ForgeRegistries.RECIPES, LabsNames.makeGroovyName(name.getPath()), new PartialRecipe(originalRecipe, newOutput.copy())); ImmutableList originalMaterials = Objects.requireNonNull(OreDictUnifier.getMaterialInfo(newOutput)).getMaterials(); List newMaterials = new ArrayList<>(); @@ -78,10 +79,21 @@ public static void replaceRecipeInput(ResourceLocation name, List> input) { + crafting.addShaped(LabsNames.makeGroovyName(name), output, input); + registerRecycling(output, input); + } + + public static void createRecipe(ItemStack output, List> input) { + crafting.addShaped(output, input); + registerRecycling(output, input); + } + + public static void changeStackRecycling(ItemStack output, List ingredients) { registerRecycling(output, Collections.singletonList(ingredients)); } @@ -111,7 +123,9 @@ private static void registerRecycling(ItemStack output, List> List gtInputs = new ArrayList<>(); for (var inputList : inputs) { for (var input : inputList) { - gtInputs.add(ofGroovyIngredient(input)); + var gtInput = ofGroovyIngredient(input); + if (gtInput != null) + gtInputs.add(gtInput); } } LabsVirtualizedRegistries.REPLACE_RECIPE_MANAGER.registerOre(output, RecyclingHandler.getRecyclingIngredients(gtInputs, output.getCount())); @@ -130,6 +144,7 @@ private static ItemStack checkAndGetOutput(ItemStack output, boolean validateOut } @SuppressWarnings("ConstantValue") + @Nullable private static GTRecipeInput ofGroovyIngredient(IIngredient ingredient) { if (ingredient instanceof OreDictIngredient oreDictIngredient) { return new GTRecipeOreInput(oreDictIngredient.getOreDict(), ingredient.getAmount()); @@ -137,6 +152,6 @@ private static GTRecipeInput ofGroovyIngredient(IIngredient ingredient) { if ((Object) ingredient instanceof ItemStack stack) { return new GTRecipeItemInput(stack); } - throw new IllegalArgumentException("Could not add groovy ingredient " + ingredient + " to recipe!"); + return null; } } diff --git a/src/main/java/com/nomiceu/nomilabs/util/LabsNames.java b/src/main/java/com/nomiceu/nomilabs/util/LabsNames.java index 79374c6c..c07157be 100644 --- a/src/main/java/com/nomiceu/nomilabs/util/LabsNames.java +++ b/src/main/java/com/nomiceu/nomilabs/util/LabsNames.java @@ -1,5 +1,6 @@ package com.nomiceu.nomilabs.util; +import com.cleanroommc.groovyscript.helper.GroovyHelper; import com.nomiceu.nomilabs.LabsValues; import net.minecraft.util.ResourceLocation; @@ -7,4 +8,8 @@ public class LabsNames { public static ResourceLocation makeLabsName(String name) { return new ResourceLocation(LabsValues.LABS_MODID, name); } + + public static ResourceLocation makeGroovyName(String name) { + return new ResourceLocation(GroovyHelper.getPackId(), name); + } }