package net.minecraft.world.item;

import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.block.Block;
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.server.level.ServerPlayer;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.world.level.ItemLike;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.Tag;
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 cwz, final Properties a) {
        super(a);
        this.content = cwz;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final ItemStack bki5 = bec.getItemInHand(anf);
        final HitResult dej6 = Item.getPlayerPOVHitResult(bqb, bec, (this.content == Fluids.EMPTY) ? ClipContext.Fluid.SOURCE_ONLY : ClipContext.Fluid.NONE);
        if (dej6.getType() == HitResult.Type.MISS) {
            return InteractionResultHolder.<ItemStack>pass(bki5);
        }
        if (dej6.getType() != HitResult.Type.BLOCK) {
            return InteractionResultHolder.<ItemStack>pass(bki5);
        }
        final BlockHitResult deh7 = (BlockHitResult)dej6;
        final BlockPos fu8 = deh7.getBlockPos();
        final Direction fz9 = deh7.getDirection();
        final BlockPos fu9 = fu8.relative(fz9);
        if (!bqb.mayInteract(bec, fu8) || !bec.mayUseItemAt(fu9, fz9, bki5)) {
            return InteractionResultHolder.<ItemStack>fail(bki5);
        }
        if (this.content == Fluids.EMPTY) {
            final BlockState cfj11 = bqb.getBlockState(fu8);
            if (cfj11.getBlock() instanceof BucketPickup) {
                final Fluid cwz12 = ((BucketPickup)cfj11.getBlock()).takeLiquid(bqb, fu8, cfj11);
                if (cwz12 != Fluids.EMPTY) {
                    bec.awardStat(Stats.ITEM_USED.get(this));
                    bec.playSound(cwz12.is(FluidTags.LAVA) ? SoundEvents.BUCKET_FILL_LAVA : SoundEvents.BUCKET_FILL, 1.0f, 1.0f);
                    final ItemStack bki6 = ItemUtils.createBucketResult(bki5, bec, new ItemStack(cwz12.getBucket()));
                    if (!bqb.isClientSide) {
                        CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayer)bec, new ItemStack(cwz12.getBucket()));
                    }
                    return InteractionResultHolder.<ItemStack>sidedSuccess(bki6, bqb.isClientSide());
                }
            }
            return InteractionResultHolder.<ItemStack>fail(bki5);
        }
        final BlockState cfj11 = bqb.getBlockState(fu8);
        final BlockPos fu10 = (cfj11.getBlock() instanceof LiquidBlockContainer && this.content == Fluids.WATER) ? fu8 : fu9;
        if (this.emptyBucket(bec, bqb, fu10, deh7)) {
            this.checkExtraContent(bqb, bki5, fu10);
            if (bec instanceof ServerPlayer) {
                CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer)bec, fu10, bki5);
            }
            bec.awardStat(Stats.ITEM_USED.get(this));
            return InteractionResultHolder.<ItemStack>sidedSuccess(this.getEmptySuccessItem(bki5, bec), bqb.isClientSide());
        }
        return InteractionResultHolder.<ItemStack>fail(bki5);
    }
    
    protected ItemStack getEmptySuccessItem(final ItemStack bki, final Player bec) {
        if (!bec.abilities.instabuild) {
            return new ItemStack(Items.BUCKET);
        }
        return bki;
    }
    
    public void checkExtraContent(final Level bqb, final ItemStack bki, final BlockPos fu) {
    }
    
    public boolean emptyBucket(@Nullable final Player bec, final Level bqb, final BlockPos fu, @Nullable final BlockHitResult deh) {
        if (!(this.content instanceof FlowingFluid)) {
            return false;
        }
        final BlockState cfj6 = bqb.getBlockState(fu);
        final Block bvr7 = cfj6.getBlock();
        final Material cxd8 = cfj6.getMaterial();
        final boolean boolean9 = cfj6.canBeReplaced(this.content);
        final boolean boolean10 = cfj6.isAir() || boolean9 || (bvr7 instanceof LiquidBlockContainer && ((LiquidBlockContainer)bvr7).canPlaceLiquid(bqb, fu, cfj6, this.content));
        if (!boolean10) {
            return deh != null && this.emptyBucket(bec, bqb, deh.getBlockPos().relative(deh.getDirection()), null);
        }
        if (bqb.dimensionType().ultraWarm() && this.content.is(FluidTags.WATER)) {
            final int integer11 = fu.getX();
            final int integer12 = fu.getY();
            final int integer13 = fu.getZ();
            bqb.playSound(bec, fu, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5f, 2.6f + (bqb.random.nextFloat() - bqb.random.nextFloat()) * 0.8f);
            for (int integer14 = 0; integer14 < 8; ++integer14) {
                bqb.addParticle(ParticleTypes.LARGE_SMOKE, integer11 + Math.random(), integer12 + Math.random(), integer13 + Math.random(), 0.0, 0.0, 0.0);
            }
            return true;
        }
        if (bvr7 instanceof LiquidBlockContainer && this.content == Fluids.WATER) {
            ((LiquidBlockContainer)bvr7).placeLiquid(bqb, fu, cfj6, ((FlowingFluid)this.content).getSource(false));
            this.playEmptySound(bec, bqb, fu);
            return true;
        }
        if (!bqb.isClientSide && boolean9 && !cxd8.isLiquid()) {
            bqb.destroyBlock(fu, true);
        }
        if (bqb.setBlock(fu, this.content.defaultFluidState().createLegacyBlock(), 11) || cfj6.getFluidState().isSource()) {
            this.playEmptySound(bec, bqb, fu);
            return true;
        }
        return false;
    }
    
    protected void playEmptySound(@Nullable final Player bec, final LevelAccessor bqc, final BlockPos fu) {
        final SoundEvent ack5 = this.content.is(FluidTags.LAVA) ? SoundEvents.BUCKET_EMPTY_LAVA : SoundEvents.BUCKET_EMPTY;
        bqc.playSound(bec, fu, ack5, SoundSource.BLOCKS, 1.0f, 1.0f);
    }
}
