package net.minecraft.world.level.block;

import javax.annotation.Nullable;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.BlockState;

public class CoralBlock extends Block {
    private final Block deadBlock;
    
    public CoralBlock(final Block bph, final Properties c) {
        super(c);
        this.deadBlock = bph;
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (!this.scanForWater(xd, fk)) {
            xd.setBlock(fk, this.deadBlock.defaultBlockState(), 2);
        }
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (!this.scanForWater(bjx, fk5)) {
            bjx.getBlockTicks().scheduleTick(fk5, this, 60 + bjx.getRandom().nextInt(40));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    protected boolean scanForWater(final BlockGetter bjg, final BlockPos fk) {
        for (final Direction fp7 : Direction.values()) {
            final FluidState coj8 = bjg.getFluidState(fk.relative(fp7));
            if (coj8.is(FluidTags.WATER)) {
                return true;
            }
        }
        return false;
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        if (!this.scanForWater(bcq.getLevel(), bcq.getClickedPos())) {
            bcq.getLevel().getBlockTicks().scheduleTick(bcq.getClickedPos(), this, 60 + bcq.getLevel().getRandom().nextInt(40));
        }
        return this.defaultBlockState();
    }
}
