Skip to content

Commit

Permalink
Hide recipe categories when all their crafting catalysts are hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Apr 19, 2018
1 parent cb8ea93 commit 156f46e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mezz/jei/ingredients/IngredientFilter.java
Expand Up @@ -158,7 +158,7 @@ private <V> void removeIngredientAtRuntime(IIngredientListElement<V> element) {
}
}

private <V> List<IIngredientListElement> findMatchingElements(IIngredientListElement<V> element) {
public <V> List<IIngredientListElement> findMatchingElements(IIngredientListElement<V> element) {
final IIngredientHelper<V> ingredientHelper = element.getIngredientHelper();
final V ingredient = element.getIngredient();
final String ingredientUid = ingredientHelper.getUniqueId(ingredient);
Expand Down
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.util.NonNullList;
import net.minecraftforge.fml.common.ProgressManager;

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

public final class IngredientListElementFactory {
Expand All @@ -25,7 +26,7 @@ public static NonNullList<IIngredientListElement> createBaseList(IIngredientRegi
return ingredientListElements;
}

public static <V> NonNullList<IIngredientListElement> createList(IIngredientRegistry ingredientRegistry, Class<V> ingredientClass, Collection<V> ingredients, IModIdHelper modIdHelper) {
public static <V> NonNullList<IIngredientListElement> createList(IIngredientRegistry ingredientRegistry, Class<? extends V> ingredientClass, Collection<V> ingredients, IModIdHelper modIdHelper) {
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredientClass);
IIngredientRenderer<V> ingredientRenderer = ingredientRegistry.getIngredientRenderer(ingredientClass);

Expand All @@ -41,6 +42,13 @@ public static <V> NonNullList<IIngredientListElement> createList(IIngredientRegi
return list;
}

@Nullable
public static <V> IIngredientListElement<V> createElement(IIngredientRegistry ingredientRegistry, Class<? extends V> ingredientClass, V ingredient, IModIdHelper modIdHelper) {
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredientClass);
IIngredientRenderer<V> ingredientRenderer = ingredientRegistry.getIngredientRenderer(ingredientClass);
return IngredientListElement.create(ingredient, ingredientHelper, ingredientRenderer, modIdHelper);
}

private static <V> void addToBaseList(NonNullList<IIngredientListElement> baseList, IIngredientRegistry ingredientRegistry, Class<V> ingredientClass, IModIdHelper modIdHelper) {
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredientClass);
IIngredientRenderer<V> ingredientRenderer = ingredientRegistry.getIngredientRenderer(ingredientClass);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/mezz/jei/ingredients/IngredientRegistry.java
Expand Up @@ -218,4 +218,23 @@ public <V> void removeIngredientsAtRuntime(Class<V> ingredientClass, Collection<
NonNullList<IIngredientListElement> ingredientListElements = IngredientListElementFactory.createList(this, ingredientClass, ingredients, modIdHelper);
ingredientFilter.removeIngredientsAtRuntime(ingredientListElements);
}

public <V> boolean isIngredientInvisible(V ingredient, IngredientFilter ingredientFilter) {
@SuppressWarnings("unchecked")
Class<? extends V> ingredientClass = (Class<? extends V>) ingredient.getClass();
IIngredientListElement<V> element = IngredientListElementFactory.createElement(this, ingredientClass, ingredient, modIdHelper);
if (element == null) {
return true;
}
List<IIngredientListElement> matchingElements = ingredientFilter.findMatchingElements(element);
if (matchingElements.isEmpty()) {
return false;
}
for (IIngredientListElement matchingElement : matchingElements) {
if (matchingElement.isVisible()) {
return false;
}
}
return true;
}
}
27 changes: 23 additions & 4 deletions src/main/java/mezz/jei/recipes/RecipeRegistry.java
Expand Up @@ -26,6 +26,7 @@
import mezz.jei.gui.Focus;
import mezz.jei.gui.recipes.RecipeClickableArea;
import mezz.jei.gui.recipes.RecipeLayout;
import mezz.jei.ingredients.IngredientRegistry;
import mezz.jei.ingredients.Ingredients;
import mezz.jei.plugins.vanilla.furnace.SmeltingRecipe;
import mezz.jei.util.ErrorUtil;
Expand Down Expand Up @@ -53,7 +54,7 @@
import java.util.Set;

public class RecipeRegistry implements IRecipeRegistry {
private final IIngredientRegistry ingredientRegistry;
private final IngredientRegistry ingredientRegistry;
@Deprecated
private final ImmutableList<IRecipeHandler> unsortedRecipeHandlers;
private final ImmutableMultimap<String, IRecipeHandler> recipeHandlers;
Expand All @@ -79,7 +80,7 @@ public RecipeRegistry(
ListMultiMap<String, Object> recipes,
ListMultiMap<Class<? extends GuiContainer>, RecipeClickableArea> recipeClickableAreasMap,
ListMultiMap<String, Object> recipeCatalysts,
IIngredientRegistry ingredientRegistry,
IngredientRegistry ingredientRegistry,
List<IRecipeRegistryPlugin> plugins
) {
this.ingredientRegistry = ingredientRegistry;
Expand Down Expand Up @@ -354,14 +355,25 @@ public IRecipeWrapper createBrewingRecipe(List<ItemStack> ingredients, ItemStack
public List<IRecipeCategory> getRecipeCategories() {
if (recipeCategoriesVisibleCache.isEmpty()) {
for (IRecipeCategory<?> recipeCategory : this.recipeCategories) {
if (!getRecipeWrappers(recipeCategory).isEmpty()) {
if (isCategoryVisible(recipeCategory)) {
recipeCategoriesVisibleCache.add(recipeCategory);
}
}
}
return recipeCategoriesVisibleCache;
}

private boolean isCategoryVisible(IRecipeCategory<?> recipeCategory) {
ImmutableList<Object> catalysts = recipeCatalysts.get(recipeCategory);
if (!catalysts.isEmpty()) {
List<Object> visibleCatalysts = getRecipeCatalysts(recipeCategory);
if (visibleCatalysts.isEmpty()) {
return false;
}
}
return !getRecipeWrappers(recipeCategory).isEmpty();
}

@Override
public ImmutableList<IRecipeCategory> getRecipeCategories(List<String> recipeCategoryUids) {
ErrorUtil.checkNotNull(recipeCategoryUids, "recipeCategoryUids");
Expand Down Expand Up @@ -663,7 +675,14 @@ public List<ItemStack> getCraftingItems(IRecipeCategory recipeCategory) {
@Override
public List<Object> getRecipeCatalysts(IRecipeCategory recipeCategory) {
ErrorUtil.checkNotNull(recipeCategory, "recipeCategory");
return recipeCatalysts.get(recipeCategory);
List<Object> visibleCatalysts = new ArrayList<>();
ImmutableList<Object> catalysts = recipeCatalysts.get(recipeCategory);
for (Object catalyst : catalysts) {
if (!ingredientRegistry.isIngredientInvisible(catalyst, Internal.getIngredientFilter())) {
visibleCatalysts.add(catalyst);
}
}
return visibleCatalysts;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mezz/jei/startup/ModRegistry.java
Expand Up @@ -22,6 +22,7 @@
import mezz.jei.collect.ListMultiMap;
import mezz.jei.collect.SetMultiMap;
import mezz.jei.gui.recipes.RecipeClickableArea;
import mezz.jei.ingredients.IngredientRegistry;
import mezz.jei.plugins.jei.info.IngredientInfoRecipe;
import mezz.jei.plugins.vanilla.anvil.AnvilRecipeWrapper;
import mezz.jei.recipes.RecipeRegistry;
Expand Down Expand Up @@ -297,7 +298,7 @@ public Map<Class, IGhostIngredientHandler> getGhostIngredientHandlers() {
return ghostIngredientHandlers;
}

public RecipeRegistry createRecipeRegistry(IIngredientRegistry ingredientRegistry) {
public RecipeRegistry createRecipeRegistry(IngredientRegistry ingredientRegistry) {
ImmutableTable<Class, String, IRecipeTransferHandler> recipeTransferHandlers = recipeTransferRegistry.getRecipeTransferHandlers();
return new RecipeRegistry(recipeCategories, unsortedRecipeHandlers, recipeHandlers, recipeTransferHandlers, unsortedRecipes, recipes, recipeClickableAreas, recipeCatalysts, ingredientRegistry, recipeRegistryPlugins);
}
Expand Down

0 comments on commit 156f46e

Please sign in to comment.