Skip to content

Commit d20f453

Browse files
committed
More code cleanup / enhancements
Make Packer and Unpacker RecipeMaps use IntCircuitRecipeBuilder - Allows for use of circuitMeta instead of having to manually add catalyst integrated circuits. Enhancements to CraftingComponent: - Split out MATERIAL_COMPONENT from ROTOR - These materials are used elsewhere, e.g. screws - Add Component for Fluid Regulators Enhancements to ModHandler: Additional Substitution 'sub' constructor options - from an OrePrefix and Material - from a tier and a Component - Supports UnificationEntry, ItemStack, String, MetaValueItem, and LookupBlock typed Component, otherwise throws - from a LookupBlock Enhancements to CountableIngredient: To facilitate the above changes to ModHandler, - Add from method for Component<T> supporting <T> of types: - UnificationEntry - ItemStack - String - MetaValueItem - LookupBlock - Add a convenience overload of "from" for UnificationEntry for getting a stack size of 1 Enhancements to RecipeBuilder: - Add methods for handling singular UnificationEntry input with optional count (defaults to stack of 1) - Add more input/output options for less verbose code: - input now supports LookupBlock, MetaValueItem, Component - output now supports LookupBlock, MetaValueItem, MetaTileEntity, and Component - each of above with the option to provide a count Code Cleanup / Refactoring in RecipeLoaders: - Formatting / line breaks to reduce depth of indents - Use new Substitution methods from above to reduce verbosity - Try to standardize the order of RecipeBuilder calls, roughly: output(s) fluidOutput(s) input(s) fluidInput(s) chancedOutput(s) nonConsumable circuitMeta duration and EUt
1 parent 57ce93d commit d20f453

File tree

9 files changed

+2070
-1268
lines changed

9 files changed

+2070
-1268
lines changed

src/main/java/gregtech/api/recipes/CountableIngredient.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package gregtech.api.recipes;
22

3+
import gregtech.api.items.metaitem.MetaItem;
34
import gregtech.api.unification.material.type.Material;
45
import gregtech.api.unification.ore.OrePrefix;
56
import gregtech.api.unification.stack.UnificationEntry;
7+
import gregtech.common.blocks.LookupBlock;
8+
import gregtech.loaders.recipe.Component;
69
import net.minecraft.item.ItemStack;
710
import net.minecraft.item.crafting.Ingredient;
811
import net.minecraftforge.oredict.OreIngredient;
@@ -43,6 +46,28 @@ public static CountableIngredient from(UnificationEntry entry, int count) {
4346
return from(entry.orePrefix, entry.material, count);
4447
}
4548

49+
/** @throws java.lang.NullPointerException if the UnificationEntry's material is {@code null} */
50+
public static CountableIngredient from(UnificationEntry entry) {
51+
return from(entry, 1);
52+
}
53+
54+
/** @throws java.lang.IllegalArgumentException for unsupported types */
55+
public static <T> CountableIngredient from(int tier, Component<T> component, int count) {
56+
T resolved = component.getIngredient(tier);
57+
if(resolved instanceof UnificationEntry ue)
58+
return from(ue, count);
59+
if(resolved instanceof ItemStack stack)
60+
return from(stack, count);
61+
if(resolved instanceof String str)
62+
return from(str, count);
63+
if(resolved instanceof MetaItem<?>.MetaValueItem meta)
64+
return from(meta.getStackForm(count));
65+
if(resolved instanceof LookupBlock<?> block)
66+
return from(block.getStack(), count);
67+
68+
throw new IllegalArgumentException("unsupported type");
69+
}
70+
4671
private Ingredient ingredient;
4772
private int count;
4873

src/main/java/gregtech/api/recipes/ModHandler.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import gregtech.api.util.ShapedOreEnergyTransferRecipe;
1818
import gregtech.api.util.world.DummyWorld;
1919
import gregtech.common.MetaFluids;
20+
import gregtech.common.blocks.LookupBlock;
21+
import gregtech.loaders.recipe.Component;
2022
import net.minecraft.block.Block;
2123
import net.minecraft.creativetab.CreativeTabs;
2224
import net.minecraft.inventory.InventoryCrafting;
@@ -25,6 +27,7 @@
2527
import net.minecraft.item.crafting.CraftingManager;
2628
import net.minecraft.item.crafting.FurnaceRecipes;
2729
import net.minecraft.item.crafting.IRecipe;
30+
import net.minecraft.util.IStringSerializable;
2831
import net.minecraft.util.NonNullList;
2932
import net.minecraft.util.ResourceLocation;
3033
import net.minecraft.world.World;
@@ -218,6 +221,19 @@ public record Substitution<T>(char key, @Nullable T value) {
218221
public static <T> Substitution<T> sub(char k, T v) {
219222
return new Substitution<>(k, v);
220223
}
224+
225+
public static Substitution<UnificationEntry> sub(char k, OrePrefix p, Material m) {
226+
return new Substitution<>(k, new UnificationEntry(p, m));
227+
}
228+
229+
public static <T> Substitution<T> sub(char k, int tier, Component<T> v) {
230+
return new Substitution<>(k, v.getIngredient(tier));
231+
}
232+
233+
public static <T extends Enum<T> & IStringSerializable>
234+
Substitution<ItemStack> sub(char k, LookupBlock<T> block) {
235+
return new Substitution<>(k, block.getStack());
236+
}
221237
}
222238

223239
/**

src/main/java/gregtech/api/recipes/RecipeBuilder.java

Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
package gregtech.api.recipes;
22

33
import gregtech.api.items.metaitem.MetaItem;
4+
import gregtech.api.metatileentity.MetaTileEntity;
45
import gregtech.api.recipes.Recipe.ChanceEntry;
56
import gregtech.api.unification.material.type.FluidMaterial;
67
import gregtech.api.unification.OreDictUnifier;
78
import gregtech.api.unification.material.type.Material;
89
import gregtech.api.unification.ore.OrePrefix;
10+
import gregtech.api.unification.stack.UnificationEntry;
911
import gregtech.api.util.EnumValidationResult;
1012
import gregtech.api.util.GTLog;
1113
import gregtech.api.util.GTUtility;
1214
import gregtech.api.util.ValidationResult;
15+
import gregtech.common.blocks.LookupBlock;
16+
import gregtech.loaders.recipe.Component;
1317
import net.minecraft.block.Block;
1418
import net.minecraft.item.Item;
1519
import net.minecraft.item.ItemStack;
1620
import net.minecraft.item.crafting.Ingredient;
21+
import net.minecraft.util.IStringSerializable;
1722
import net.minecraft.util.NonNullList;
1823
import net.minecraftforge.fluids.FluidStack;
1924
import net.minecraftforge.oredict.OreDictionary;
2025
import org.apache.commons.lang3.builder.ToStringBuilder;
2126

2227
import java.util.*;
28+
import java.util.function.Predicate;
29+
30+
import static gregtech.api.recipes.CountableIngredient.from;
2331

2432
/**
2533
* @see Recipe
@@ -94,30 +102,56 @@ public R inputs(ItemStack... inputs) {
94102
return inputs(Arrays.asList(inputs));
95103
}
96104

105+
private static final Predicate<ItemStack> nullOrEmpty = stack -> stack == null || stack.isEmpty();
106+
97107
public R inputs(Collection<ItemStack> inputs) {
98-
if (GTUtility.iterableContains(inputs, stack -> stack == null || stack.isEmpty())) {
108+
if (GTUtility.iterableContains(inputs, nullOrEmpty)) {
99109
GTLog.logger.error("Input cannot contain null or empty ItemStacks. Inputs: {}", inputs);
100110
GTLog.logger.error("Stacktrace:", new IllegalArgumentException());
101111
recipeStatus = EnumValidationResult.INVALID;
102112
}
103-
inputs.forEach(stack -> {
104-
if (!(stack == null || stack.isEmpty())) {
105-
this.inputs.add(CountableIngredient.from(stack));
106-
}
107-
});
113+
114+
for(ItemStack stack : inputs)
115+
if (!nullOrEmpty.test(stack))
116+
this.inputs.add(from(stack));
117+
108118
return (R) this;
109119
}
110120

111121
public R input(String oredict, int count) {
112-
return inputs(CountableIngredient.from(oredict, count));
122+
return inputs(from(oredict, count));
113123
}
114124

115125
public R input(OrePrefix orePrefix, Material material) {
116-
return inputs(CountableIngredient.from(orePrefix, material, 1));
126+
return inputs(from(orePrefix, material, 1));
117127
}
118128

119129
public R input(OrePrefix orePrefix, Material material, int count) {
120-
return inputs(CountableIngredient.from(orePrefix, material, count));
130+
return inputs(from(orePrefix, material, count));
131+
}
132+
133+
public R input(UnificationEntry entry) {
134+
return input(entry, 1);
135+
}
136+
137+
public R input(UnificationEntry entry, int count) {
138+
return inputs(from(entry, count));
139+
}
140+
141+
public <T extends Enum<T> & IStringSerializable> R input(LookupBlock<T> block, int count) {
142+
return inputs(block.getStack(count));
143+
}
144+
145+
public <T extends Enum<T> & IStringSerializable> R input(LookupBlock<T> block) {
146+
return input(block, 1);
147+
}
148+
149+
public R input(MetaItem<?>.MetaValueItem item, int count) {
150+
return inputs(item.getStackForm(count));
151+
}
152+
153+
public R input(MetaItem<?>.MetaValueItem item) {
154+
return inputs(item.getStackForm());
121155
}
122156

123157
public R input(Item item) {
@@ -148,6 +182,33 @@ public R input(Block item, int count, boolean wild) {
148182
return inputs(new ItemStack(item, count, OreDictionary.WILDCARD_VALUE));
149183
}
150184

185+
/**
186+
* Resolves a single {@link Component} and adds it to the inputs with a count of 1.
187+
* @throws IllegalArgumentException if the resolved type isn't supported.
188+
*/
189+
public <T> R input(int tier, Component<T> component) {
190+
return input(tier, component, 1);
191+
}
192+
193+
/**
194+
* Resolves a single {@link Component} and adds it to the inputs with the specified count.
195+
* @throws IllegalArgumentException if the resolved type isn't supported.
196+
*/
197+
public R input(int tier, Component<?> component, int count) {
198+
return inputs(from(tier, component, count));
199+
}
200+
201+
/**
202+
* For {@link Component}, which are resolved before adding to inputs.
203+
* @throws IllegalArgumentException if an input type isn't supported
204+
*/
205+
public R inputs(int tier, Component<?>... components) {
206+
List<CountableIngredient> ingredients = new ArrayList<>(components.length);
207+
for(var component : components)
208+
ingredients.add(from(tier, component, 1));
209+
return inputs(ingredients.toArray(new CountableIngredient[0]));
210+
}
211+
151212
public R inputs(CountableIngredient... inputs) {
152213
List<CountableIngredient> ingredients = new ArrayList<>();
153214
for (CountableIngredient input : inputs) {
@@ -168,7 +229,7 @@ public R inputsIngredients(Collection<CountableIngredient> ingredients) {
168229
}
169230

170231
public R notConsumable(ItemStack itemStack) {
171-
return inputs(CountableIngredient.from(itemStack, 0));
232+
return inputs(from(itemStack, 0));
172233
}
173234

174235
public R notConsumable(OrePrefix prefix, Material material) {
@@ -180,7 +241,7 @@ public R notConsumable(Ingredient ingredient) {
180241
}
181242

182243
public R notConsumable(MetaItem<?>.MetaValueItem item) {
183-
return inputs(CountableIngredient.from(item.getStackForm(), 0));
244+
return inputs(from(item.getStackForm(), 0));
184245
}
185246

186247
public R notConsumable(FluidMaterial fluidMat) {
@@ -219,6 +280,44 @@ public R output(Block item, int count) {
219280
return outputs(new ItemStack(item, count));
220281
}
221282

283+
public R output(LookupBlock<?> block, int count) {
284+
return outputs(block.getStack(count));
285+
}
286+
287+
public R output(LookupBlock<?> block) {
288+
return outputs(block.getStack());
289+
}
290+
291+
public R output(MetaItem<?>.MetaValueItem item, int count) {
292+
return outputs(item.getStackForm(count));
293+
}
294+
295+
public R output(MetaItem<?>.MetaValueItem item) {
296+
return outputs(item.getStackForm());
297+
}
298+
299+
public R output(MetaTileEntity entity, int count) {
300+
return outputs(entity.getStackForm(count));
301+
}
302+
303+
public R output(MetaTileEntity entity) {
304+
return outputs(entity.getStackForm());
305+
}
306+
307+
/**
308+
* Resolves a single tiered {@link Component} to an ItemStack and adds it to the outputs.
309+
* @throws IllegalArgumentException if the type is not supported.
310+
*/
311+
public <T> R output(int tier, Component<T> component) {
312+
T resolved = component.getIngredient(tier);
313+
if(resolved instanceof ItemStack stack)
314+
return outputs(stack);
315+
if(resolved instanceof MetaItem<?>.MetaValueItem item)
316+
return outputs(item.getStackForm());
317+
318+
throw new IllegalArgumentException("unsupported type");
319+
}
320+
222321
public R outputs(ItemStack... outputs) {
223322
return outputs(Arrays.asList(outputs));
224323
}

src/main/java/gregtech/api/recipes/RecipeMaps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,15 +705,15 @@ public class RecipeMaps {
705705

706706

707707
@ZenProperty
708-
public static final RecipeMap<SimpleRecipeBuilder> PACKER_RECIPES = new RecipeMap<>("packer", 2, 2, 1, 1, 0, 0, 0, 0, new SimpleRecipeBuilder().EUt(12).duration(10))
708+
public static final RecipeMap<IntCircuitRecipeBuilder> PACKER_RECIPES = new RecipeMap<>("packer", 2, 2, 1, 1, 0, 0, 0, 0, new IntCircuitRecipeBuilder().EUt(12).duration(10))
709709
.setSlotOverlay(false, false, true, GuiTextures.BOX_OVERLAY)
710710
.setSlotOverlay(true, false, GuiTextures.BOXED_OVERLAY)
711711
.setProgressBar(GuiTextures.PROGRESS_BAR_ARROW, MoveType.HORIZONTAL)
712712
.setSound(GTSoundEvents.ASSEMBLER);
713713

714714

715715
@ZenProperty
716-
public static final RecipeMap<SimpleRecipeBuilder> UNPACKER_RECIPES = new RecipeMap<>("unpacker", 2, 2, 1, 2, 0, 0, 0, 0, new SimpleRecipeBuilder().EUt(12).duration(10))
716+
public static final RecipeMap<IntCircuitRecipeBuilder> UNPACKER_RECIPES = new RecipeMap<>("unpacker", 2, 2, 1, 2, 0, 0, 0, 0, new IntCircuitRecipeBuilder().EUt(12).duration(10))
717717
.setSlotOverlay(false, false, GuiTextures.BOXED_OVERLAY)
718718
.setProgressBar(GuiTextures.PROGRESS_BAR_ARROW, MoveType.HORIZONTAL)
719719
.setSound(GTSoundEvents.ASSEMBLER);

src/main/java/gregtech/loaders/oreprocessing/MaterialRecipeHandler.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,18 @@ public static void processTinyDust(OrePrefix orePrefix, DustMaterial material) {
168168
ModHandler.addShapedRecipe(String.format("tiny_dust_assembling_%s", material),
169169
dustStack, "XXX", "XXX", "XXX", 'X', new UnificationEntry(orePrefix, material));
170170

171-
RecipeMaps.PACKER_RECIPES.recipeBuilder().input(orePrefix, material, 9)
172-
.inputs(new CountableIngredient(new IntCircuitIngredient(1), 0))
171+
RecipeMaps.PACKER_RECIPES
172+
.recipeBuilder()
173173
.outputs(dustStack)
174+
.input(orePrefix, material, 9)
175+
.circuitMeta(1)
174176
.buildAndRegister();
175177

176-
RecipeMaps.UNPACKER_RECIPES.recipeBuilder().input(OrePrefix.dust, material)
177-
.inputs(new CountableIngredient(new IntCircuitIngredient(1), 0))
178+
RecipeMaps.UNPACKER_RECIPES
179+
.recipeBuilder()
178180
.outputs(GTUtility.copyAmount(9, tinyDustStack))
181+
.input(OrePrefix.dust, material)
182+
.circuitMeta(1)
179183
.buildAndRegister();
180184
}
181185

@@ -306,14 +310,18 @@ public static void processNugget(OrePrefix orePrefix, SolidMaterial material) {
306310
ModHandler.addShapedRecipe(String.format("nugget_assembling_%s", material),
307311
ingotStack, "XXX", "XXX", "XXX", 'X', new UnificationEntry(orePrefix, material));
308312

309-
RecipeMaps.UNPACKER_RECIPES.recipeBuilder().input(OrePrefix.ingot, material)
310-
.inputs(new CountableIngredient(new IntCircuitIngredient(1), 0))
313+
RecipeMaps.UNPACKER_RECIPES
314+
.recipeBuilder()
311315
.outputs(GTUtility.copyAmount(9, nuggetStack))
316+
.input(OrePrefix.ingot, material)
317+
.circuitMeta(1)
312318
.buildAndRegister();
313319

314-
RecipeMaps.PACKER_RECIPES.recipeBuilder().input(orePrefix, material, 9)
315-
.inputs(new CountableIngredient(new IntCircuitIngredient(1), 0))
320+
RecipeMaps.PACKER_RECIPES
321+
.recipeBuilder()
316322
.outputs(ingotStack)
323+
.input(orePrefix, material, 9)
324+
.circuitMeta(1)
317325
.buildAndRegister();
318326

319327
if (material.shouldGenerateFluid()) {

src/main/java/gregtech/loaders/recipe/CraftingComponent.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public class CraftingComponent {
6767
default -> MetaItems.ELECTRIC_PUMP_UV;
6868
};
6969

70+
/** Tiered fluid regulator components */
71+
public static final Component<MetaItem<?>.MetaValueItem> FLUID_REGULATOR = tier -> switch(tier) {
72+
case ULV, LV -> MetaItems.FLUID_REGULATOR_LV;
73+
case MV -> MetaItems.FLUID_REGULATOR_MV;
74+
case HV -> MetaItems.FLUID_REGULATOR_HV;
75+
case EV -> MetaItems.FLUID_REGULATOR_EV;
76+
case IV -> MetaItems.FLUID_REGULATOR_IV;
77+
case LuV -> MetaItems.FLUID_REGULATOR_LUV;
78+
case ZPM -> MetaItems.FLUID_REGULATOR_ZPM;
79+
default -> MetaItems.FLUID_REGULATOR_UV;
80+
};
81+
7082
/** Tiered materials for generic cables used in most crafting recipes. */
7183
public static final Component<Material> CABLE_MATERIALS = tier -> switch(tier) {
7284
case ULV -> Materials.Lead;
@@ -173,21 +185,22 @@ public class CraftingComponent {
173185
default -> MetaItems.ELECTRIC_MOTOR_UV;
174186
};
175187

176-
/** Tiered Rotors */
177-
public static final Component<UnificationEntry> ROTOR = tier -> {
178-
var material = switch(tier) {
179-
case ULV, LV -> Materials.Tin;
180-
case MV -> Materials.Bronze;
181-
case HV -> Materials.Steel;
182-
case EV -> Materials.StainlessSteel;
183-
case IV -> Materials.TungstenSteel;
184-
case LuV -> Materials.Chrome;
185-
case ZPM -> Materials.Iridium;
186-
default -> Materials.Osmium;
187-
};
188-
return new UnificationEntry(OrePrefix.rotor, material);
188+
/** Tiered materials used in components like rotors */
189+
public static final Component<Material> MATERIAL_COMPONENT = tier -> switch(tier) {
190+
case ULV, LV -> Materials.Tin;
191+
case MV -> Materials.Bronze;
192+
case HV -> Materials.Steel;
193+
case EV -> Materials.StainlessSteel;
194+
case IV -> Materials.TungstenSteel;
195+
case LuV -> Materials.Chrome;
196+
case ZPM -> Materials.Iridium;
197+
default -> Materials.Osmium;
189198
};
190199

200+
/** Tiered Rotors */
201+
public static final Component<UnificationEntry> ROTOR =
202+
bind(OrePrefix.rotor, MATERIAL_COMPONENT);
203+
191204
/** Tiered Sensors. ULV uses LV. */
192205
public static final Component<MetaItem<?>.MetaValueItem> SENSOR = tier -> switch(tier) {
193206
case ULV, LV -> MetaItems.SENSOR_LV;

0 commit comments

Comments
 (0)