From d84c7b99b0b9a41d08369a6707ef3b2e73b4c14e Mon Sep 17 00:00:00 2001 From: mezz Date: Sun, 12 May 2019 11:54:53 -0700 Subject: [PATCH] Fix #1567 Fix #1575 Improve display of wildcard items with no creative menu subtypes --- .../crafting/CraftingRecipeChecker.java | 30 ++++++++++++------- .../java/mezz/jei/startup/StackHelper.java | 26 ++++++++++++++-- src/main/resources/jei_at.cfg | 3 ++ 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/main/java/mezz/jei/plugins/vanilla/crafting/CraftingRecipeChecker.java b/src/main/java/mezz/jei/plugins/vanilla/crafting/CraftingRecipeChecker.java index 13d97e874..1941c2026 100644 --- a/src/main/java/mezz/jei/plugins/vanilla/crafting/CraftingRecipeChecker.java +++ b/src/main/java/mezz/jei/plugins/vanilla/crafting/CraftingRecipeChecker.java @@ -4,6 +4,8 @@ import java.util.Iterator; import java.util.List; +import mezz.jei.Internal; +import mezz.jei.startup.StackHelper; import net.minecraftforge.oredict.OreIngredient; import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; @@ -30,24 +32,25 @@ public static List getValidRecipes(final IJeiHelpers jeiHelpers) { CraftingRecipeValidator shapelessOreRecipeValidator = new CraftingRecipeValidator<>(recipe -> new ShapelessRecipeWrapper<>(jeiHelpers, recipe)); CraftingRecipeValidator shapelessRecipesValidator = new CraftingRecipeValidator<>(recipe -> new ShapelessRecipeWrapper<>(jeiHelpers, recipe)); + StackHelper stackHelper = Internal.getStackHelper(); Iterator recipeIterator = CraftingManager.REGISTRY.iterator(); List validRecipes = new ArrayList<>(); while (recipeIterator.hasNext()) { IRecipe recipe = recipeIterator.next(); if (recipe instanceof ShapedOreRecipe) { - if (shapedOreRecipeValidator.isRecipeValid((ShapedOreRecipe) recipe)) { + if (shapedOreRecipeValidator.isRecipeValid((ShapedOreRecipe) recipe, stackHelper)) { validRecipes.add(recipe); } } else if (recipe instanceof ShapedRecipes) { - if (shapedRecipesValidator.isRecipeValid((ShapedRecipes) recipe)) { + if (shapedRecipesValidator.isRecipeValid((ShapedRecipes) recipe, stackHelper)) { validRecipes.add(recipe); } } else if (recipe instanceof ShapelessOreRecipe) { - if (shapelessOreRecipeValidator.isRecipeValid((ShapelessOreRecipe) recipe)) { + if (shapelessOreRecipeValidator.isRecipeValid((ShapelessOreRecipe) recipe, stackHelper)) { validRecipes.add(recipe); } } else if (recipe instanceof ShapelessRecipes) { - if (shapelessRecipesValidator.isRecipeValid((ShapelessRecipes) recipe)) { + if (shapelessRecipesValidator.isRecipeValid((ShapelessRecipes) recipe, stackHelper)) { validRecipes.add(recipe); } } else { @@ -59,13 +62,14 @@ public static List getValidRecipes(final IJeiHelpers jeiHelpers) { private static final class CraftingRecipeValidator { private static final int INVALID_COUNT = -1; + private static final int CANT_DISPLAY = -2; private final IRecipeWrapperFactory recipeWrapperFactory; public CraftingRecipeValidator(IRecipeWrapperFactory recipeWrapperFactory) { this.recipeWrapperFactory = recipeWrapperFactory; } - public boolean isRecipeValid(T recipe) { + public boolean isRecipeValid(T recipe, StackHelper stackHelper) { ItemStack recipeOutput = recipe.getRecipeOutput(); //noinspection ConstantConditions if (recipeOutput == null || recipeOutput.isEmpty()) { @@ -80,8 +84,12 @@ public boolean isRecipeValid(T recipe) { Log.get().error("Recipe has no input Ingredients. {}", recipeInfo); return false; } - int inputCount = getInputCount(ingredients); - if (inputCount == INVALID_COUNT) { + int inputCount = getInputCount(ingredients, stackHelper); + if (inputCount == CANT_DISPLAY) { + String recipeInfo = getInfo(recipe); + Log.get().warn("Recipe contains ingredients that can't be understood or displayed by JEI: {}", recipeInfo); + return false; + } else if (inputCount == INVALID_COUNT) { return false; } else if (inputCount > 9) { String recipeInfo = getInfo(recipe); @@ -100,15 +108,17 @@ private String getInfo(T recipe) { return ErrorUtil.getInfoFromRecipe(recipe, recipeWrapper); } - protected static int getInputCount(List ingredientList) { + protected static int getInputCount(List ingredientList, StackHelper stackHelper) { int inputCount = 0; for (Ingredient ingredient : ingredientList) { - ItemStack[] input = ingredient.getMatchingStacks(); + List input = stackHelper.getMatchingStacks(ingredient); //noinspection ConstantConditions if (input == null) { return INVALID_COUNT; - } else if (ingredient instanceof OreIngredient && input.length == 0) { + } else if (ingredient instanceof OreIngredient && input.isEmpty()) { return INVALID_COUNT; + } else if (!ingredient.isSimple() && input.isEmpty()) { + return CANT_DISPLAY; } else { inputCount++; } diff --git a/src/main/java/mezz/jei/startup/StackHelper.java b/src/main/java/mezz/jei/startup/StackHelper.java index e59c1f9c5..2bc245f7c 100644 --- a/src/main/java/mezz/jei/startup/StackHelper.java +++ b/src/main/java/mezz/jei/startup/StackHelper.java @@ -2,6 +2,7 @@ import javax.annotation.Nullable; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.EnumMap; @@ -204,6 +205,21 @@ public boolean isEquivalent(@Nullable ItemStack lhs, @Nullable ItemStack rhs) { return keyLhs.equals(keyRhs); } + public List getMatchingStacks(Ingredient ingredient) { + if (ingredient == Ingredient.EMPTY) { + return Collections.emptyList(); + } + ItemStack[] matchingStacks = ingredient.getMatchingStacks(); + //noinspection ConstantConditions + if (matchingStacks == null) { + return Collections.emptyList(); + } + if (matchingStacks.length > 0) { + return Arrays.asList(matchingStacks); + } + return getAllSubtypes(Arrays.asList(ingredient.matchingStacks)); + } + @Override public List getSubtypes(@Nullable ItemStack itemStack) { if (itemStack == null || itemStack.isEmpty()) { @@ -224,7 +240,9 @@ private void addSubtypesToList(final List subtypeList, ItemStack item final int stackSize = itemStack.getCount(); for (CreativeTabs itemTab : item.getCreativeTabs()) { if (itemTab == null) { - subtypeList.add(itemStack); + ItemStack copy = itemStack.copy(); + copy.setItemDamage(0); + subtypeList.add(copy); } else { addSubtypesFromCreativeTabToList(subtypeList, item, stackSize, itemTab); } @@ -248,6 +266,10 @@ private void addSubtypesFromCreativeTabToList(List subtypeList, Item } catch (RuntimeException | LinkageError e) { Log.get().warn("Caught a crash while getting sub-items of {}", item, e); } + if (subItems.isEmpty()) { + subtypeList.add(new ItemStack(item, stackSize)); + return; + } for (ItemStack subItem : subItems) { if (subItem.isEmpty()) { @@ -355,7 +377,7 @@ private void toItemStackList(UniqueItemStackListBuilder itemStackListBuilder, @N toItemStackList(itemStackListBuilder, stack, expandSubtypes); } } else if (input instanceof Ingredient) { - ItemStack[] stacks = ((Ingredient) input).getMatchingStacks(); + List stacks = getMatchingStacks((Ingredient) input); for (ItemStack stack : stacks) { toItemStackList(itemStackListBuilder, stack, expandSubtypes); } diff --git a/src/main/resources/jei_at.cfg b/src/main/resources/jei_at.cfg index 1ce8651ba..4bb9bafd3 100644 --- a/src/main/resources/jei_at.cfg +++ b/src/main/resources/jei_at.cfg @@ -15,3 +15,6 @@ public net.minecraft.client.gui.recipebook.GuiRecipeBook field_191904_o # width public net.minecraft.client.gui.recipebook.GuiRecipeBook field_191905_p # height public net.minecraft.client.gui.recipebook.GuiRecipeBook field_191903_n # xOffset public net.minecraft.client.gui.recipebook.GuiRecipeBook field_193018_j # recipeTabs + +#Ingredient +public net.minecraft.item.crafting.Ingredient field_193371_b # matchingStacks