package mezz.jei.common.plugins.vanilla.crafting;

import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.common.util.RecipeErrorUtil;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.Ingredient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

public final class CategoryRecipeValidator<T extends Recipe<?>> {
	private static final Logger LOGGER = LogManager.getLogger();
	private static final int INVALID_COUNT = -1;
	private final IRecipeCategory<T> recipeCategory;
	private final int maxInputs;

	public CategoryRecipeValidator(IRecipeCategory<T> recipeCategory, int maxInputs) {
		this.recipeCategory = recipeCategory;
		this.maxInputs = maxInputs;
	}

	public boolean isRecipeValid(T recipe) {
		return hasValidInputsAndOutputs(recipe);
	}

	public boolean isRecipeHandled(T recipe) {
		return this.recipeCategory.isHandled(recipe);
	}

	@SuppressWarnings("ConstantConditions")
	private boolean hasValidInputsAndOutputs(T recipe) {
		if (recipe.isSpecial()) {
			return true;
		}
		ItemStack recipeOutput = recipe.getResultItem();
		if (recipeOutput == null || recipeOutput.isEmpty()) {
			String recipeInfo = RecipeErrorUtil.getInfoFromRecipe(recipe, recipeCategory);
			LOGGER.error("Recipe has no output. {}", recipeInfo);
			return false;
		}
		List<Ingredient> ingredients = recipe.getIngredients();
		if (ingredients == null) {
			String recipeInfo = RecipeErrorUtil.getInfoFromRecipe(recipe, recipeCategory);
			LOGGER.error("Recipe has no input Ingredients. {}", recipeInfo);
			return false;
		}
		int inputCount = getInputCount(ingredients);
		if (inputCount == INVALID_COUNT) {
			return false;
		} else if (inputCount > maxInputs) {
			String recipeInfo = RecipeErrorUtil.getInfoFromRecipe(recipe, recipeCategory);
			LOGGER.error("Recipe has too many inputs. {}", recipeInfo);
			return false;
		} else if (inputCount == 0 && maxInputs > 0) {
			String recipeInfo = RecipeErrorUtil.getInfoFromRecipe(recipe, recipeCategory);
			LOGGER.error("Recipe has no inputs. {}", recipeInfo);
			return false;
		}
		return true;
	}

	@SuppressWarnings("ConstantConditions")
	private static int getInputCount(List<Ingredient> ingredientList) {
		int inputCount = 0;
		for (Ingredient ingredient : ingredientList) {
			ItemStack[] input = ingredient.getItems();
			if (input == null) {
				return INVALID_COUNT;
			} else {
				inputCount++;
			}
		}
		return inputCount;
	}
}

@@ -1240,24 +1240,59 @@
 sEmpty()) %7B%0A
+%09%09%09if (LOGGER.isDebugEnabled()) %7B%0A%09
 %09%09%09String re
@@ -1364,36 +1364,57 @@
 ry);%0A%09%09%09
+%09
 LOGGER.
-error(%22Recipe
+debug(%22Skipping Recipe because it
  has no 
@@ -1431,32 +1431,37 @@
 %22, recipeInfo);%0A
+%09%09%09%7D%0A
 %09%09%09return false;
@@ -1548,24 +1548,59 @@
  == null) %7B%0A
+%09%09%09if (LOGGER.isDebugEnabled()) %7B%0A%09
 %09%09%09String re
@@ -1672,36 +1672,57 @@
 ry);%0A%09%09%09
+%09
 LOGGER.
-error(%22Recipe
+debug(%22Skipping Recipe because it
  has no 
@@ -1750,32 +1750,37 @@
 %22, recipeInfo);%0A
+%09%09%09%7D%0A
 %09%09%09return false;
@@ -1864,24 +1864,234 @@
 ID_COUNT) %7B%0A
+%09%09%09if (LOGGER.isDebugEnabled()) %7B%0A%09%09%09%09String recipeInfo = RecipeErrorUtil.getInfoFromRecipe(recipe, recipeCategory);%0A%09%09%09%09LOGGER.debug(%22Skipping Recipe because it contains invalid inputs. %7B%7D%22, recipeInfo);%0A%09%09%09%7D%0A
 %09%09%09return fa
@@ -2130,24 +2130,59 @@
 axInputs) %7B%0A
+%09%09%09if (LOGGER.isDebugEnabled()) %7B%0A%09
 %09%09%09String re
@@ -2254,36 +2254,57 @@
 ry);%0A%09%09%09
+%09
 LOGGER.
-error(%22Recipe
+debug(%22Skipping Recipe because it
  has too
@@ -2327,32 +2327,37 @@
 %22, recipeInfo);%0A
+%09%09%09%7D%0A
 %09%09%09return false;
@@ -2406,16 +2406,51 @@
  %3E 0) %7B%0A
+%09%09%09if (LOGGER.isDebugEnabled()) %7B%0A%09
 %09%09%09Strin
@@ -2530,28 +2530,49 @@
 %0A%09%09%09
+%09
 LOGGER.
-error(%22Recipe
+debug(%22Skipping Recipe because it
  has
@@ -2601,16 +2601,21 @@
 eInfo);%0A
+%09%09%09%7D%0A
 %09%09%09retur

Fix #2993 Reduce recipe validation errors to debug logs