Skip to content

Commit

Permalink
Create a way to add extra ingredients in the API (not at runtime)
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 12, 2024
1 parent c4130f8 commit dc2fbd1
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 6 deletions.
11 changes: 11 additions & 0 deletions CommonApi/src/main/java/mezz/jei/api/IModPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.resources.ResourceLocation;

import mezz.jei.api.registration.IAdvancedRegistration;
import mezz.jei.api.registration.IExtraIngredientRegistration;
import mezz.jei.api.registration.IGuiHandlerRegistration;
import mezz.jei.api.registration.IModIngredientRegistration;
import mezz.jei.api.registration.IRecipeCatalystRegistration;
Expand Down Expand Up @@ -54,6 +55,16 @@ default void registerIngredients(IModIngredientRegistration registration) {

}

/**
* Register extra ItemStacks that are not in the creative menu,
* or FluidStacks that are different from the default ones available via the fluid registry.
*
* @since 19.18.0
*/
default void registerExtraIngredients(IExtraIngredientRegistration registration) {

}

/**
* Register search aliases for ingredients.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mezz.jei.api.registration;

import mezz.jei.api.IModPlugin;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.helpers.IColorHelper;
import mezz.jei.api.helpers.IPlatformFluidHelper;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.subtypes.ISubtypeManager;
import mezz.jei.api.runtime.IIngredientManager;
import net.minecraft.world.item.ItemStack;

import java.util.Collection;

/**
* Allows adding extra ingredients (including ItemStack and FluidStack) for any registered ingredient type.
*
* This is intended to be used to add ingredients to another mod's type.
* If you want to add ingredients to your own custom type,
* pass them to {@link IModIngredientRegistration#register} instead.
*
* This is given to your {@link IModPlugin#registerExtraIngredients(IExtraIngredientRegistration)}.
*
* @since 19.18.0
*/
public interface IExtraIngredientRegistration {
/**
* Add extra ItemStacks that are not already in the creative menu.
*
* @param extraItemStacks A collection of extra ItemStacks to be displayed in the ingredient list.
*
* @since 19.18.0
*/
default void addExtraItemStacks(Collection<ItemStack> extraItemStacks) {
addExtraIngredients(VanillaTypes.ITEM_STACK, extraItemStacks);
}

/**
* Add extra ingredients to an existing ingredient type.
*
* @param ingredientType The type of the ingredient.
* This must already be registered with {@link IModIngredientRegistration#register} by another mod.
* @param extraIngredients A collection of extra ingredients to be displayed in the ingredient list.
*
* @since 19.18.0
*/
<V> void addExtraIngredients(
IIngredientType<V> ingredientType,
Collection<V> extraIngredients
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.ingredients.subtypes.UidContext;
import mezz.jei.api.registration.IExtraIngredientRegistration;
import mezz.jei.api.registration.IIngredientAliasRegistration;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Unmodifiable;
Expand Down Expand Up @@ -86,6 +87,10 @@ default Collection<ItemStack> getAllItemStacks() {
/**
* Add new ingredients to JEI at runtime.
* Used by mods that have items created while the game is running, or use the server to define items.
*
* If you just want to add ingredients to an existing type
* (like adding more ItemStacks or FluidStacks, not at runtime),
* use {@link IExtraIngredientRegistration#addExtraIngredients} instead.
*/
<V> void addIngredientsAtRuntime(IIngredientType<V> ingredientType, Collection<V> ingredients);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public PluginLoader(

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

if (ingredientFilterConfig.getSearchIngredientAliases()) {
PluginCaller.callOnPlugins("Registering search ingredient aliases", plugins, p -> p.registerIngredientAliases(ingredientManagerBuilder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.ingredients.subtypes.ISubtypeManager;
import mezz.jei.api.registration.IExtraIngredientRegistration;
import mezz.jei.api.registration.IIngredientAliasRegistration;
import mezz.jei.api.registration.IModIngredientRegistration;
import mezz.jei.api.runtime.IIngredientManager;
Expand All @@ -20,7 +21,7 @@
import java.util.LinkedHashMap;
import java.util.SequencedMap;

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

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

@Override
public <V> void addExtraIngredients(IIngredientType<V> ingredientType, Collection<V> extraIngredients) {
ErrorUtil.checkNotNull(ingredientType, "ingredientType");
ErrorUtil.checkNotNull(extraIngredients, "extraIngredients");

IngredientInfo<?> ingredientInfo = ingredientInfos.get(ingredientType);
if (ingredientInfo == null) {
throw new IllegalArgumentException("Ingredient type has not been registered: " + ingredientType.getUid());
}
@SuppressWarnings("unchecked")
IngredientInfo<V> castIngredientInfo = (IngredientInfo<V>) ingredientInfo;
castIngredientInfo.addIngredients(extraIngredients);
}

@Override
public <I> void addAlias(IIngredientType<I> type, I ingredient, String alias) {
ErrorUtil.checkNotNull(type, "type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.registration.IAdvancedRegistration;
import mezz.jei.api.registration.IExtraIngredientRegistration;
import mezz.jei.api.registration.IGuiHandlerRegistration;
import mezz.jei.api.registration.IIngredientAliasRegistration;
import mezz.jei.api.registration.IModInfoRegistration;
Expand Down Expand Up @@ -90,6 +91,13 @@ public void registerIngredients(IModIngredientRegistration registration) {
}
}

@Override
public void registerExtraIngredients(IExtraIngredientRegistration registration) {
if (DebugConfig.isDebugModeEnabled()) {
registration.addExtraIngredients(DebugIngredient.TYPE, DebugIngredientListFactory.create(0, 10));
}
}

@Override
public void registerIngredientAliases(IIngredientAliasRegistration registration) {
registration.addAlias(
Expand Down Expand Up @@ -357,7 +365,7 @@ public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
debugRecipeCategory.setRuntime(jeiRuntime);
}
IIngredientManager ingredientManager = jeiRuntime.getIngredientManager();
ingredientManager.addIngredientsAtRuntime(DebugIngredient.TYPE, DebugIngredientListFactory.create());
ingredientManager.addIngredientsAtRuntime(DebugIngredient.TYPE, DebugIngredientListFactory.create(10, 20));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public final class DebugIngredientListFactory {
private DebugIngredientListFactory() {
}

public static Collection<DebugIngredient> create() {
public static Collection<DebugIngredient> create(int start, int end) {
List<DebugIngredient> ingredients = new ArrayList<>();
for (int i = 0; i < 10; i++) {
for (int i = start; i < end; i++) {
DebugIngredient debugIngredient = new DebugIngredient(i);
ingredients.add(debugIngredient);
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ modrinthId=u6dRKJwZ
jUnitVersion=5.8.2

# Version
specificationVersion=19.17.0
specificationVersion=19.18.0

0 comments on commit dc2fbd1

Please sign in to comment.