<commit_before>package mezz.jei;

import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.MutableClassToInstanceMap;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import net.minecraft.item.ItemStack;

import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;

import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeHandler;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.util.Log;
import mezz.jei.util.RecipeCategoryComparator;
import mezz.jei.util.RecipeMap;
import mezz.jei.util.StackUtil;

public class RecipeRegistry implements IRecipeRegistry {
	private final ImmutableMap<Class, IRecipeHandler> recipeHandlers;
	private final ImmutableClassToInstanceMap<IRecipeCategory> recipeCategoriesMap;
	private final RecipeMap recipeInputMap;
	private final RecipeMap recipeOutputMap;

	RecipeRegistry(@Nonnull ImmutableList<IRecipeCategory> recipeCategories, @Nonnull ImmutableList<IRecipeHandler> recipeHandlers, @Nonnull ImmutableList<Object> recipes) {
		recipeCategories = ImmutableSet.copyOf(recipeCategories).asList(); //remove duplicates
		this.recipeCategoriesMap = buildRecipeCategoriesMap(recipeCategories);
		this.recipeHandlers = buildRecipeHandlersMap(recipeHandlers);

		RecipeCategoryComparator recipeCategoryComparator = new RecipeCategoryComparator(recipeCategories);
		this.recipeInputMap = new RecipeMap(recipeCategoryComparator);
		this.recipeOutputMap = new RecipeMap(recipeCategoryComparator);

		addRecipes(recipes);
	}

	private static ImmutableClassToInstanceMap<IRecipeCategory> buildRecipeCategoriesMap(@Nonnull ImmutableList<IRecipeCategory> recipeCategories) {
		MutableClassToInstanceMap<IRecipeCategory> mutableRecipeCategoriesMap = MutableClassToInstanceMap.create();
		for (IRecipeCategory recipeCategory : recipeCategories) {
			mutableRecipeCategoriesMap.put(recipeCategory.getClass(), recipeCategory);
		}
		return ImmutableClassToInstanceMap.copyOf(mutableRecipeCategoriesMap);
	}

	private static ImmutableMap<Class, IRecipeHandler> buildRecipeHandlersMap(@Nonnull List<IRecipeHandler> recipeHandlers) {
		HashMap<Class, IRecipeHandler> mutableRecipeHandlers = Maps.newHashMap();
		for (IRecipeHandler recipeHandler : recipeHandlers) {
			if (recipeHandler == null) {
				continue;
			}

			Class recipeClass = recipeHandler.getRecipeClass();
			if (recipeClass == null) {
				continue;
			}

			if (mutableRecipeHandlers.containsKey(recipeClass)) {
				throw new IllegalArgumentException("A Recipe Handler has already been registered for this recipe class: " + recipeClass.getName());
			}

			mutableRecipeHandlers.put(recipeClass, recipeHandler);
		}
		return ImmutableMap.copyOf(mutableRecipeHandlers);
	}

	private void addRecipes(@Nullable ImmutableList<Object> recipes) {
		if (recipes == null) {
			return;
		}

		for (Object recipe : recipes) {
			if (recipe == null) {
				continue;
			}

			Class recipeClass = recipe.getClass();

			IRecipeHandler recipeHandler = getRecipeHandler(recipeClass);
			if (recipeHandler == null) {
				Log.debug("Can't handle recipe: {}", recipe);
				continue;
			}
			Class recipeCategoryClass = recipeHandler.getRecipeCategoryClass();
			IRecipeCategory recipeCategory = recipeCategoriesMap.getInstance(recipeCategoryClass);
			if (recipeCategory == null) {
				Log.error("No recipe category registered for recipeCategoryClass: {}", recipeCategoryClass);
				continue;
			}

			//noinspection unchecked
			if (!recipeHandler.isRecipeValid(recipe)) {
				continue;
			}

			//noinspection unchecked
			IRecipeWrapper recipeWrapper = recipeHandler.getRecipeWrapper(recipe);

			List inputs = recipeWrapper.getInputs();
			List<FluidStack> fluidInputs = recipeWrapper.getFluidInputs();
			if (inputs != null || fluidInputs != null) {
				List<ItemStack> inputStacks = StackUtil.toItemStackList(inputs);
				if (fluidInputs == null) {
					fluidInputs = Collections.emptyList();
				}
				recipeInputMap.addRecipe(recipe, recipeCategory, inputStacks, fluidInputs);
			}

			List outputs = recipeWrapper.getOutputs();<commit_msg>Cut down on debug log spam<commit_after>package mezz.jei;

import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.MutableClassToInstanceMap;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.minecraft.item.ItemStack;

import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;

import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeHandler;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.util.Log;
import mezz.jei.util.RecipeCategoryComparator;
import mezz.jei.util.RecipeMap;
import mezz.jei.util.StackUtil;

public class RecipeRegistry implements IRecipeRegistry {
	private final ImmutableMap<Class, IRecipeHandler> recipeHandlers;
	private final ImmutableClassToInstanceMap<IRecipeCategory> recipeCategoriesMap;
	private final RecipeMap recipeInputMap;
	private final RecipeMap recipeOutputMap;
	private final Set<Class> unhandledRecipeClasses = new HashSet<>();

	RecipeRegistry(@Nonnull ImmutableList<IRecipeCategory> recipeCategories, @Nonnull ImmutableList<IRecipeHandler> recipeHandlers, @Nonnull ImmutableList<Object> recipes) {
		recipeCategories = ImmutableSet.copyOf(recipeCategories).asList(); //remove duplicates
		this.recipeCategoriesMap = buildRecipeCategoriesMap(recipeCategories);
		this.recipeHandlers = buildRecipeHandlersMap(recipeHandlers);

		RecipeCategoryComparator recipeCategoryComparator = new RecipeCategoryComparator(recipeCategories);
		this.recipeInputMap = new RecipeMap(recipeCategoryComparator);
		this.recipeOutputMap = new RecipeMap(recipeCategoryComparator);

		addRecipes(recipes);
	}

	private static ImmutableClassToInstanceMap<IRecipeCategory> buildRecipeCategoriesMap(@Nonnull ImmutableList<IRecipeCategory> recipeCategories) {
		MutableClassToInstanceMap<IRecipeCategory> mutableRecipeCategoriesMap = MutableClassToInstanceMap.create();
		for (IRecipeCategory recipeCategory : recipeCategories) {
			mutableRecipeCategoriesMap.put(recipeCategory.getClass(), recipeCategory);
		}
		return ImmutableClassToInstanceMap.copyOf(mutableRecipeCategoriesMap);
	}

	private static ImmutableMap<Class, IRecipeHandler> buildRecipeHandlersMap(@Nonnull List<IRecipeHandler> recipeHandlers) {
		HashMap<Class, IRecipeHandler> mutableRecipeHandlers = Maps.newHashMap();
		for (IRecipeHandler recipeHandler : recipeHandlers) {
			if (recipeHandler == null) {
				continue;
			}

			Class recipeClass = recipeHandler.getRecipeClass();
			if (recipeClass == null) {
				continue;
			}

			if (mutableRecipeHandlers.containsKey(recipeClass)) {
				throw new IllegalArgumentException("A Recipe Handler has already been registered for this recipe class: " + recipeClass.getName());
			}

			mutableRecipeHandlers.put(recipeClass, recipeHandler);
		}
		return ImmutableMap.copyOf(mutableRecipeHandlers);
	}

	private void addRecipes(@Nullable ImmutableList<Object> recipes) {
		if (recipes == null) {
			return;
		}

		for (Object recipe : recipes) {
			if (recipe == null) {
				continue;
			}

			Class recipeClass = recipe.getClass();

			IRecipeHandler recipeHandler = getRecipeHandler(recipeClass);
			if (recipeHandler == null) {
				if (!unhandledRecipeClasses.contains(recipeClass)) {
					unhandledRecipeClasses.add(recipeClass);
					Log.debug("Can't handle recipe: {}", recipeClass);
				}
				continue;
			}
			Class recipeCategoryClass = recipeHandler.getRecipeCategoryClass();
			IRecipeCategory recipeCategory = recipeCategoriesMap.getInstance(recipeCategoryClass);
			if (recipeCategory == null) {
				Log.error("No recipe category registered for recipeCategoryClass: {}", recipeCategoryClass);
				continue;
			}

			//noinspection unchecked
			if (!recipeHandler.isRecipeValid(recipe)) {
				continue;
			}

			//noinspection unchecked
			IRecipeWrapper recipeWrapper = recipeHandler.getRecipeWrapper(recipe);

			List inputs = recipeWrapper.getInputs();
			List<FluidStack> fluidInputs = recipeWrapper.getFluidInputs();
			if (inputs != null || fluidInputs != null) {
				List<ItemStack> inputStacks = StackUtil.toItemStackList(inputs);
				if (fluidInputs == null) {
					fluidInputs = Collections.emptyList();
				}
				recipeInputMap.addRecipe(recipe, recipeCategory, inputStacks, fluidInputs);
			}

			List outputs = recipeWrapper.getOutputs();
{'fraction_non_alphanumeric': 0.09260888693356797, 'fraction_numerical': 0.0, 'mean_word_length': 5.946524064171123, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '241195', 'n_tokens_mistral': 2987, 'n_tokens_neox': 2704, 'n_words': 503}