Skip to content

Commit

Permalink
add IJeiHelpers#getRecipeType to help addon mods
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Oct 25, 2022
1 parent 9a8f8dd commit 3809667
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 8 deletions.
Expand Up @@ -163,7 +163,7 @@ public RecipeManager createRecipeManager(
) {
List<IRecipeCategory<?>> recipeCategories = createRecipeCategories(plugins, vanillaPlugin);

RecipeCatalystRegistration recipeCatalystRegistration = new RecipeCatalystRegistration(registeredIngredients, ingredientManager);
RecipeCatalystRegistration recipeCatalystRegistration = new RecipeCatalystRegistration(registeredIngredients, ingredientManager, jeiHelpers);
PluginCaller.callOnPlugins("Registering recipe catalysts", plugins, p -> p.registerRecipeCatalysts(recipeCatalystRegistration));
ImmutableListMultimap<ResourceLocation, ITypedIngredient<?>> recipeCatalysts = recipeCatalystRegistration.getRecipeCatalysts();

Expand Down
@@ -1,5 +1,6 @@
package mezz.jei.common.load.registration;

import mezz.jei.api.helpers.IJeiHelpers;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.recipe.RecipeType;
Expand All @@ -17,17 +18,24 @@ public class RecipeCatalystRegistration implements IRecipeCatalystRegistration {
private final ListMultiMap<ResourceLocation, ITypedIngredient<?>> recipeCatalysts = new ListMultiMap<>();
private final RegisteredIngredients registeredIngredients;
private final IIngredientManager ingredientManager;
private final IJeiHelpers jeiHelpers;

public RecipeCatalystRegistration(RegisteredIngredients registeredIngredients, IIngredientManager ingredientManager) {
public RecipeCatalystRegistration(RegisteredIngredients registeredIngredients, IIngredientManager ingredientManager, IJeiHelpers jeiHelpers) {
this.registeredIngredients = registeredIngredients;
this.ingredientManager = ingredientManager;
this.jeiHelpers = jeiHelpers;
}

@Override
public IIngredientManager getIngredientManager() {
return ingredientManager;
}

@Override
public IJeiHelpers getJeiHelpers() {
return jeiHelpers;
}

@Override
public <T> void addRecipeCatalyst(IIngredientType<T> ingredientType, T ingredient, RecipeType<?>... recipeTypes) {
ErrorUtil.checkNotEmpty(recipeTypes, "recipeTypes");
Expand Down
Expand Up @@ -5,6 +5,7 @@
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.api.registration.IRecipeCategoryRegistration;
import mezz.jei.common.runtime.JeiHelpers;
import mezz.jei.common.util.ErrorUtil;
import org.jetbrains.annotations.Unmodifiable;

Expand All @@ -17,9 +18,9 @@
public class RecipeCategoryRegistration implements IRecipeCategoryRegistration {
private final List<IRecipeCategory<?>> recipeCategories = new ArrayList<>();
private final Set<RecipeType<?>> recipeTypes = new HashSet<>();
private final IJeiHelpers jeiHelpers;
private final JeiHelpers jeiHelpers;

public RecipeCategoryRegistration(IJeiHelpers jeiHelpers) {
public RecipeCategoryRegistration(JeiHelpers jeiHelpers) {
this.jeiHelpers = jeiHelpers;
}

Expand All @@ -38,6 +39,7 @@ public void addRecipeCategories(IRecipeCategory<?>... recipeCategories) {
}

Collections.addAll(this.recipeCategories, recipeCategories);
this.jeiHelpers.setRecipeCategories(Collections.unmodifiableCollection(this.recipeCategories));
}

@Override
Expand Down
24 changes: 23 additions & 1 deletion Common/src/main/java/mezz/jei/common/runtime/JeiHelpers.java
Expand Up @@ -6,16 +6,24 @@
import mezz.jei.api.helpers.IPlatformFluidHelper;
import mezz.jei.api.helpers.IStackHelper;
import mezz.jei.api.recipe.IFocusFactory;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.common.gui.GuiHelper;
import mezz.jei.common.platform.Services;
import net.minecraft.resources.ResourceLocation;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

public class JeiHelpers implements IJeiHelpers {
private final GuiHelper guiHelper;
private final IStackHelper stackHelper;
private final IModIdHelper modIdHelper;
private final IFocusFactory focusFactory;

private final IPlatformFluidHelper<?> platformFluidHelper;
private @Nullable Collection<IRecipeCategory<?>> recipeCategories;

public JeiHelpers(
GuiHelper guiHelper,
Expand All @@ -30,6 +38,10 @@ public JeiHelpers(
this.platformFluidHelper = Services.PLATFORM.getFluidHelper();
}

public void setRecipeCategories(Collection<IRecipeCategory<?>> recipeCategories) {
this.recipeCategories = Collections.unmodifiableCollection(recipeCategories);
}

@Override
public IGuiHelper getGuiHelper() {
return guiHelper;
Expand All @@ -54,4 +66,14 @@ public IFocusFactory getFocusFactory() {
public IPlatformFluidHelper<?> getPlatformFluidHelper() {
return platformFluidHelper;
}

@Override
public Optional<RecipeType<?>> getRecipeType(ResourceLocation uid) {
return Optional.ofNullable(this.recipeCategories)
.flatMap(r -> r.stream()
.map(IRecipeCategory::getRecipeType)
.filter(t -> t.getUid().equals(uid))
.findFirst()
);
}
}
17 changes: 16 additions & 1 deletion CommonApi/src/main/java/mezz/jei/api/helpers/IJeiHelpers.java
Expand Up @@ -2,10 +2,14 @@

import mezz.jei.api.IModPlugin;
import mezz.jei.api.recipe.IFocusFactory;
import mezz.jei.api.recipe.RecipeType;
import net.minecraft.resources.ResourceLocation;

import java.util.Optional;

/**
* {@link IJeiHelpers} provides helpers and tools for addon mods.
*
* <p>
* An instance is passed to your {@link IModPlugin}'s registration methods.
*/
public interface IJeiHelpers {
Expand Down Expand Up @@ -37,4 +41,15 @@ public interface IJeiHelpers {
* @since 10.1.0
*/
IPlatformFluidHelper<?> getPlatformFluidHelper();

/**
* Get the registered recipe type for the given unique id.
* <p>
* This is useful for integrating with other mods that do not share their
* recipe types directly from their API.
*
* @see RecipeType#getUid()
* @since 11.4.0
*/
Optional<RecipeType<?>> getRecipeType(ResourceLocation uid);
}
Expand Up @@ -117,7 +117,7 @@ public interface IRecipeManager {

/**
* Get the registered recipe type for the given unique id.
*
* <p>
* This is useful for integrating with other mods that do not share their
* recipe types directly from their API.
*
Expand Down
@@ -1,6 +1,7 @@
package mezz.jei.api.registration;

import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.helpers.IJeiHelpers;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.runtime.IIngredientManager;
Expand All @@ -13,6 +14,12 @@ public interface IRecipeCatalystRegistration {
*/
IIngredientManager getIngredientManager();

/**
* {@link IJeiHelpers} provides helpers and tools for addon mods.
* @since 11.4.0
*/
IJeiHelpers getJeiHelpers();

/**
* Add an association between an {@link ItemStack} and what it can craft.
* (i.e. Furnace ItemStack can craft Smelting and Fuel Recipes)
Expand Down
Expand Up @@ -8,6 +8,9 @@
* This is given to your {@link IModPlugin#registerCategories(IRecipeCategoryRegistration)}.
*/
public interface IRecipeCategoryRegistration {
/**
* {@link IJeiHelpers} provides helpers and tools for addon mods.
*/
IJeiHelpers getJeiHelpers();

/**
Expand Down
Expand Up @@ -16,6 +16,9 @@
* Get the instance passed in to your plugin's {@link IModPlugin#registerRecipeTransferHandlers}.
*/
public interface IRecipeTransferRegistration {
/**
* {@link IJeiHelpers} provides helpers and tools for addon mods.
*/
IJeiHelpers getJeiHelpers();

IRecipeTransferHandlerHelper getTransferHelper();
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -38,7 +38,7 @@ curseHomepageUrl=https://www.curseforge.com/minecraft/mc-mods/jei
jUnitVersion=5.8.2

# Version
specificationVersion=11.3.0
specificationVersion=11.4.0

# Workaround for Spotless bug
# https://github.com/diffplug/spotless/issues/834
Expand Down

0 comments on commit 3809667

Please sign in to comment.