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.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 bjw, final Player ayj, final InteractionHand ajk) {
        final List<AreaEffectCloud> list5 = bjw.<AreaEffectCloud>getEntitiesOfClass(AreaEffectCloud.class, ayj.getBoundingBox().inflate(2.0), akp -> akp != null && akp.isAlive() && akp.getOwner() instanceof EnderDragon);
        final ItemStack ben6 = ayj.getItemInHand(ajk);
        if (!list5.isEmpty()) {
            final AreaEffectCloud akp2 = list5.get(0);
            akp2.setRadius(akp2.getRadius() - 0.5f);
            bjw.playSound(null, ayj.getX(), ayj.getY(), ayj.getZ(), SoundEvents.BOTTLE_FILL_DRAGONBREATH, SoundSource.NEUTRAL, 1.0f, 1.0f);
            return InteractionResultHolder.<ItemStack>success(this.turnBottleIntoItem(ben6, ayj, new ItemStack(Items.DRAGON_BREATH)));
        }
        final HitResult cvi7 = Item.getPlayerPOVHitResult(bjw, ayj, ClipContext.Fluid.SOURCE_ONLY);
        if (cvi7.getType() == HitResult.Type.MISS) {
            return InteractionResultHolder.<ItemStack>pass(ben6);
        }
        if (cvi7.getType() == HitResult.Type.BLOCK) {
            final BlockPos fk8 = ((BlockHitResult)cvi7).getBlockPos();
            if (!bjw.mayInteract(ayj, fk8)) {
                return InteractionResultHolder.<ItemStack>pass(ben6);
            }
            if (bjw.getFluidState(fk8).is(FluidTags.WATER)) {
                bjw.playSound(ayj, ayj.getX(), ayj.getY(), ayj.getZ(), SoundEvents.BOTTLE_FILL, SoundSource.NEUTRAL, 1.0f, 1.0f);
                return InteractionResultHolder.<ItemStack>success(this.turnBottleIntoItem(ben6, ayj, PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER)));
            }
        }
        return InteractionResultHolder.<ItemStack>pass(ben6);
    }
    
    protected ItemStack turnBottleIntoItem(final ItemStack ben1, final Player ayj, final ItemStack ben3) {
        ben1.shrink(1);
        ayj.awardStat(Stats.ITEM_USED.get(this));
        if (ben1.isEmpty()) {
            return ben3;
        }
        if (!ayj.inventory.add(ben3)) {
            ayj.drop(ben3, false);
        }
        return ben1;
    }
}
