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.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;

public class SpongeBlock extends Block {
    protected SpongeBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj4.getBlock() == byj1.getBlock()) {
            return;
        }
        this.tryAbsorbWater(bjw, fk);
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        this.tryAbsorbWater(bjw, fk3);
        super.neighborChanged(byj, bjw, fk3, bph, fk5, boolean6);
    }
    
    protected void tryAbsorbWater(final Level bjw, final BlockPos fk) {
        if (this.removeWaterBreadthFirstSearch(bjw, fk)) {
            bjw.setBlock(fk, Blocks.WET_SPONGE.defaultBlockState(), 2);
            bjw.levelEvent(2001, fk, Block.getId(Blocks.WATER.defaultBlockState()));
        }
    }
    
    private boolean removeWaterBreadthFirstSearch(final Level bjw, final BlockPos fk) {
        final Queue<Tuple<BlockPos, Integer>> queue4 = Lists.newLinkedList();
        queue4.add(new Tuple<BlockPos, Integer>(fk, 0));
        int integer5 = 0;
        while (!queue4.isEmpty()) {
            final Tuple<BlockPos, Integer> abz6 = queue4.poll();
            final BlockPos fk2 = abz6.getA();
            final int integer6 = abz6.getB();
            for (final Direction fp12 : Direction.values()) {
                final BlockPos fk3 = fk2.relative(fp12);
                final BlockState byj14 = bjw.getBlockState(fk3);
                final FluidState coj15 = bjw.getFluidState(fk3);
                final Material coo16 = byj14.getMaterial();
                if (coj15.is(FluidTags.WATER)) {
                    if (byj14.getBlock() instanceof BucketPickup && ((BucketPickup)byj14.getBlock()).takeLiquid(bjw, fk3, byj14) != Fluids.EMPTY) {
                        ++integer5;
                        if (integer6 < 6) {
                            queue4.add(new Tuple<BlockPos, Integer>(fk3, integer6 + 1));
                        }
                    }
                    else if (byj14.getBlock() instanceof LiquidBlock) {
                        bjw.setBlock(fk3, Blocks.AIR.defaultBlockState(), 3);
                        ++integer5;
                        if (integer6 < 6) {
                            queue4.add(new Tuple<BlockPos, Integer>(fk3, integer6 + 1));
                        }
                    }
                    else if (coo16 == Material.WATER_PLANT || coo16 == Material.REPLACEABLE_WATER_PLANT) {
                        final BlockEntity bwl17 = byj14.getBlock().isEntityBlock() ? bjw.getBlockEntity(fk3) : null;
                        Block.dropResources(byj14, bjw, fk3, bwl17);
                        bjw.setBlock(fk3, Blocks.AIR.defaultBlockState(), 3);
                        ++integer5;
                        if (integer6 < 6) {
                            queue4.add(new Tuple<BlockPos, Integer>(fk3, integer6 + 1));
                        }
                    }
                }
            }
            if (integer5 > 64) {
                break;
            }
        }
        return integer5 > 0;
    }
}
