package net.minecraft.world.item;

import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.core.BlockPos;
import java.util.List;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.ItemLike;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
import net.minecraft.world.entity.AreaEffectCloud;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;

public class BottleItem extends Item {
    public BottleItem(final Properties a) {
        super(a);
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final List<AreaEffectCloud> list5 = bqb.<AreaEffectCloud>getEntitiesOfClass(AreaEffectCloud.class, bec.getBoundingBox().inflate(2.0), aol -> aol != null && aol.isAlive() && aol.getOwner() instanceof EnderDragon);
        final ItemStack bki6 = bec.getItemInHand(anf);
        if (!list5.isEmpty()) {
            final AreaEffectCloud aol2 = list5.get(0);
            aol2.setRadius(aol2.getRadius() - 0.5f);
            bqb.playSound(null, bec.getX(), bec.getY(), bec.getZ(), SoundEvents.BOTTLE_FILL_DRAGONBREATH, SoundSource.NEUTRAL, 1.0f, 1.0f);
            return InteractionResultHolder.<ItemStack>sidedSuccess(this.turnBottleIntoItem(bki6, bec, new ItemStack(Items.DRAGON_BREATH)), bqb.isClientSide());
        }
        final HitResult dej7 = Item.getPlayerPOVHitResult(bqb, bec, ClipContext.Fluid.SOURCE_ONLY);
        if (dej7.getType() == HitResult.Type.MISS) {
            return InteractionResultHolder.<ItemStack>pass(bki6);
        }
        if (dej7.getType() == HitResult.Type.BLOCK) {
            final BlockPos fu8 = ((BlockHitResult)dej7).getBlockPos();
            if (!bqb.mayInteract(bec, fu8)) {
                return InteractionResultHolder.<ItemStack>pass(bki6);
            }
            if (bqb.getFluidState(fu8).is(FluidTags.WATER)) {
                bqb.playSound(bec, bec.getX(), bec.getY(), bec.getZ(), SoundEvents.BOTTLE_FILL, SoundSource.NEUTRAL, 1.0f, 1.0f);
                return InteractionResultHolder.<ItemStack>sidedSuccess(this.turnBottleIntoItem(bki6, bec, PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER)), bqb.isClientSide());
            }
        }
        return InteractionResultHolder.<ItemStack>pass(bki6);
    }
    
    protected ItemStack turnBottleIntoItem(final ItemStack bki1, final Player bec, final ItemStack bki3) {
        bki1.shrink(1);
        bec.awardStat(Stats.ITEM_USED.get(this));
        if (bki1.isEmpty()) {
            return bki3;
        }
        if (!bec.inventory.add(bki3)) {
            bec.drop(bki3, false);
        }
        return bki1;
    }
}
