package net.minecraft.world.level.storage.loot.functions;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import java.util.function.Function;
import java.util.Optional;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import org.apache.logging.log4j.Logger;

public class SmeltItemFunction extends LootItemConditionalFunction {
    private static final Logger LOGGER;
    
    private SmeltItemFunction(final LootItemCondition[] arr) {
        super(arr);
    }
    
    public ItemStack run(final ItemStack ben, final LootContext crx) {
        if (ben.isEmpty()) {
            return ben;
        }
        final Optional<SmeltingRecipe> optional4 = crx.getLevel().getRecipeManager().<SimpleContainer, SmeltingRecipe>getRecipeFor(RecipeType.SMELTING, new SimpleContainer(new ItemStack[] { ben }), crx.getLevel());
        if (optional4.isPresent()) {
            final ItemStack ben2 = optional4.get().getResultItem();
            if (!ben2.isEmpty()) {
                final ItemStack ben3 = ben2.copy();
                ben3.setCount(ben.getCount());
                return ben3;
            }
        }
        SmeltItemFunction.LOGGER.warn("Couldn't smelt {} because there is no smelting recipe", ben);
        return ben;
    }
    
    public static Builder<?> smelted() {
        return LootItemConditionalFunction.simpleBuilder((Function<LootItemCondition[], LootItemFunction>)SmeltItemFunction::new);
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SmeltItemFunction> {
        protected Serializer() {
            super(new ResourceLocation("furnace_smelt"), SmeltItemFunction.class);
        }
        
        @Override
        public SmeltItemFunction deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            return new SmeltItemFunction(arr, null);
        }
    }
}
