Skip to content

Commit

Permalink
Copy Property Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jul 31, 2024
1 parent 5d63b49 commit 9ddd116
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/groovy-tests/recipeMapTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import gregtech.api.recipes.RecipeMaps
import gregtech.api.recipes.chance.output.impl.ChancedItemOutput
import gregtech.api.recipes.ingredients.nbtmatch.NBTCondition
import gregtech.api.recipes.ingredients.nbtmatch.NBTMatcher
import gregtech.api.recipes.recipeproperties.CleanroomProperty
import gregtech.api.recipes.recipeproperties.ResearchProperty
import gregtech.api.recipes.recipeproperties.TemperatureProperty

import static com.nomiceu.nomilabs.groovy.GroovyHelpers.GTRecipeHelpers.*
import static gregtech.api.GTValues.*
Expand Down Expand Up @@ -161,4 +164,48 @@ mods.gregtech.chemical_reactor.changeByOutput(null, [fluid('polytetrafluoroethyl
.buildAndRegister()
}

// Example 6: Doubling the Output of the Crystal Processor Assembly Circuit Recipes
mods.gregtech.circuit_assembler.changeByOutput([metaitem('circuit.crystal_assembly')], null)
.forEach { ChangeRecipeBuilder builder ->
builder.changeEachOutput { stack ->
stack.count *= 2
return stack
}
.copyProperties(CleanroomProperty.instance) // Copy the CLEANROOM Property!
.replaceAndRegister()
}

// Example 7: Adding an Alternative Blast Furnace Recipe for Red Steel, with Double Output but Double Temperature
// Alternative = `buildAndRegister` not `replaceAndRegister`
mods.gregtech.electric_blast_furnace.changeByOutput([metaitem('ingotRedSteel')], null)
.forEach { ChangeRecipeBuilder builder ->
builder.changeEachOutput { stack ->
stack.count *= 2
return stack
}
.changeCircuitMeta { meta -> meta + 10 }
/*
* It is important that you, somehow, make a copy of the input property, and not modify the property itself!
* Otherwise, reloading may not work correctly, and can cause modifications to be applied on top of each other!
*
* Note that the `changeProperty` method does not allow for property string keys, you must input an object.
*
* Note that this is not needed for the TemperatureProperty, as it simply stores an integer.
* (Also note that you would do `copyProperties(TemperatureProperty.instance)` to copy the temperature property.
*/
.changeProperty(TemperatureProperty.instance, temp -> temp * 2)
.buildAndRegister()
}

// Example 8: Doubling the Output of the Fusion MK I Controller Assembly Line Recipe (With Recycling)
mods.gregtech.assembly_line.changeByOutput([metaitem('fusion_reactor.luv')], null)
.forEach { ChangeRecipeBuilder builder ->
builder.changeEachOutput { stack ->
stack.count *= 2
return stack
}.builder { recipe -> recipe.changeRecycling() }
.copyProperties(ResearchProperty.instance)
.replaceAndRegister()
}

// See {@link com.nomiceu.nomilabs.groovy.ChangeRecipeBuilder} for more functions!
30 changes: 30 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/groovy/ChangeRecipeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

import com.cleanroommc.groovyscript.api.GroovyLog;
import com.nomiceu.nomilabs.gregtech.mixinhelper.AccessibleIntCircuitIngredient;

import gregtech.api.recipes.Recipe;
Expand All @@ -18,6 +19,7 @@
import gregtech.api.recipes.chance.output.impl.ChancedItemOutput;
import gregtech.api.recipes.ingredients.GTRecipeInput;
import gregtech.api.recipes.ingredients.IntCircuitIngredient;
import gregtech.api.recipes.recipeproperties.RecipeProperty;

@SuppressWarnings({ "unused", "UnusedReturnValue" })
public class ChangeRecipeBuilder<R extends RecipeBuilder<R>> {
Expand Down Expand Up @@ -240,6 +242,34 @@ public ChangeRecipeBuilder<R> changeChancedFluidOutputLogic(Function<ChancedOutp
return this;
}

public ChangeRecipeBuilder<R> copyProperties(RecipeProperty<?>... properties) {
for (var property : properties) {
var prop = originalRecipe.getProperty(property, null);
if (prop == null) {
GroovyLog.get().error("Could not find property {} in recipe {}!", property.getKey(), originalRecipe);
continue;
}

builder.applyProperty(property, prop);
}
return this;
}

/**
* It is important that you, somehow, make a copy of the input property, and not modify the property itself!<br>
* Otherwise, reloading may not work correctly, and can cause modifications to be applied on top of each other!
*/
public <T> ChangeRecipeBuilder<R> changeProperty(RecipeProperty<T> property, Function<T, T> changer) {
T prop = originalRecipe.getProperty(property, null);
if (prop == null) {
GroovyLog.get().error("Could not find property {} in recipe {}!", property.getKey(), originalRecipe);
return this;
}

builder.applyProperty(property, changer.apply(prop));
return this;
}

public void buildAndRegister() {
builder.buildAndRegister();
}
Expand Down

0 comments on commit 9ddd116

Please sign in to comment.