Skip to content

Commit

Permalink
Close mezz#1298 add API method for hiding recipe categories
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 30, 2018
1 parent 8aad20b commit 4b483f7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ mcp_mappings=snapshot_20180812
curse_project_id=238222

version_major=4
version_minor=12
version_patch=1
version_minor=13
version_patch=0
20 changes: 20 additions & 0 deletions src/api/java/mezz/jei/api/IRecipeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ public interface IRecipeRegistry {
*/
void unhideRecipe(IRecipeWrapper recipe, String recipeCategoryUid);

/**
* Hide an entire recipe category of recipes from JEI.
* This can be used by mods that create recipe progression.
*
* @param recipeCategoryUid the unique ID for the recipe category
* @see #unhideRecipeCategory(String)
* @since JEI 4.13.0
*/
void hideRecipeCategory(String recipeCategoryUid);

/**
* Unhides a recipe category that was hidden by {@link #hideRecipeCategory(String)}.
* This can be used by mods that create recipe progression.
*
* @param recipeCategoryUid the unique ID for the recipe category
* @see #hideRecipeCategory(String)
* @since JEI 4.13.0
*/
void unhideRecipeCategory(String recipeCategoryUid);

// DEPRECATED BELOW

/**
Expand Down
24 changes: 6 additions & 18 deletions src/main/java/mezz/jei/plugins/jei/debug/DebugRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.ingredients.VanillaTypes;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.api.recipe.VanillaRecipeCategoryUid;
import mezz.jei.plugins.jei.JEIInternalPlugin;
Expand All @@ -27,13 +26,10 @@
import net.minecraftforge.fml.client.config.GuiButtonExt;
import net.minecraftforge.fml.client.config.HoverChecker;

import javax.annotation.Nullable;

public class DebugRecipe implements IRecipeWrapper {
private final GuiButtonExt button;
private final HoverChecker buttonHoverChecker;
@Nullable
private List<IRecipeWrapper> hiddenRecipes;
private boolean hiddenRecipes;

public DebugRecipe() {
this.button = new GuiButtonExt(0, 110, 30, "test");
Expand Down Expand Up @@ -99,20 +95,12 @@ public boolean handleClick(Minecraft minecraft, int mouseX, int mouseY, int mous
ingredientFilter.setFilterText(filterText + " test");

IRecipeRegistry recipeRegistry = runtime.getRecipeRegistry();
if (hiddenRecipes == null) {
@SuppressWarnings("unchecked")
IRecipeCategory<IRecipeWrapper> craftingRecipeCategory = recipeRegistry.getRecipeCategory(VanillaRecipeCategoryUid.CRAFTING);
if (craftingRecipeCategory != null) {
hiddenRecipes = recipeRegistry.getRecipeWrappers(craftingRecipeCategory);
}
for (IRecipeWrapper recipeWrapper : hiddenRecipes) {
recipeRegistry.hideRecipe(recipeWrapper, VanillaRecipeCategoryUid.CRAFTING);
}
if (!hiddenRecipes) {
recipeRegistry.hideRecipeCategory(VanillaRecipeCategoryUid.CRAFTING);
hiddenRecipes = true;
} else {
for (IRecipeWrapper recipeWrapper : hiddenRecipes) {
recipeRegistry.unhideRecipe(recipeWrapper, VanillaRecipeCategoryUid.CRAFTING);
}
hiddenRecipes = null;
recipeRegistry.unhideRecipeCategory(VanillaRecipeCategoryUid.CRAFTING);
hiddenRecipes = false;
}
}
return true;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/mezz/jei/recipes/RecipeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class RecipeRegistry implements IRecipeRegistry {
private final ImmutableList<IRecipeHandler> unsortedRecipeHandlers;
private final ImmutableMultimap<String, IRecipeHandler> recipeHandlers;
private final ImmutableList<IRecipeCategory> recipeCategories;
private final Set<String> hiddenRecipeCategoryUids = new HashSet<>();
private final List<IRecipeCategory> recipeCategoriesVisibleCache = new ArrayList<>();
private final ImmutableTable<Class, String, IRecipeTransferHandler> recipeTransferHandlers;
private final ImmutableMultimap<Class<? extends GuiContainer>, RecipeClickableArea> recipeClickableAreasMap;
Expand Down Expand Up @@ -371,6 +372,9 @@ public List<IRecipeCategory> getRecipeCategories() {
}

private boolean isCategoryVisible(IRecipeCategory<?> recipeCategory) {
if (hiddenRecipeCategoryUids.contains(recipeCategory.getUid())) {
return false;
}
List<Object> allCatalysts = getRecipeCatalysts(recipeCategory, true);
if (!allCatalysts.isEmpty()) {
List<Object> visibleCatalysts = getRecipeCatalysts(recipeCategory, false);
Expand Down Expand Up @@ -728,6 +732,29 @@ public void unhideRecipe(IRecipeWrapper recipe, String recipeCategoryUid) {
recipeCategoriesVisibleCache.clear();
}

@Override
public void hideRecipeCategory(String recipeCategoryUid) {
ErrorUtil.checkNotNull(recipeCategoryUid, "recipeCategoryUid");
ErrorUtil.assertMainThread();
IRecipeCategory recipeCategory = recipeCategoriesMap.get(recipeCategoryUid);
if (recipeCategory == null) {
throw new IllegalArgumentException("Unknown recipe category: " + recipeCategoryUid);
}
hiddenRecipeCategoryUids.add(recipeCategoryUid);
recipeCategoriesVisibleCache.remove(recipeCategory);
}

@Override
public void unhideRecipeCategory(String recipeCategoryUid) {
ErrorUtil.checkNotNull(recipeCategoryUid, "recipeCategoryUid");
ErrorUtil.assertMainThread();
if (!recipeCategoriesMap.containsKey(recipeCategoryUid)) {
throw new IllegalArgumentException("Unknown recipe category: " + recipeCategoryUid);
}
hiddenRecipeCategoryUids.remove(recipeCategoryUid);
recipeCategoriesVisibleCache.clear();
}

@Override
@Deprecated
public void hideRecipe(IRecipeWrapper recipe) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/mezz/jei/util/ErrorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ public static void checkIsValidIngredient(@Nullable Object ingredient, String na
public static void assertMainThread() {
Minecraft minecraft = Minecraft.getMinecraft();
if (minecraft != null && !minecraft.isCallingFromMinecraftThread()) {
throw new IllegalStateException("A JEI API method is being called by another mod from the wrong thread. It must be called on the main thread.");
Thread currentThread = Thread.currentThread();
throw new IllegalStateException(
"A JEI API method is being called by another mod from the wrong thread:\n" +
currentThread + "\n" +
"It must be called on the main thread by using Minecraft.addScheduledTask."
);
}
}

Expand Down

0 comments on commit 4b483f7

Please sign in to comment.