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 bjw, final Player ayj, final InteractionHand ajk) {
        final ItemStack ben5 = ayj.getItemInHand(ajk);
        final CompoundTag jt6 = ben5.getTag();
        if (!ayj.abilities.instabuild) {
            ayj.setItemInHand(ajk, ItemStack.EMPTY);
        }
        if (jt6 == null || !jt6.contains("Recipes", 9)) {
            KnowledgeBookItem.LOGGER.error("Tag not valid: {}", jt6);
            return InteractionResultHolder.<ItemStack>fail(ben5);
        }
        if (!bjw.isClientSide) {
            final ListTag jz7 = jt6.getList("Recipes", 8);
            final List<Recipe<?>> list8 = Lists.newArrayList();
            final RecipeManager bgx9 = bjw.getServer().getRecipeManager();
            for (int integer10 = 0; integer10 < jz7.size(); ++integer10) {
                final String string11 = jz7.getString(integer10);
                final Optional<? extends Recipe<?>> optional12 = bgx9.byKey(new ResourceLocation(string11));
                if (!optional12.isPresent()) {
                    KnowledgeBookItem.LOGGER.error("Invalid recipe: {}", string11);
                    return InteractionResultHolder.<ItemStack>fail(ben5);
                }
                list8.add(optional12.get());
            }
            ayj.awardRecipes(list8);
            ayj.awardStat(Stats.ITEM_USED.get(this));
        }
        return InteractionResultHolder.<ItemStack>success(ben5);
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
