Skip to content

Commit 8237564

Browse files
committed
Add some overloads to help create basic multi input ingredients. Also update docs to state that the default components are not included on the ingredient when created by item or stack, and that the ingredient needs to be manually created
1 parent 74ba83a commit 8237564

File tree

9 files changed

+176
-52
lines changed

9 files changed

+176
-52
lines changed

src/api/java/mekanism/api/recipes/ingredients/creator/IChemicalStackIngredientCreator.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ default STACK_INGREDIENT from(IChemicalProvider<CHEMICAL> provider, long amount)
4747
return from(chemicalCreator().of(provider), amount);
4848
}
4949

50+
@Override
51+
default STACK_INGREDIENT from(int amount, CHEMICAL... chemicals) {
52+
return from((long) amount, chemicals);
53+
}
54+
55+
/**
56+
* Creates a Chemical Stack Ingredient that matches any of the provided chemicals.
57+
*
58+
* @param amount Amount needed.
59+
* @param chemicals Chemicals to match.
60+
*
61+
* @throws NullPointerException if the given instance is null.
62+
* @throws IllegalArgumentException if the given instance is empty or an amount smaller than one; or if no types or only a single type is passed. Call via
63+
* {@link #from(IChemicalProvider, long)} if you only have one element.
64+
* @since 10.6.0
65+
*/
66+
default STACK_INGREDIENT from(long amount, IChemicalProvider<CHEMICAL>... chemicals) {
67+
if (chemicals.length < 2) {
68+
throw new IllegalArgumentException("Attempted to create an ChemicalStackIngredients with less than two chemicals. At least one chemical is required, and if you only have one use from(IChemicalProvider, long) instead.");
69+
}
70+
return from(chemicalCreator().of(chemicals), amount);
71+
}
72+
5073
/**
5174
* Creates a Chemical Stack Ingredient that matches a provided chemical and amount.
5275
*

src/api/java/mekanism/api/recipes/ingredients/creator/IFluidStackIngredientCreator.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mekanism.api.recipes.ingredients.creator;
22

3+
import java.util.Arrays;
34
import java.util.Objects;
45
import mekanism.api.annotations.NothingNullByDefault;
56
import mekanism.api.providers.IFluidProvider;
@@ -23,29 +24,69 @@ public interface IFluidStackIngredientCreator extends IIngredientCreator<Fluid,
2324
*
2425
* @throws NullPointerException if the given instance is null.
2526
* @throws IllegalArgumentException if the given instance is empty or an amount smaller than one.
27+
* @implNote This wraps via {@link #from(FluidIngredient, int)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the
28+
* ingredient. If this is not desired, manually create the ingredient via {@link DataComponentFluidIngredient} and call {@link #from(FluidIngredient, int)}.
2629
*/
2730
default FluidStackIngredient from(IFluidProvider provider, int amount) {
2831
Objects.requireNonNull(provider, "FluidStackIngredients cannot be created from a null fluid provider.");
2932
return from(provider.getFluidStack(amount));
3033
}
3134

35+
/**
36+
* Creates an Item Stack Ingredient that matches a provided items and amount.
37+
*
38+
* @param amount Amount needed.
39+
* @param fluids Fluid providers that provides the items to match.
40+
*
41+
* @implNote This wraps via {@link #from(FluidIngredient, int)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the
42+
* ingredient. If this is not desired, manually create the ingredient via {@link DataComponentFluidIngredient} and call {@link #from(FluidIngredient, int)}.
43+
* @since 10.6.0
44+
*/
45+
default FluidStackIngredient from(int amount, IFluidProvider... fluids) {
46+
return from(amount, Arrays.stream(fluids).map(IFluidProvider::getFluid).toArray(Fluid[]::new));
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*
52+
* @implNote This wraps via {@link #from(FluidIngredient, int)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the
53+
* ingredient. If this is not desired, manually create the ingredient via {@link DataComponentFluidIngredient} and call {@link #from(FluidIngredient, int)}.
54+
*/
3255
@Override
3356
default FluidStackIngredient from(Fluid instance, int amount) {
3457
return from(SizedFluidIngredient.of(instance, amount));
3558
}
3659

60+
/**
61+
* {@inheritDoc}
62+
*
63+
* @implNote This wraps via {@link #from(FluidIngredient, int)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the
64+
* ingredient. If this is not desired, manually create the ingredient via {@link DataComponentFluidIngredient} and call {@link #from(FluidIngredient, int)}.
65+
* @since 10.6.0
66+
*/
67+
@Override
68+
default FluidStackIngredient from(int amount, Fluid... fluids) {
69+
if (fluids.length < 2) {
70+
throw new IllegalArgumentException("Attempted to create an FluidStackIngredient with less than two fluids. At least one fluid is required, and if you only have one use from(Fluid, int) instead.");
71+
}
72+
return from(FluidIngredient.of(fluids), amount);
73+
}
74+
75+
/**
76+
* {@inheritDoc}
77+
*
78+
* @implNote If the stack has any non-default data components, a non-strict component matching those additions will be used.
79+
*/
3780
@Override
3881
default FluidStackIngredient from(FluidStack instance) {
39-
//TODO - 1.20.5: Helper for this and item stack ingredient creator to create compound ingredients?
40-
// Maybe by having a var-arg variant of this?
4182
Objects.requireNonNull(instance, "FluidStackIngredients cannot be created from a null FluidStack.");
4283
if (instance.isEmpty()) {
4384
throw new IllegalArgumentException("FluidStackIngredients cannot be created using the empty stack.");
4485
}
4586
//Copy the stack to ensure it doesn't get modified afterward
4687
instance = instance.copy();
4788
//Support Components that are on the stack in case it matters
48-
// Note: Only bother making it a data component ingredient if the stack has data, otherwise there is no point in doing the extra checks
89+
// Note: Only bother making it a data component ingredient if the stack has non-default data, otherwise there is no point in doing the extra checks
4990
DataComponentPredicate predicate = IngredientCreatorAccess.getComponentPatchPredicate(instance.getComponentsPatch());
5091
if (predicate != null) {
5192
return from(DataComponentFluidIngredient.of(false, predicate, instance.getFluidHolder()), instance.getAmount());

src/api/java/mekanism/api/recipes/ingredients/creator/IIngredientCreator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ public interface IIngredientCreator<TYPE, STACK, INGREDIENT extends InputIngredi
3333
*/
3434
INGREDIENT from(TYPE instance, int amount);
3535

36+
/**
37+
* Creates an Ingredient that matches any of the provided types.
38+
*
39+
* @param amount Amount needed.
40+
* @param items Types to match.
41+
*
42+
* @throws NullPointerException if the given instance is null.
43+
* @throws IllegalArgumentException if the given instance is empty or an amount smaller than one; or if no types or only a single type is passed. Call via
44+
* {@link #from(Object, int)} if you only have one element.
45+
* @since 10.6.0
46+
*/
47+
INGREDIENT from(int amount, TYPE... items);
48+
3649
/**
3750
* Creates an Ingredient that matches a provided type and amount.
3851
*

src/api/java/mekanism/api/recipes/ingredients/creator/IItemStackIngredientCreator.java

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
@NothingNullByDefault
1717
public interface IItemStackIngredientCreator extends IIngredientCreator<Item, ItemStack, ItemStackIngredient> {
1818

19+
/**
20+
* {@inheritDoc}
21+
*
22+
* @implNote If the stack has any non-default data components, a non-strict component matching those additions will be used.
23+
*/
1924
@Override
2025
default ItemStackIngredient from(ItemStack instance) {
2126
Objects.requireNonNull(instance, "ItemStackIngredients cannot be created from a null ItemStack.");
@@ -29,6 +34,7 @@ default ItemStackIngredient from(ItemStack instance) {
2934
* @param amount Amount needed.
3035
*
3136
* @apiNote If the amount needed is the same as the stack's size, {@link #from(ItemStack)} can be used instead.
37+
* @implNote If the stack has any non-default data components, a non-strict component matching those additions will be used.
3238
*/
3339
default ItemStackIngredient from(ItemStack stack, int amount) {
3440
Objects.requireNonNull(stack, "ItemStackIngredients cannot be created from a null ItemStack.");
@@ -38,7 +44,7 @@ default ItemStackIngredient from(ItemStack stack, int amount) {
3844
//Copy the stack to ensure it doesn't get modified afterward
3945
stack = stack.copy();
4046
//Support Components that are on the stack in case it matters
41-
// Note: Only bother making it a data component ingredient if the stack has data, otherwise there is no point in doing the extra checks
47+
// Note: Only bother making it a data component ingredient if the stack has non-default data, otherwise there is no point in doing the extra checks
4248
DataComponentPredicate predicate = IngredientCreatorAccess.getComponentPatchPredicate(stack.getComponentsPatch());
4349
if (predicate != null) {
4450
return from(DataComponentIngredient.of(false, predicate, stack.getItemHolder()), amount);
@@ -64,8 +70,8 @@ default ItemStackIngredient fromHolder(Holder<Item> item) {
6470
*
6571
* @param item Item provider that provides the item to match.
6672
*
67-
* @implNote This wraps via {@link #from(ItemStack)} so if there is any durability or default NBT it will be included in the ingredient. If this is not desired,
68-
* manually create an ingredient and call {@link #from(Ingredient)}.
73+
* @implNote This wraps via {@link #from(Ingredient)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the ingredient. If this
74+
* is not desired, manually create the ingredient via {@link DataComponentIngredient} and call {@link #from(Ingredient)}.
6975
*/
7076
default ItemStackIngredient from(ItemLike item) {
7177
return from(item, 1);
@@ -77,24 +83,68 @@ default ItemStackIngredient from(ItemLike item) {
7783
* @param item Item provider that provides the item to match.
7884
* @param amount Amount needed.
7985
*
80-
* @implNote This wraps via {@link #from(ItemStack, int)} so if there is any durability or default NBT it will be included in the ingredient. If this is not desired,
81-
* manually create an ingredient and call {@link #from(Ingredient, int)}.
86+
* @implNote This wraps via {@link #from(Ingredient, int)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the ingredient. If
87+
* this is not desired, manually create the ingredient via {@link DataComponentIngredient} and call {@link #from(Ingredient, int)}.
8288
*/
8389
default ItemStackIngredient from(ItemLike item, int amount) {
84-
return from(new ItemStack(item), amount);
90+
return from(Ingredient.of(item), amount);
91+
}
92+
93+
/**
94+
* Creates an Item Stack Ingredient that matches a provided items.
95+
*
96+
* @param items Item providers that provides the items to match.
97+
*
98+
* @throws IllegalArgumentException if no items are passed, or only a single item is passed. Call via {@link #from(ItemLike)} if you only have one element.
99+
* @implNote This wraps via {@link #from(Ingredient)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the ingredient. If this
100+
* is not desired, manually create the ingredients via {@link DataComponentIngredient} and call {@link #from(Ingredient)}.
101+
* @since 10.6.0
102+
*/
103+
default ItemStackIngredient from(ItemLike... items) {
104+
return from(1, items);
105+
}
106+
107+
/**
108+
* Creates an Item Stack Ingredient that matches a provided items and amount.
109+
*
110+
* @param amount Amount needed.
111+
* @param items Item providers that provides the items to match.
112+
*
113+
* @throws IllegalArgumentException if no items are passed, or only a single item is passed. Call via {@link #from(ItemLike, int)} if you only have one element.
114+
* @implNote This wraps via {@link #from(Ingredient, int)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the ingredient. If
115+
* this is not desired, manually create the ingredients via {@link DataComponentIngredient} and call {@link #from(Ingredient, int)}.
116+
* @since 10.6.0
117+
*/
118+
default ItemStackIngredient from(int amount, ItemLike... items) {
119+
if (items.length < 2) {
120+
throw new IllegalArgumentException("Attempted to create an ItemStackIngredient with less than two items. At least one item is required, and if you only have one use from(ItemLike, int) instead.");
121+
}
122+
return from(Ingredient.of(items), amount);
85123
}
86124

87125
/**
88126
* {@inheritDoc}
89127
*
90-
* @implNote This wraps via {@link #from(ItemStack)} so if there is any durability or default NBT it will be included in the ingredient. If this is not desired,
91-
* manually create an ingredient and call {@link #from(Ingredient)}.
128+
* @implNote This wraps via {@link #from(Ingredient)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the ingredient. If this
129+
* is not desired, manually create the ingredient via {@link DataComponentIngredient} and call {@link #from(Ingredient)}.
92130
*/
93131
@Override
94132
default ItemStackIngredient from(Item item, int amount) {
95133
return from((ItemLike) item, amount);
96134
}
97135

136+
/**
137+
* {@inheritDoc}
138+
*
139+
* @implNote This wraps via {@link #from(Ingredient)} so if there is any durability or default NBT it will <strong>NOT</strong> be included in the ingredient. If this
140+
* is not desired, manually create the ingredient via {@link DataComponentIngredient} and call {@link #from(Ingredient)}.
141+
* @since 10.6.0
142+
*/
143+
@Override
144+
default ItemStackIngredient from(int amount, Item... items) {
145+
return from(amount, (ItemLike[]) items);
146+
}
147+
98148
/**
99149
* Creates an Item Stack Ingredient that matches a given Item tag.
100150
*

src/datagen/main/java/mekanism/common/recipe/RecipeProviderUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ public static void addPrecisionSawmillWoodTypeRecipes(RecipeOutput consumer, Str
138138

139139
public static void addSandStoneToSandRecipe(RecipeOutput consumer, String path, @Nullable ICondition condition, ItemLike sand, TagKey<Item> sandstoneTag) {
140140
build(consumer, ItemStackToItemStackRecipeBuilder.crushing(
141-
IngredientCreatorAccess.item().from(Ingredient.of(sandstoneTag)),
141+
IngredientCreatorAccess.item().from(sandstoneTag),
142142
new ItemStack(sand, 2)
143143
), path, condition);
144144
}
145145

146146
@Deprecated
147147
public static void addSandStoneToSandRecipe(RecipeOutput consumer, String path, @Nullable ICondition condition, ItemLike sand, ItemLike... sandstones) {
148148
build(consumer, ItemStackToItemStackRecipeBuilder.crushing(
149-
IngredientCreatorAccess.item().from(Ingredient.of(sandstones)),
149+
IngredientCreatorAccess.item().from(sandstones),
150150
new ItemStack(sand, 2)
151151
), path, condition);
152152
}

src/datagen/main/java/mekanism/common/recipe/compat/BiomesOPlentyRecipeProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import net.minecraft.world.item.Item;
1717
import net.minecraft.world.item.ItemStack;
1818
import net.minecraft.world.item.Items;
19-
import net.minecraft.world.item.crafting.Ingredient;
2019
import net.minecraft.world.level.ItemLike;
2120
import net.minecraft.world.level.block.Block;
2221

@@ -118,7 +117,7 @@ private void addDyeRecipes(RecipeOutput consumer, String basePath) {
118117
}
119118

120119
private void dye(RecipeOutput consumer, String basePath, ItemLike output, EnumColor color, Block... inputs) {
121-
ItemStackIngredient inputIngredient = IngredientCreatorAccess.item().from(Ingredient.of(inputs));
120+
ItemStackIngredient inputIngredient = IngredientCreatorAccess.item().from(inputs);
122121
ItemStackToItemStackRecipeBuilder.enriching(
123122
inputIngredient,
124123
new ItemStack(output, 2)
@@ -134,7 +133,7 @@ private void dye(RecipeOutput consumer, String basePath, ItemLike output, EnumCo
134133
}
135134

136135
private void largeDye(RecipeOutput consumer, String basePath, ItemLike output, EnumColor color, Block... inputs) {
137-
ItemStackIngredient inputIngredient = IngredientCreatorAccess.item().from(Ingredient.of(inputs));
136+
ItemStackIngredient inputIngredient = IngredientCreatorAccess.item().from(inputs);
138137
ItemStackToItemStackRecipeBuilder.enriching(
139138
inputIngredient,
140139
new ItemStack(output, 4)

src/datagen/main/java/mekanism/common/recipe/impl/CrusherRecipeProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import net.minecraft.world.item.HoneycombItem;
1414
import net.minecraft.world.item.ItemStack;
1515
import net.minecraft.world.item.Items;
16-
import net.minecraft.world.item.crafting.Ingredient;
1716
import net.minecraft.world.level.block.Block;
1817
import net.minecraft.world.level.block.Blocks;
1918
import net.neoforged.neoforge.common.Tags;
@@ -104,10 +103,10 @@ public void addRecipes(RecipeOutput consumer) {
104103
).build(consumer, Mekanism.rl(basePath + "soul_soil_to_soul_sand"));
105104
//Polished or Smooth Basalt -> Basalt
106105
ItemStackToItemStackRecipeBuilder.crushing(
107-
IngredientCreatorAccess.item().from(Ingredient.of(
106+
IngredientCreatorAccess.item().from(
108107
Blocks.POLISHED_BASALT,
109108
Blocks.SMOOTH_BASALT
110-
)),
109+
),
111110
new ItemStack(Blocks.BASALT)
112111
).build(consumer, Mekanism.rl(basePath + "polished_or_smooth_basalt_to_basalt"));
113112
//Chiseled Nether Bricks -> Nether Bricks

0 commit comments

Comments
 (0)