package net.minecraft.world.item;

import org.apache.logging.log4j.LogManager;
import java.util.Optional;
import net.minecraft.world.item.crafting.RecipeManager;
import java.util.List;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import java.util.Collection;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.resources.ResourceLocation;
import com.google.common.collect.Lists;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.apache.logging.log4j.Logger;

public class KnowledgeBookItem extends Item {
    private static final Logger LOGGER;
    
    public KnowledgeBookItem(final Properties a) {
        super(a);
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final ItemStack bki5 = bec.getItemInHand(anf);
        final CompoundTag le6 = bki5.getTag();
        if (!bec.abilities.instabuild) {
            bec.setItemInHand(anf, ItemStack.EMPTY);
        }
        if (le6 == null || !le6.contains("Recipes", 9)) {
            KnowledgeBookItem.LOGGER.error("Tag not valid: {}", le6);
            return InteractionResultHolder.<ItemStack>fail(bki5);
        }
        if (!bqb.isClientSide) {
            final ListTag lk7 = le6.getList("Recipes", 8);
            final List<Recipe<?>> list8 = Lists.newArrayList();
            final RecipeManager bmv9 = bqb.getServer().getRecipeManager();
            for (int integer10 = 0; integer10 < lk7.size(); ++integer10) {
                final String string11 = lk7.getString(integer10);
                final Optional<? extends Recipe<?>> optional12 = bmv9.byKey(new ResourceLocation(string11));
                if (!optional12.isPresent()) {
                    KnowledgeBookItem.LOGGER.error("Invalid recipe: {}", string11);
                    return InteractionResultHolder.<ItemStack>fail(bki5);
                }
                list8.add(optional12.get());
            }
            bec.awardRecipes(list8);
            bec.awardStat(Stats.ITEM_USED.get(this));
        }
        return InteractionResultHolder.<ItemStack>sidedSuccess(bki5, bqb.isClientSide());
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
