package mezz.jei.recipes;

import javax.annotation.Nullable;

import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.resources.ResourceLocation;

import com.google.common.collect.ImmutableTable;
import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandler;
import mezz.jei.config.Constants;
import mezz.jei.util.ErrorUtil;

public class RecipeTransferManager {
	private final ImmutableTable<Class<?>, ResourceLocation, IRecipeTransferHandler<?, ?>> recipeTransferHandlers;

	public RecipeTransferManager(ImmutableTable<Class<?>, ResourceLocation, IRecipeTransferHandler<?, ?>> recipeTransferHandlers) {
		this.recipeTransferHandlers = recipeTransferHandlers;
	}

	@SuppressWarnings("unchecked")
	@Nullable
	public <C extends AbstractContainerMenu, R> IRecipeTransferHandler<C, R> getRecipeTransferHandler(C container, IRecipeCategory<R> recipeCategory) {
		ErrorUtil.checkNotNull(container, "container");
		ErrorUtil.checkNotNull(recipeCategory, "recipeCategory");

		Class<? extends AbstractContainerMenu> containerClass = container.getClass();
		IRecipeTransferHandler<?, ?> recipeTransferHandler = recipeTransferHandlers.get(containerClass, recipeCategory.getUid());
		if (recipeTransferHandler != null) {
			if (recipeTransferHandler.getRecipeClass().isAssignableFrom(recipeCategory.getRecipeClass())) {
				return (IRecipeTransferHandler<C, R>) recipeTransferHandler;
			}
		}

		return (IRecipeTransferHandler<C, R>) recipeTransferHandlers.get(containerClass, Constants.UNIVERSAL_RECIPE_TRANSFER_UID);
	}
}

@@ -737,40 +737,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22unchecked%22)%0A
 %09@Nu
@@ -1084,16 +1084,85 @@
 lass();%0A
+%09%09Class%3C? extends R%3E recipeClass = recipeCategory.getRecipeClass();%0A%0A
 %09%09IRecip
@@ -1182,65 +1182,48 @@
 ler%3C
-?, ?%3E recipeTransfer
+C, R%3E handler = get
 Handler
- = 
+(
 recipe
-TransferHandlers.get(
+Class, 
 cont
@@ -1262,39 +1262,25 @@
 d());%0A%09%09if (
-recipeTransferH
+h
 andler != nu
@@ -1292,38 +1292,50 @@
 %0A%09%09%09
-if (recipeTransfer
+return handler;%0A%09%09%7D%0A%0A%09%09return get
 Handler
-.getR
+(r
 ecip
@@ -1344,149 +1344,328 @@
 lass
-().isAssignableFrom(recipeCategory.getRecipeClass())) %7B%0A%09%09%09%09return (IRecipeTransferHandler%3CC, R%3E) recipeTransferHandler;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return (
+, containerClass, Constants.UNIVERSAL_RECIPE_TRANSFER_UID);%0A%09%7D%0A%0A%09@SuppressWarnings(%22unchecked%22)%0A%09@Nullable%0A%09private %3CC extends AbstractContainerMenu, R%3E IRecipeTransferHandler%3CC, R%3E getHandler(Class%3C? extends R%3E recipeClass, Class%3C? extends AbstractContainerMenu%3E containerClass, ResourceLocation recipeCategoryUid) %7B%0A%09%09
 IRec
@@ -1687,14 +1687,23 @@
 ler%3C
-C, R%3E)
+?, ?%3E handler =
  rec
@@ -1746,48 +1746,251 @@
 ss, 
-Constants.UNIVERSAL_RECIPE_TRANSFER_UID)
+recipeCategoryUid);%0A%09%09if (handler != null &&%0A%09%09%09handler.getRecipeClass().isAssignableFrom(recipeClass) &&%0A%09%09%09handler.getContainerClass().isAssignableFrom(containerClass)%0A%09%09) %7B%0A%09%09%09return (IRecipeTransferHandler%3CC, R%3E) handler;%0A%09%09%7D%0A%09%09return null
 ;%0A%09%7D

Fix #2493, Restrict universal recipe transfer handlers to only accept recipes classes they can handle