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

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
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);
    }
    
    @Override
    public LootItemFunctionType getType() {
        return LootItemFunctions.FURNACE_SMELT;
    }
    
    public ItemStack run(final ItemStack bki, final LootContext dat) {
        if (bki.isEmpty()) {
            return bki;
        }
        final Optional<SmeltingRecipe> optional4 = dat.getLevel().getRecipeManager().<SimpleContainer, SmeltingRecipe>getRecipeFor(RecipeType.SMELTING, new SimpleContainer(new ItemStack[] { bki }), dat.getLevel());
        if (optional4.isPresent()) {
            final ItemStack bki2 = optional4.get().getResultItem();
            if (!bki2.isEmpty()) {
                final ItemStack bki3 = bki2.copy();
                bki3.setCount(bki.getCount());
                return bki3;
            }
        }
        SmeltItemFunction.LOGGER.warn("Couldn't smelt {} because there is no smelting recipe", bki);
        return bki;
    }
    
    public static Builder<?> smelted() {
        return LootItemConditionalFunction.simpleBuilder((Function<LootItemCondition[], LootItemFunction>)SmeltItemFunction::new);
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
    
    public static class Serializer extends LootItemConditionalFunction.Serializer<SmeltItemFunction> {
        @Override
        public SmeltItemFunction deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            return new SmeltItemFunction(arr, null);
        }
    }
}
