Skip to content

Commit

Permalink
Fix #1567 Fix #1575 Improve display of wildcard items with no creativ…
Browse files Browse the repository at this point in the history
…e menu subtypes
  • Loading branch information
mezz committed May 12, 2019
1 parent 1ff6c8a commit d84c7b9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
Expand Up @@ -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;
Expand All @@ -30,24 +32,25 @@ public static List<IRecipe> getValidRecipes(final IJeiHelpers jeiHelpers) {
CraftingRecipeValidator<ShapelessOreRecipe> shapelessOreRecipeValidator = new CraftingRecipeValidator<>(recipe -> new ShapelessRecipeWrapper<>(jeiHelpers, recipe));
CraftingRecipeValidator<ShapelessRecipes> shapelessRecipesValidator = new CraftingRecipeValidator<>(recipe -> new ShapelessRecipeWrapper<>(jeiHelpers, recipe));

StackHelper stackHelper = Internal.getStackHelper();
Iterator<IRecipe> recipeIterator = CraftingManager.REGISTRY.iterator();
List<IRecipe> 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 {
Expand All @@ -59,13 +62,14 @@ public static List<IRecipe> getValidRecipes(final IJeiHelpers jeiHelpers) {

private static final class CraftingRecipeValidator<T extends IRecipe> {
private static final int INVALID_COUNT = -1;
private static final int CANT_DISPLAY = -2;
private final IRecipeWrapperFactory<T> recipeWrapperFactory;

public CraftingRecipeValidator(IRecipeWrapperFactory<T> 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()) {
Expand All @@ -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);
Expand All @@ -100,15 +108,17 @@ private String getInfo(T recipe) {
return ErrorUtil.getInfoFromRecipe(recipe, recipeWrapper);
}

protected static int getInputCount(List<Ingredient> ingredientList) {
protected static int getInputCount(List<Ingredient> ingredientList, StackHelper stackHelper) {
int inputCount = 0;
for (Ingredient ingredient : ingredientList) {
ItemStack[] input = ingredient.getMatchingStacks();
List<ItemStack> 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++;
}
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/mezz/jei/startup/StackHelper.java
Expand Up @@ -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;
Expand Down Expand Up @@ -204,6 +205,21 @@ public boolean isEquivalent(@Nullable ItemStack lhs, @Nullable ItemStack rhs) {
return keyLhs.equals(keyRhs);
}

public List<ItemStack> 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<ItemStack> getSubtypes(@Nullable ItemStack itemStack) {
if (itemStack == null || itemStack.isEmpty()) {
Expand All @@ -224,7 +240,9 @@ private void addSubtypesToList(final List<ItemStack> 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);
}
Expand All @@ -248,6 +266,10 @@ private void addSubtypesFromCreativeTabToList(List<ItemStack> 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()) {
Expand Down Expand Up @@ -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<ItemStack> stacks = getMatchingStacks((Ingredient) input);
for (ItemStack stack : stacks) {
toItemStackList(itemStackListBuilder, stack, expandSubtypes);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/jei_at.cfg
Expand Up @@ -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

0 comments on commit d84c7b9

Please sign in to comment.