<commit_before>package mezz.jei;

import javax.annotation.Nullable;
import java.util.Locale;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.util.Log;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionHelper;
import net.minecraft.tileentity.TileEntityFurnace;

public class IngredientRegistry implements IIngredientRegistry {
	private final ImmutableMap<Class, ImmutableList> ingredientsMap;
	private final ImmutableMap<Class, IIngredientHelper> ingredientHelperMap;
	private final ImmutableMap<Class, IIngredientRenderer> ingredientRendererMap;
	private final ImmutableList<ItemStack> fuels;
	private final ImmutableList<ItemStack> potionIngredients;

	public IngredientRegistry(
			ImmutableMap<Class, ImmutableList> ingredientsMap,
			ImmutableMap<Class, IIngredientHelper> ingredientHelperMap,
			ImmutableMap<Class, IIngredientRenderer> ingredientRendererMap
	) {
		this.ingredientsMap = ingredientsMap;
		this.ingredientHelperMap = ingredientHelperMap;
		this.ingredientRendererMap = ingredientRendererMap;

		IIngredientHelper<ItemStack> itemStackHelper = getIngredientHelper(ItemStack.class);

		ImmutableList.Builder<ItemStack> fuelsBuilder = ImmutableList.builder();
		ImmutableList.Builder<ItemStack> potionIngredientsBuilder = ImmutableList.builder();
		ImmutableListMultimap.Builder<String, ItemStack> itemsByModIdBuilder = ImmutableListMultimap.builder();

		for (ItemStack itemStack : getIngredients(ItemStack.class)) {
			String modId = itemStackHelper.getModId(itemStack).toLowerCase(Locale.ENGLISH);
			itemsByModIdBuilder.put(modId, itemStack);

			if (TileEntityFurnace.isItemFuel(itemStack)) {
				fuelsBuilder.add(itemStack);
			}

			if (PotionHelper.isReagent(itemStack)) {
				potionIngredientsBuilder.add(itemStack);
			}
		}

		this.fuels = fuelsBuilder.build();
		this.potionIngredients = potionIngredientsBuilder.build();
	}

	@Override
	public <V> ImmutableList<V> getIngredients(@Nullable Class<V> ingredientClass) {
		if (ingredientClass == null) {
			Log.error("Null ingredientClass", new NullPointerException());
			return ImmutableList.of();
		}

		//noinspection unchecked
		ImmutableList<V> ingredients = ingredientsMap.get(ingredientClass);<commit_msg>Protect against more ItemStack crashes (#500)<commit_after>package mezz.jei;

import javax.annotation.Nullable;
import java.util.Locale;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.util.Log;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionHelper;
import net.minecraft.tileentity.TileEntityFurnace;

public class IngredientRegistry implements IIngredientRegistry {
	private final ImmutableMap<Class, ImmutableList> ingredientsMap;
	private final ImmutableMap<Class, IIngredientHelper> ingredientHelperMap;
	private final ImmutableMap<Class, IIngredientRenderer> ingredientRendererMap;
	private final ImmutableList<ItemStack> fuels;
	private final ImmutableList<ItemStack> potionIngredients;

	public IngredientRegistry(
			ImmutableMap<Class, ImmutableList> ingredientsMap,
			ImmutableMap<Class, IIngredientHelper> ingredientHelperMap,
			ImmutableMap<Class, IIngredientRenderer> ingredientRendererMap
	) {
		this.ingredientsMap = ingredientsMap;
		this.ingredientHelperMap = ingredientHelperMap;
		this.ingredientRendererMap = ingredientRendererMap;

		IIngredientHelper<ItemStack> itemStackHelper = getIngredientHelper(ItemStack.class);

		ImmutableList.Builder<ItemStack> fuelsBuilder = ImmutableList.builder();
		ImmutableList.Builder<ItemStack> potionIngredientsBuilder = ImmutableList.builder();
		ImmutableListMultimap.Builder<String, ItemStack> itemsByModIdBuilder = ImmutableListMultimap.builder();

		for (ItemStack itemStack : getIngredients(ItemStack.class)) {
			String modId = itemStackHelper.getModId(itemStack).toLowerCase(Locale.ENGLISH);
			itemsByModIdBuilder.put(modId, itemStack);

			try {
				if (TileEntityFurnace.isItemFuel(itemStack)) {
					fuelsBuilder.add(itemStack);
				}
			} catch (RuntimeException e) {
				String itemStackInfo = itemStackHelper.getErrorInfo(itemStack);
				Log.error("Failed to check if item is fuel {}.", itemStackInfo, e);
			} catch (LinkageError e) {
				String itemStackInfo = itemStackHelper.getErrorInfo(itemStack);
				Log.error("Failed to check if item is fuel {}.", itemStackInfo, e);
			}

			try {
				if (PotionHelper.isReagent(itemStack)) {
					potionIngredientsBuilder.add(itemStack);
				}
			} catch (RuntimeException e) {
				String itemStackInfo = itemStackHelper.getErrorInfo(itemStack);
				Log.error("Failed to check if item is a potion ingredient {}.", itemStackInfo, e);
			} catch (LinkageError e) {
				String itemStackInfo = itemStackHelper.getErrorInfo(itemStack);
				Log.error("Failed to check if item is a potion ingredient {}.", itemStackInfo, e);
			}
		}

		this.fuels = fuelsBuilder.build();
		this.potionIngredients = potionIngredientsBuilder.build();
	}

	@Override
	public <V> ImmutableList<V> getIngredients(@Nullable Class<V> ingredientClass) {
		if (ingredientClass == null) {
			Log.error("Null ingredientClass", new NullPointerException());
			return ImmutableList.of();
		}

		//noinspection unchecked
		ImmutableList<V> ingredients = ingredientsMap.get(ingredientClass);
{'fraction_non_alphanumeric': 0.09389911383776414, 'fraction_numerical': 0.0005112474437627812, 'mean_word_length': 6.33625, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '4432928', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1786, 'n_words': 322}