Skip to content

Commit dc2fbd1

Browse files
committed
Create a way to add extra ingredients in the API (not at runtime)
1 parent c4130f8 commit dc2fbd1

File tree

8 files changed

+98
-6
lines changed

8 files changed

+98
-6
lines changed

CommonApi/src/main/java/mezz/jei/api/IModPlugin.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.minecraft.resources.ResourceLocation;
99

1010
import mezz.jei.api.registration.IAdvancedRegistration;
11+
import mezz.jei.api.registration.IExtraIngredientRegistration;
1112
import mezz.jei.api.registration.IGuiHandlerRegistration;
1213
import mezz.jei.api.registration.IModIngredientRegistration;
1314
import mezz.jei.api.registration.IRecipeCatalystRegistration;
@@ -54,6 +55,16 @@ default void registerIngredients(IModIngredientRegistration registration) {
5455

5556
}
5657

58+
/**
59+
* Register extra ItemStacks that are not in the creative menu,
60+
* or FluidStacks that are different from the default ones available via the fluid registry.
61+
*
62+
* @since 19.18.0
63+
*/
64+
default void registerExtraIngredients(IExtraIngredientRegistration registration) {
65+
66+
}
67+
5768
/**
5869
* Register search aliases for ingredients.
5970
*
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package mezz.jei.api.registration;
2+
3+
import mezz.jei.api.IModPlugin;
4+
import mezz.jei.api.constants.VanillaTypes;
5+
import mezz.jei.api.helpers.IColorHelper;
6+
import mezz.jei.api.helpers.IPlatformFluidHelper;
7+
import mezz.jei.api.ingredients.IIngredientHelper;
8+
import mezz.jei.api.ingredients.IIngredientRenderer;
9+
import mezz.jei.api.ingredients.IIngredientType;
10+
import mezz.jei.api.ingredients.subtypes.ISubtypeManager;
11+
import mezz.jei.api.runtime.IIngredientManager;
12+
import net.minecraft.world.item.ItemStack;
13+
14+
import java.util.Collection;
15+
16+
/**
17+
* Allows adding extra ingredients (including ItemStack and FluidStack) for any registered ingredient type.
18+
*
19+
* This is intended to be used to add ingredients to another mod's type.
20+
* If you want to add ingredients to your own custom type,
21+
* pass them to {@link IModIngredientRegistration#register} instead.
22+
*
23+
* This is given to your {@link IModPlugin#registerExtraIngredients(IExtraIngredientRegistration)}.
24+
*
25+
* @since 19.18.0
26+
*/
27+
public interface IExtraIngredientRegistration {
28+
/**
29+
* Add extra ItemStacks that are not already in the creative menu.
30+
*
31+
* @param extraItemStacks A collection of extra ItemStacks to be displayed in the ingredient list.
32+
*
33+
* @since 19.18.0
34+
*/
35+
default void addExtraItemStacks(Collection<ItemStack> extraItemStacks) {
36+
addExtraIngredients(VanillaTypes.ITEM_STACK, extraItemStacks);
37+
}
38+
39+
/**
40+
* Add extra ingredients to an existing ingredient type.
41+
*
42+
* @param ingredientType The type of the ingredient.
43+
* This must already be registered with {@link IModIngredientRegistration#register} by another mod.
44+
* @param extraIngredients A collection of extra ingredients to be displayed in the ingredient list.
45+
*
46+
* @since 19.18.0
47+
*/
48+
<V> void addExtraIngredients(
49+
IIngredientType<V> ingredientType,
50+
Collection<V> extraIngredients
51+
);
52+
}

CommonApi/src/main/java/mezz/jei/api/runtime/IIngredientManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
1212
import mezz.jei.api.ingredients.ITypedIngredient;
1313
import mezz.jei.api.ingredients.subtypes.UidContext;
14+
import mezz.jei.api.registration.IExtraIngredientRegistration;
1415
import mezz.jei.api.registration.IIngredientAliasRegistration;
1516
import net.minecraft.world.item.ItemStack;
1617
import org.jetbrains.annotations.Unmodifiable;
@@ -86,6 +87,10 @@ default Collection<ItemStack> getAllItemStacks() {
8687
/**
8788
* Add new ingredients to JEI at runtime.
8889
* Used by mods that have items created while the game is running, or use the server to define items.
90+
*
91+
* If you just want to add ingredients to an existing type
92+
* (like adding more ItemStacks or FluidStacks, not at runtime),
93+
* use {@link IExtraIngredientRegistration#addExtraIngredients} instead.
8994
*/
9095
<V> void addIngredientsAtRuntime(IIngredientType<V> ingredientType, Collection<V> ingredients);
9196

Library/src/main/java/mezz/jei/library/load/PluginLoader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public PluginLoader(
8484

8585
IngredientManagerBuilder ingredientManagerBuilder = new IngredientManagerBuilder(subtypeManager, colorHelper);
8686
PluginCaller.callOnPlugins("Registering ingredients", plugins, p -> p.registerIngredients(ingredientManagerBuilder));
87+
PluginCaller.callOnPlugins("Registering extra ingredients", plugins, p -> p.registerExtraIngredients(ingredientManagerBuilder));
8788

8889
if (ingredientFilterConfig.getSearchIngredientAliases()) {
8990
PluginCaller.callOnPlugins("Registering search ingredient aliases", plugins, p -> p.registerIngredientAliases(ingredientManagerBuilder));

Library/src/main/java/mezz/jei/library/load/registration/IngredientManagerBuilder.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import mezz.jei.api.ingredients.IIngredientType;
99
import mezz.jei.api.ingredients.ITypedIngredient;
1010
import mezz.jei.api.ingredients.subtypes.ISubtypeManager;
11+
import mezz.jei.api.registration.IExtraIngredientRegistration;
1112
import mezz.jei.api.registration.IIngredientAliasRegistration;
1213
import mezz.jei.api.registration.IModIngredientRegistration;
1314
import mezz.jei.api.runtime.IIngredientManager;
@@ -20,7 +21,7 @@
2021
import java.util.LinkedHashMap;
2122
import java.util.SequencedMap;
2223

23-
public class IngredientManagerBuilder implements IModIngredientRegistration, IIngredientAliasRegistration {
24+
public class IngredientManagerBuilder implements IModIngredientRegistration, IIngredientAliasRegistration, IExtraIngredientRegistration {
2425
private final SequencedMap<IIngredientType<?>, IngredientInfo<?>> ingredientInfos = new LinkedHashMap<>();
2526
private final ISubtypeManager subtypeManager;
2627
private final IColorHelper colorHelper;
@@ -46,7 +47,7 @@ public <V> void register(IIngredientType<V> ingredientType, Collection<V> allIng
4647
"ingredients in the ingredient list, and it must have a height of 16"
4748
);
4849
if (ingredientInfos.containsKey(ingredientType)) {
49-
throw new IllegalArgumentException("Ingredient type has already been registered: " + ingredientType.getIngredientClass());
50+
throw new IllegalArgumentException("Ingredient type has already been registered: " + ingredientType.getUid());
5051
}
5152

5253
ingredientInfos.put(ingredientType, new IngredientInfo<>(ingredientType, allIngredients, ingredientHelper, ingredientRenderer, null));
@@ -81,6 +82,20 @@ public <V> void register(
8182
ingredientInfos.put(ingredientType, new IngredientInfo<>(ingredientType, allIngredients, ingredientHelper, ingredientRenderer, ingredientCodec));
8283
}
8384

85+
@Override
86+
public <V> void addExtraIngredients(IIngredientType<V> ingredientType, Collection<V> extraIngredients) {
87+
ErrorUtil.checkNotNull(ingredientType, "ingredientType");
88+
ErrorUtil.checkNotNull(extraIngredients, "extraIngredients");
89+
90+
IngredientInfo<?> ingredientInfo = ingredientInfos.get(ingredientType);
91+
if (ingredientInfo == null) {
92+
throw new IllegalArgumentException("Ingredient type has not been registered: " + ingredientType.getUid());
93+
}
94+
@SuppressWarnings("unchecked")
95+
IngredientInfo<V> castIngredientInfo = (IngredientInfo<V>) ingredientInfo;
96+
castIngredientInfo.addIngredients(extraIngredients);
97+
}
98+
8499
@Override
85100
public <I> void addAlias(IIngredientType<I> type, I ingredient, String alias) {
86101
ErrorUtil.checkNotNull(type, "type");

Library/src/main/java/mezz/jei/library/plugins/debug/JeiDebugPlugin.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
1414
import mezz.jei.api.ingredients.ITypedIngredient;
1515
import mezz.jei.api.registration.IAdvancedRegistration;
16+
import mezz.jei.api.registration.IExtraIngredientRegistration;
1617
import mezz.jei.api.registration.IGuiHandlerRegistration;
1718
import mezz.jei.api.registration.IIngredientAliasRegistration;
1819
import mezz.jei.api.registration.IModInfoRegistration;
@@ -90,6 +91,13 @@ public void registerIngredients(IModIngredientRegistration registration) {
9091
}
9192
}
9293

94+
@Override
95+
public void registerExtraIngredients(IExtraIngredientRegistration registration) {
96+
if (DebugConfig.isDebugModeEnabled()) {
97+
registration.addExtraIngredients(DebugIngredient.TYPE, DebugIngredientListFactory.create(0, 10));
98+
}
99+
}
100+
93101
@Override
94102
public void registerIngredientAliases(IIngredientAliasRegistration registration) {
95103
registration.addAlias(
@@ -357,7 +365,7 @@ public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
357365
debugRecipeCategory.setRuntime(jeiRuntime);
358366
}
359367
IIngredientManager ingredientManager = jeiRuntime.getIngredientManager();
360-
ingredientManager.addIngredientsAtRuntime(DebugIngredient.TYPE, DebugIngredientListFactory.create());
368+
ingredientManager.addIngredientsAtRuntime(DebugIngredient.TYPE, DebugIngredientListFactory.create(10, 20));
361369
}
362370
}
363371
}

Library/src/main/java/mezz/jei/library/plugins/debug/ingredients/DebugIngredientListFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public final class DebugIngredientListFactory {
88
private DebugIngredientListFactory() {
99
}
1010

11-
public static Collection<DebugIngredient> create() {
11+
public static Collection<DebugIngredient> create(int start, int end) {
1212
List<DebugIngredient> ingredients = new ArrayList<>();
13-
for (int i = 0; i < 10; i++) {
13+
for (int i = start; i < end; i++) {
1414
DebugIngredient debugIngredient = new DebugIngredient(i);
1515
ingredients.add(debugIngredient);
1616
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ modrinthId=u6dRKJwZ
7474
jUnitVersion=5.8.2
7575

7676
# Version
77-
specificationVersion=19.17.0
77+
specificationVersion=19.18.0

0 commit comments

Comments
 (0)