package net.minecraft.world.level.block;

import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.material.FluidState;
import java.util.Queue;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
import net.minecraft.tags.FluidTags;
import net.minecraft.core.Direction;
import net.minecraft.util.Tuple;
import com.google.common.collect.Lists;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;

public class SpongeBlock extends Block {
    protected SpongeBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj4.is(cfj1.getBlock())) {
            return;
        }
        this.tryAbsorbWater(bqb, fu);
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        this.tryAbsorbWater(bqb, fu3);
        super.neighborChanged(cfj, bqb, fu3, bvr, fu5, boolean6);
    }
    
    protected void tryAbsorbWater(final Level bqb, final BlockPos fu) {
        if (this.removeWaterBreadthFirstSearch(bqb, fu)) {
            bqb.setBlock(fu, Blocks.WET_SPONGE.defaultBlockState(), 2);
            bqb.levelEvent(2001, fu, Block.getId(Blocks.WATER.defaultBlockState()));
        }
    }
    
    private boolean removeWaterBreadthFirstSearch(final Level bqb, final BlockPos fu) {
        final Queue<Tuple<BlockPos, Integer>> queue4 = Lists.newLinkedList();
        queue4.add(new Tuple<BlockPos, Integer>(fu, 0));
        int integer5 = 0;
        while (!queue4.isEmpty()) {
            final Tuple<BlockPos, Integer> aek6 = queue4.poll();
            final BlockPos fu2 = aek6.getA();
            final int integer6 = aek6.getB();
            for (final Direction fz12 : Direction.values()) {
                final BlockPos fu3 = fu2.relative(fz12);
                final BlockState cfj14 = bqb.getBlockState(fu3);
                final FluidState cxa15 = bqb.getFluidState(fu3);
                final Material cxd16 = cfj14.getMaterial();
                if (cxa15.is(FluidTags.WATER)) {
                    if (cfj14.getBlock() instanceof BucketPickup && ((BucketPickup)cfj14.getBlock()).takeLiquid(bqb, fu3, cfj14) != Fluids.EMPTY) {
                        ++integer5;
                        if (integer6 < 6) {
                            queue4.add(new Tuple<BlockPos, Integer>(fu3, integer6 + 1));
                        }
                    }
                    else if (cfj14.getBlock() instanceof LiquidBlock) {
                        bqb.setBlock(fu3, Blocks.AIR.defaultBlockState(), 3);
                        ++integer5;
                        if (integer6 < 6) {
                            queue4.add(new Tuple<BlockPos, Integer>(fu3, integer6 + 1));
                        }
                    }
                    else if (cxd16 == Material.WATER_PLANT || cxd16 == Material.REPLACEABLE_WATER_PLANT) {
                        final BlockEntity cdl17 = cfj14.getBlock().isEntityBlock() ? bqb.getBlockEntity(fu3) : null;
                        Block.dropResources(cfj14, bqb, fu3, cdl17);
                        bqb.setBlock(fu3, Blocks.AIR.defaultBlockState(), 3);
                        ++integer5;
                        if (integer6 < 6) {
                            queue4.add(new Tuple<BlockPos, Integer>(fu3, integer6 + 1));
                        }
                    }
                }
            }
            if (integer5 > 64) {
                break;
            }
        }
        return integer5 > 0;
    }
}
