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.resources.ResourceLocation;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
import net.minecraftforge.common.loot.LootModifier;
import net.minecraftforge.items.ItemHandlerHelper;

public class AutoSmeltLootModifier extends LootModifier {
	
	public AutoSmeltLootModifier(LootItemCondition[] 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.getLevel() //
				.getRecipeManager() //
				.getRecipeFor(RecipeType.SMELTING, new SimpleContainer(stack), context.getLevel()) //
				.map(SmeltingRecipe::getResultItem).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, LootItemCondition[] conditions) {
			return new AutoSmeltLootModifier(conditions);
		}
		
		@Override
		public JsonObject write(AutoSmeltLootModifier instance) {
			return makeConditions(instance.conditions);
		}
	}
	
}

@@ -337,65 +337,8 @@
 pe;%0A
-import net.minecraft.world.item.crafting.SmeltingRecipe;%0A
 impo
@@ -392,16 +392,16 @@
 ontext;%0A
+
 import n
@@ -591,59 +591,8 @@
 ier;
-%0Aimport net.minecraftforge.items.ItemHandlerHelper;
 %0A%0Apu
@@ -1014,32 +1014,87 @@
 text context) %7B%0A
+%09%09final var container = new SimpleContainer(stack);%0A%09%09%0A
 %09%09return context
@@ -1174,35 +1174,25 @@
 ELTING, 
-new SimpleC
+c
 ontainer
 (stack),
@@ -1183,23 +1183,16 @@
 ontainer
-(stack)
 , contex
@@ -1221,38 +1221,53 @@
 map(
-SmeltingRecipe::getResultItem)
+recipe -%3E recipe.assemble(container)) //%0A%09%09%09%09
 .fil
@@ -1334,55 +1334,34 @@
  -%3E 
-ItemHandlerHelper.copyStackWithSize(itemStack, 
+%7B%0A%09%09%09%09%09itemStack.setCount(
 stac
@@ -1396,16 +1396,46 @@
 Count())
+;%0A%09%09%09%09%09return itemStack;%0A%09%09%09%09%7D
 ) //%0A%09%09%09

Update autosmelt to the right methods