package net.minecraft.world.item;

import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.material.Material;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.material.FlowingFluid;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.LiquidBlockContainer;
import net.minecraft.world.level.ItemLike;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.FluidTags;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.BucketPickup;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.material.Fluid;

public class BucketItem extends Item {
    private final Fluid content;
    
    public BucketItem(final Fluid coi, final Properties a) {
        super(a);
        this.content = coi;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bjw, final Player ayj, final InteractionHand ajk) {
        final ItemStack ben5 = ayj.getItemInHand(ajk);
        final HitResult cvi6 = Item.getPlayerPOVHitResult(bjw, ayj, (this.content == Fluids.EMPTY) ? ClipContext.Fluid.SOURCE_ONLY : ClipContext.Fluid.NONE);
        if (cvi6.getType() == HitResult.Type.MISS) {
            return InteractionResultHolder.<ItemStack>pass(ben5);
        }
        if (cvi6.getType() != HitResult.Type.BLOCK) {
            return InteractionResultHolder.<ItemStack>pass(ben5);
        }
        final BlockHitResult cvg7 = (BlockHitResult)cvi6;
        final BlockPos fk8 = cvg7.getBlockPos();
        final Direction fp9 = cvg7.getDirection();
        final BlockPos fk9 = fk8.relative(fp9);
        if (!bjw.mayInteract(ayj, fk8) || !ayj.mayUseItemAt(fk9, fp9, ben5)) {
            return InteractionResultHolder.<ItemStack>fail(ben5);
        }
        if (this.content == Fluids.EMPTY) {
            final BlockState byj11 = bjw.getBlockState(fk8);
            if (byj11.getBlock() instanceof BucketPickup) {
                final Fluid coi12 = ((BucketPickup)byj11.getBlock()).takeLiquid(bjw, fk8, byj11);
                if (coi12 != Fluids.EMPTY) {
                    ayj.awardStat(Stats.ITEM_USED.get(this));
                    ayj.playSound(coi12.is(FluidTags.LAVA) ? SoundEvents.BUCKET_FILL_LAVA : SoundEvents.BUCKET_FILL, 1.0f, 1.0f);
                    final ItemStack ben6 = this.createResultItem(ben5, ayj, coi12.getBucket());
                    if (!bjw.isClientSide) {
                        CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayer)ayj, new ItemStack(coi12.getBucket()));
                    }
                    return InteractionResultHolder.<ItemStack>success(ben6);
                }
            }
            return InteractionResultHolder.<ItemStack>fail(ben5);
        }
        final BlockState byj11 = bjw.getBlockState(fk8);
        final BlockPos fk10 = (byj11.getBlock() instanceof LiquidBlockContainer && this.content == Fluids.WATER) ? fk8 : fk9;
        if (this.emptyBucket(ayj, bjw, fk10, cvg7)) {
            this.checkExtraContent(bjw, ben5, fk10);
            if (ayj instanceof ServerPlayer) {
                CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer)ayj, fk10, ben5);
            }
            ayj.awardStat(Stats.ITEM_USED.get(this));
            return InteractionResultHolder.<ItemStack>success(this.getEmptySuccessItem(ben5, ayj));
        }
        return InteractionResultHolder.<ItemStack>fail(ben5);
    }
    
    protected ItemStack getEmptySuccessItem(final ItemStack ben, final Player ayj) {
        if (!ayj.abilities.instabuild) {
            return new ItemStack(Items.BUCKET);
        }
        return ben;
    }
    
    public void checkExtraContent(final Level bjw, final ItemStack ben, final BlockPos fk) {
    }
    
    private ItemStack createResultItem(final ItemStack ben, final Player ayj, final Item bei) {
        if (ayj.abilities.instabuild) {
            return ben;
        }
        ben.shrink(1);
        if (ben.isEmpty()) {
            return new ItemStack(bei);
        }
        if (!ayj.inventory.add(new ItemStack(bei))) {
            ayj.drop(new ItemStack(bei), false);
        }
        return ben;
    }
    
    public boolean emptyBucket(@Nullable final Player ayj, final Level bjw, final BlockPos fk, @Nullable final BlockHitResult cvg) {
        if (!(this.content instanceof FlowingFluid)) {
            return false;
        }
        final BlockState byj6 = bjw.getBlockState(fk);
        final Material coo7 = byj6.getMaterial();
        final boolean boolean8 = byj6.canBeReplaced(this.content);
        if (byj6.isAir() || boolean8 || (byj6.getBlock() instanceof LiquidBlockContainer && ((LiquidBlockContainer)byj6.getBlock()).canPlaceLiquid(bjw, fk, byj6, this.content))) {
            if (bjw.dimension.isUltraWarm() && this.content.is(FluidTags.WATER)) {
                final int integer9 = fk.getX();
                final int integer10 = fk.getY();
                final int integer11 = fk.getZ();
                bjw.playSound(ayj, fk, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5f, 2.6f + (bjw.random.nextFloat() - bjw.random.nextFloat()) * 0.8f);
                for (int integer12 = 0; integer12 < 8; ++integer12) {
                    bjw.addParticle(ParticleTypes.LARGE_SMOKE, integer9 + Math.random(), integer10 + Math.random(), integer11 + Math.random(), 0.0, 0.0, 0.0);
                }
            }
            else if (byj6.getBlock() instanceof LiquidBlockContainer && this.content == Fluids.WATER) {
                if (((LiquidBlockContainer)byj6.getBlock()).placeLiquid(bjw, fk, byj6, ((FlowingFluid)this.content).getSource(false))) {
                    this.playEmptySound(ayj, bjw, fk);
                }
            }
            else {
                if (!bjw.isClientSide && boolean8 && !coo7.isLiquid()) {
                    bjw.destroyBlock(fk, true);
                }
                this.playEmptySound(ayj, bjw, fk);
                bjw.setBlock(fk, this.content.defaultFluidState().createLegacyBlock(), 11);
            }
            return true;
        }
        return cvg != null && this.emptyBucket(ayj, bjw, cvg.getBlockPos().relative(cvg.getDirection()), null);
    }
    
    protected void playEmptySound(@Nullable final Player ayj, final LevelAccessor bjx, final BlockPos fk) {
        final SoundEvent aah5 = this.content.is(FluidTags.LAVA) ? SoundEvents.BUCKET_EMPTY_LAVA : SoundEvents.BUCKET_EMPTY;
        bjx.playSound(ayj, fk, aah5, SoundSource.BLOCKS, 1.0f, 1.0f);
    }
}
