package info.u_team.u_team_test.global_loot_modifier;

import java.util.List;
import java.util.stream.Collectors;

import com.google.gson.JsonObject;

import net.minecraft.item.crafting.FurnaceRecipe;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.loot.conditions.ILootCondition;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
import net.minecraftforge.common.loot.LootModifier;
import net.minecraftforge.items.ItemHandlerHelper;

public class AutoSmeltLootModifier extends LootModifier {
	
	public AutoSmeltLootModifier(ILootCondition[] conditions) {
		super(conditions);
	}
	
	@Override
	protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
		return generatedLoot.stream().map(stack -> smeltItem(stack, context)).collect(Collectors.toList());
	}
	
	private static ItemStack smeltItem(ItemStack stack, LootContext context) {
		return context.getWorld() //
				.getRecipeManager() //
				.getRecipe(IRecipeType.SMELTING, new Inventory(stack), context.getWorld()) //
				.map(FurnaceRecipe::getRecipeOutput).filter(itemStack -> !itemStack.isEmpty()) //
				.map(itemStack -> ItemHandlerHelper.copyStackWithSize(itemStack, stack.getCount() * itemStack.getCount())) //
				.orElse(stack);
	}
	
	public static class Serializer extends GlobalLootModifierSerializer<AutoSmeltLootModifier> {
		
		@Override
		public AutoSmeltLootModifier read(ResourceLocation name, JsonObject json, ILootCondition[] conditions) {
			return new AutoSmeltLootModifier(conditions);
		}
		
		@Override
		public JsonObject write(AutoSmeltLootModifier instance) {
			return makeConditions(instance.conditions);
		}
	}
	
}

@@ -169,185 +169,174 @@
 aft.
-item.crafting.FurnaceRecipe;%0Aimport net.minecraft.item.crafting.IRecipeType;%0Aimport net.minecraft.loot.conditions.ILootCondition;%0Aimport net.minecraft.resources.ResourceLocation
+resources.ResourceLocation;%0Aimport net.minecraft.world.SimpleContainer;%0Aimport net.minecraft.world.item.ItemStack;%0Aimport net.minecraft.world.item.crafting.RecipeType
 ;%0Aim
@@ -364,31 +364,36 @@
 rld.
-entity.player.Inventory
+item.crafting.SmeltingRecipe
 ;%0Aim
@@ -413,38 +413,54 @@
 craft.world.
-item.ItemStack
+level.storage.loot.LootContext
 ;%0Aimport net
@@ -491,35 +491,52 @@
 torage.loot.
-LootContext
+predicates.LootItemCondition
 ;%0Aimport net
@@ -787,21 +787,24 @@
 odifier(
-I
 Loot
+Item
 Conditio
@@ -1150,21 +1150,21 @@
 text.get
-World
+Level
 () //%0A%09%09
@@ -1202,18 +1202,20 @@
 etRecipe
+For
 (
-I
 RecipeTy
@@ -1235,17 +1235,23 @@
 new 
-Inventory
+SimpleContainer
 (sta
@@ -1270,13 +1270,13 @@
 .get
-World
+Level
 ()) 
@@ -1291,15 +1291,16 @@
 map(
-Furnace
+Smelting
 Reci
@@ -1312,18 +1312,16 @@
 etRe
-cipeOutput
+sultItem
 ).fi
@@ -1691,13 +1691,16 @@
 on, 
-I
 Loot
+Item
 Cond

Fix autosmelt loot modifier