Skip to content

Commit

Permalink
make IIngredientVisibility available earlier, in IJeiHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 18, 2024
1 parent 287dcd9 commit 9e68684
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 128 deletions.
9 changes: 9 additions & 0 deletions CommonApi/src/main/java/mezz/jei/api/helpers/IJeiHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.vanilla.IVanillaRecipeFactory;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.api.runtime.IIngredientVisibility;
import net.minecraft.resources.ResourceLocation;

import java.util.Optional;
Expand Down Expand Up @@ -102,4 +103,12 @@ public interface IJeiHelpers {
* @since 19.15.0
*/
IVanillaRecipeFactory getVanillaRecipeFactory();

/**
* The {@link IIngredientVisibility} allows mod plugins to do advanced filtering of
* ingredients based on what is visible in JEI.
*
* @since 19.18.4
*/
IIngredientVisibility getIngredientVisibility();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ public interface IRecipeRegistration {
* ingredients based on what is visible in JEI.
*
* @since 9.3.1
* @deprecated use {@link IJeiHelpers#getIngredientVisibility()}
*/
IIngredientVisibility getIngredientVisibility();
@Deprecated(since = "19.18.4", forRemoval = true)
default IIngredientVisibility getIngredientVisibility() {
return getJeiHelpers().getIngredientVisibility();
}

/**
* Add the recipes provided by your plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ public interface IRuntimeRegistration {
/**
* The {@link IIngredientVisibility} allows mod plugins to do advanced filtering of
* ingredients based on what is visible in JEI.
* @deprecated use {@link IJeiHelpers#getIngredientVisibility()}
*/
IIngredientVisibility getIngredientVisibility();
@Deprecated(since = "19.18.4", forRemoval = true)
default IIngredientVisibility getIngredientVisibility() {
return getJeiHelpers().getIngredientVisibility();
}

/**
* Get a helper for all runtime Screen functions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package mezz.jei.api.runtime;

import mezz.jei.api.IModPlugin;
import mezz.jei.api.helpers.IJeiHelpers;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.registration.IRecipeRegistration;
import org.jetbrains.annotations.ApiStatus;

/**
* The {@link IIngredientVisibility} allows mod plugins to do advanced filtering of
* ingredients based on what is visible in JEI.
*
* An instance available during {@link IModPlugin#registerRecipes}
* from {@link IRecipeRegistration#getIngredientVisibility()}
* and it is accessible at runtime from
* {@link IJeiRuntime#getIngredientVisibility()}.
* An instance available from {@link IJeiHelpers#getIngredientVisibility()}.
*
* @since JEI 9.3.0
*/
@ApiStatus.NonExtendable
public interface IIngredientVisibility {
/**
* Returns true if the given ingredient is visible in JEI's ingredient list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ public interface IJeiRuntime {
* ingredients based on what is visible in JEI.
*
* @since 9.3.0
* @deprecated use {@link IJeiHelpers#getIngredientVisibility()}
*/
IIngredientVisibility getIngredientVisibility();
@Deprecated(since = "19.18.4", forRemoval = true)
default IIngredientVisibility getIngredientVisibility() {
return getJeiHelpers().getIngredientVisibility();
}

/**
* The {@link IJeiKeyMappings} gives access to key mappings used by JEI.
Expand Down
2 changes: 1 addition & 1 deletion Gui/src/main/java/mezz/jei/gui/startup/JeiGuiStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public static JeiEventHandlers start(IRuntimeRegistration registration) {
IScreenHelper screenHelper = registration.getScreenHelper();
IRecipeTransferManager recipeTransferManager = registration.getRecipeTransferManager();
IRecipeManager recipeManager = registration.getRecipeManager();
IIngredientVisibility ingredientVisibility = registration.getIngredientVisibility();
IIngredientManager ingredientManager = registration.getIngredientManager();
IEditModeConfig editModeConfig = registration.getEditModeConfig();

IJeiHelpers jeiHelpers = registration.getJeiHelpers();
IIngredientVisibility ingredientVisibility = jeiHelpers.getIngredientVisibility();
IColorHelper colorHelper = jeiHelpers.getColorHelper();
IModIdHelper modIdHelper = jeiHelpers.getModIdHelper();
IFocusFactory focusFactory = jeiHelpers.getFocusFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static List<Optional<ITypedIngredient<?>>> calculateDisplayIngredients(L
// hide invisible ingredients if there are any
// try scanning through all the ingredients without building the list of visible ingredients.
// if an invisible ingredient is found, start building the list of visible ingredients
IIngredientVisibility ingredientVisibility = Internal.getJeiRuntime().getIngredientVisibility();
IIngredientVisibility ingredientVisibility = Internal.getJeiRuntime().getJeiHelpers().getIngredientVisibility();
for (int i = 0; i < allIngredients.size() && visibleIngredients.size() < MAX_DISPLAYED_INGREDIENTS; i++) {
Optional<ITypedIngredient<?>> ingredient = allIngredients.get(i);
boolean visible = ingredient.isEmpty() || ingredientVisibility.isIngredientVisible(ingredient.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public <V> boolean isIngredientVisible(ITypedIngredient<V> typedIngredient) {
@Override
public <V> boolean isIngredientVisible(IIngredientType<V> ingredientType, V ingredient) {
IIngredientHelper<V> ingredientHelper = ingredientManager.getIngredientHelper(ingredientType);
return TypedIngredient.createAndFilterInvalid(ingredientManager, ingredientType, ingredient, false)
return TypedIngredient.createAndFilterInvalid(ingredientHelper, ingredientType, ingredient, false)
.map(i -> isIngredientVisible(i, ingredientHelper))
.orElse(false);
}
Expand Down
115 changes: 67 additions & 48 deletions Library/src/main/java/mezz/jei/library/load/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.ImmutableTable;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.helpers.IColorHelper;
import mezz.jei.api.helpers.IJeiHelpers;
Expand All @@ -13,23 +12,29 @@
import mezz.jei.api.recipe.advanced.IRecipeManagerPlugin;
import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.api.recipe.category.extensions.IRecipeCategoryDecorator;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandler;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandlerHelper;
import mezz.jei.api.recipe.transfer.IRecipeTransferManager;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.api.runtime.IIngredientVisibility;
import mezz.jei.api.runtime.IJeiFeatures;
import mezz.jei.api.runtime.IScreenHelper;
import mezz.jei.common.Internal;
import mezz.jei.common.config.IClientToggleState;
import mezz.jei.common.config.IIngredientFilterConfig;
import mezz.jei.common.network.IConnectionToServer;
import mezz.jei.common.platform.IPlatformFluidHelperInternal;
import mezz.jei.common.platform.Services;
import mezz.jei.common.util.StackHelper;
import mezz.jei.core.util.LoggedTimer;
import mezz.jei.library.config.EditModeConfig;
import mezz.jei.library.config.IModIdFormatConfig;
import mezz.jei.library.config.RecipeCategorySortingConfig;
import mezz.jei.library.focus.FocusFactory;
import mezz.jei.library.gui.helpers.GuiHelper;
import mezz.jei.library.helpers.CodecHelper;
import mezz.jei.library.helpers.ModIdHelper;
import mezz.jei.library.ingredients.IngredientBlacklistInternal;
import mezz.jei.library.ingredients.IngredientVisibility;
import mezz.jei.library.ingredients.subtypes.SubtypeInterpreters;
import mezz.jei.library.ingredients.subtypes.SubtypeManager;
import mezz.jei.library.load.registration.AdvancedRegistration;
Expand All @@ -52,26 +57,14 @@
import mezz.jei.library.runtime.JeiHelpers;
import mezz.jei.library.startup.StartData;
import mezz.jei.library.transfer.RecipeTransferHandlerHelper;
import net.minecraft.world.inventory.AbstractContainerMenu;
import org.jetbrains.annotations.Unmodifiable;

import java.util.List;

public class PluginLoader {
private final StartData data;
private final LoggedTimer timer;
private final IIngredientManager ingredientManager;
private final JeiHelpers jeiHelpers;

public PluginLoader(
StartData data,
IModIdFormatConfig modIdFormatConfig,
IIngredientFilterConfig ingredientFilterConfig,
IColorHelper colorHelper
) {
this.data = data;
this.timer = new LoggedTimer();
public final class PluginLoader {
private PluginLoader() {}

public static SubtypeManager registerSubtypes(StartData data) {
IPlatformFluidHelperInternal<?> fluidHelper = Services.PLATFORM.getFluidHelper();
List<IModPlugin> plugins = data.plugins();
SubtypeRegistration subtypeRegistration = new SubtypeRegistration();
Expand All @@ -80,38 +73,65 @@ public PluginLoader(
p.registerFluidSubtypes(subtypeRegistration, fluidHelper)
);
SubtypeInterpreters subtypeInterpreters = subtypeRegistration.getInterpreters();
SubtypeManager subtypeManager = new SubtypeManager(subtypeInterpreters);
return new SubtypeManager(subtypeInterpreters);
}

public static IIngredientManager registerIngredients(StartData data, SubtypeManager subtypeManager, IColorHelper colorHelper, IIngredientFilterConfig ingredientFilterConfig) {
List<IModPlugin> plugins = data.plugins();
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));
}
return ingredientManagerBuilder.build();
}

this.ingredientManager = ingredientManagerBuilder.build();

ImmutableSetMultimap<String, String> modAliases;
if (ingredientFilterConfig.getSearchModAliases()) {
ModInfoRegistration modInfoRegistration = new ModInfoRegistration();
PluginCaller.callOnPlugins("Registering Mod Info", plugins, p -> p.registerModInfo(modInfoRegistration));
modAliases = modInfoRegistration.getModAliases();
} else {
modAliases = ImmutableSetMultimap.of();
public static ImmutableSetMultimap<String, String> registerModAliases(
StartData data,
IIngredientFilterConfig ingredientFilterConfig
) {
List<IModPlugin> plugins = data.plugins();
if (!ingredientFilterConfig.getSearchModAliases()) {
return ImmutableSetMultimap.of();
}
ModInfoRegistration modInfoRegistration = new ModInfoRegistration();
PluginCaller.callOnPlugins("Registering Mod Info", plugins, p -> p.registerModInfo(modInfoRegistration));
return modInfoRegistration.getModAliases();
}

public static JeiHelpers createJeiHelpers(
ImmutableSetMultimap<String, String> modAliases,
IModIdFormatConfig modIdFormatConfig,
IColorHelper colorHelper,
EditModeConfig editModeConfig,
FocusFactory focusFactory,
CodecHelper codecHelper,
IIngredientManager ingredientManager,
SubtypeManager subtypeManager
) {
VanillaRecipeFactory vanillaRecipeFactory = new VanillaRecipeFactory(ingredientManager);

StackHelper stackHelper = new StackHelper(subtypeManager);
GuiHelper guiHelper = new GuiHelper(ingredientManager);
FocusFactory focusFactory = new FocusFactory(ingredientManager);
IModIdHelper modIdHelper = new ModIdHelper(modIdFormatConfig, ingredientManager, modAliases);
this.jeiHelpers = new JeiHelpers(guiHelper, stackHelper, modIdHelper, focusFactory, colorHelper, ingredientManager, vanillaRecipeFactory);

IClientToggleState toggleState = Internal.getClientToggleState();
IngredientBlacklistInternal blacklist = new IngredientBlacklistInternal();
ingredientManager.registerIngredientListener(blacklist);

IIngredientVisibility ingredientVisibility = new IngredientVisibility(
blacklist,
toggleState,
editModeConfig,
ingredientManager
);

return new JeiHelpers(guiHelper, stackHelper, modIdHelper, focusFactory, colorHelper, ingredientManager, vanillaRecipeFactory, codecHelper, ingredientVisibility);
}

@Unmodifiable
private List<IRecipeCategory<?>> createRecipeCategories(List<IModPlugin> plugins, VanillaPlugin vanillaPlugin) {
private static List<IRecipeCategory<?>> createRecipeCategories(List<IModPlugin> plugins, VanillaPlugin vanillaPlugin, JeiHelpers jeiHelpers) {
RecipeCategoryRegistration recipeCategoryRegistration = new RecipeCategoryRegistration(jeiHelpers);
PluginCaller.callOnPlugins("Registering categories", plugins, p -> p.registerCategories(recipeCategoryRegistration));
CraftingRecipeCategory craftingCategory = vanillaPlugin.getCraftingCategory()
Expand All @@ -123,41 +143,48 @@ private List<IRecipeCategory<?>> createRecipeCategories(List<IModPlugin> plugins
return recipeCategoryRegistration.getRecipeCategories();
}

public IScreenHelper createGuiScreenHelper(List<IModPlugin> plugins, IJeiHelpers jeiHelpers) {
public static IScreenHelper createGuiScreenHelper(List<IModPlugin> plugins, IJeiHelpers jeiHelpers, IIngredientManager ingredientManager) {
GuiHandlerRegistration guiHandlerRegistration = new GuiHandlerRegistration(jeiHelpers);
PluginCaller.callOnPlugins("Registering gui handlers", plugins, p -> p.registerGuiHandlers(guiHandlerRegistration));
return guiHandlerRegistration.createGuiScreenHelper(ingredientManager);
}

public ImmutableTable<Class<? extends AbstractContainerMenu>, RecipeType<?>, IRecipeTransferHandler<?, ?>> createRecipeTransferHandlers(VanillaPlugin vanillaPlugin, List<IModPlugin> plugins) {
public static IRecipeTransferManager createRecipeTransferManager(
VanillaPlugin vanillaPlugin,
List<IModPlugin> plugins,
JeiHelpers jeiHelpers,
IConnectionToServer connectionToServer
) {
IStackHelper stackHelper = jeiHelpers.getStackHelper();
CraftingRecipeCategory craftingCategory = vanillaPlugin.getCraftingCategory()
.orElseThrow(() -> new NullPointerException("vanilla crafting category"));
IRecipeTransferHandlerHelper handlerHelper = new RecipeTransferHandlerHelper(stackHelper, craftingCategory);
RecipeTransferRegistration recipeTransferRegistration = new RecipeTransferRegistration(stackHelper, handlerHelper, this.jeiHelpers, data.serverConnection());
RecipeTransferRegistration recipeTransferRegistration = new RecipeTransferRegistration(stackHelper, handlerHelper, jeiHelpers, connectionToServer);
PluginCaller.callOnPlugins("Registering recipes transfer handlers", plugins, p -> p.registerRecipeTransferHandlers(recipeTransferRegistration));
return recipeTransferRegistration.getRecipeTransferHandlers();
return recipeTransferRegistration.createRecipeTransferManager();
}

public RecipeManager createRecipeManager(
public static RecipeManager createRecipeManager(
List<IModPlugin> plugins,
VanillaPlugin vanillaPlugin,
RecipeCategorySortingConfig recipeCategorySortingConfig,
IIngredientVisibility ingredientVisibility
JeiHelpers jeiHelpers,
IIngredientManager ingredientManager
) {
List<IRecipeCategory<?>> recipeCategories = createRecipeCategories(plugins, vanillaPlugin);
List<IRecipeCategory<?>> recipeCategories = createRecipeCategories(plugins, vanillaPlugin, jeiHelpers);

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

LoggedTimer timer = new LoggedTimer();
timer.start("Building recipe registry");
RecipeManagerInternal recipeManagerInternal = new RecipeManagerInternal(
recipeCategories,
recipeCatalysts,
ingredientManager,
recipeCategorySortingConfig,
ingredientVisibility
jeiHelpers.getIngredientVisibility()
);
timer.stop();

Expand All @@ -171,19 +198,11 @@ public RecipeManager createRecipeManager(
recipeManagerInternal.addPlugins(recipeManagerPlugins);
recipeManagerInternal.addDecorators(recipeCategoryDecorators);

RecipeRegistration recipeRegistration = new RecipeRegistration(jeiHelpers, ingredientManager, ingredientVisibility, recipeManagerInternal);
RecipeRegistration recipeRegistration = new RecipeRegistration(jeiHelpers, ingredientManager, recipeManagerInternal);
PluginCaller.callOnPlugins("Registering recipes", plugins, p -> p.registerRecipes(recipeRegistration));

recipeManagerInternal.compact();

return new RecipeManager(recipeManagerInternal, ingredientManager);
}

public IIngredientManager getIngredientManager() {
return ingredientManager;
}

public JeiHelpers getJeiHelpers() {
return jeiHelpers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import mezz.jei.api.recipe.vanilla.IVanillaRecipeFactory;
import mezz.jei.api.registration.IRecipeRegistration;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.api.runtime.IIngredientVisibility;
import mezz.jei.common.util.ErrorUtil;
import mezz.jei.library.plugins.jei.info.IngredientInfoRecipe;
import mezz.jei.library.recipes.RecipeManagerInternal;
Expand All @@ -19,18 +18,15 @@
public class RecipeRegistration implements IRecipeRegistration {
private final IJeiHelpers jeiHelpers;
private final IIngredientManager ingredientManager;
private final IIngredientVisibility ingredientVisibility;
private final RecipeManagerInternal recipeManager;

public RecipeRegistration(
IJeiHelpers jeiHelpers,
IIngredientManager ingredientManager,
IIngredientVisibility ingredientVisibility,
RecipeManagerInternal recipeManager
) {
this.jeiHelpers = jeiHelpers;
this.ingredientManager = ingredientManager;
this.ingredientVisibility = ingredientVisibility;
this.recipeManager = recipeManager;
}

Expand All @@ -49,11 +45,6 @@ public IVanillaRecipeFactory getVanillaRecipeFactory() {
return jeiHelpers.getVanillaRecipeFactory();
}

@Override
public IIngredientVisibility getIngredientVisibility() {
return ingredientVisibility;
}

@Override
public <T> void addRecipes(RecipeType<T> recipeType, List<T> recipes) {
ErrorUtil.checkNotNull(recipeType, "recipeType");
Expand Down
Loading

0 comments on commit 9e68684

Please sign in to comment.