package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelReader;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.material.Fluids;
import javax.annotation.Nullable;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class KelpBlock extends Block implements LiquidBlockContainer {
    public static final IntegerProperty AGE;
    protected static final VoxelShape SHAPE;
    
    protected KelpBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)KelpBlock.AGE, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return KelpBlock.SHAPE;
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final FluidState coj3 = bcq.getLevel().getFluidState(bcq.getClickedPos());
        if (coj3.is(FluidTags.WATER) && coj3.getAmount() == 8) {
            return this.getStateForPlacement(bcq.getLevel());
        }
        return null;
    }
    
    public BlockState getStateForPlacement(final LevelAccessor bjx) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)KelpBlock.AGE, bjx.getRandom().nextInt(25));
    }
    
    @Override
    public FluidState getFluidState(final BlockState byj) {
        return Fluids.WATER.getSource(false);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (!byj.canSurvive(xd, fk)) {
            xd.destroyBlock(fk, true);
            return;
        }
        final BlockPos fk2 = fk.above();
        final BlockState byj2 = xd.getBlockState(fk2);
        if (byj2.getBlock() == Blocks.WATER && byj.<Integer>getValue((Property<Integer>)KelpBlock.AGE) < 25 && random.nextDouble() < 0.14) {
            xd.setBlockAndUpdate(fk2, ((AbstractStateHolder<O, BlockState>)byj).<Comparable>cycle((Property<Comparable>)KelpBlock.AGE));
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockPos fk2 = fk.below();
        final BlockState byj2 = bjz.getBlockState(fk2);
        final Block bph7 = byj2.getBlock();
        return bph7 != Blocks.MAGMA_BLOCK && (bph7 == this || bph7 == Blocks.KELP_PLANT || byj2.isFaceSturdy(bjz, fk2, Direction.UP));
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (!byj1.canSurvive(bjx, fk5)) {
            if (fp == Direction.DOWN) {
                return Blocks.AIR.defaultBlockState();
            }
            bjx.getBlockTicks().scheduleTick(fk5, this, 1);
        }
        if (fp == Direction.UP && byj3.getBlock() == this) {
            return Blocks.KELP_PLANT.defaultBlockState();
        }
        bjx.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bjx));
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(KelpBlock.AGE);
    }
    
    @Override
    public boolean canPlaceLiquid(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final Fluid coi) {
        return false;
    }
    
    @Override
    public boolean placeLiquid(final LevelAccessor bjx, final BlockPos fk, final BlockState byj, final FluidState coj) {
        return false;
    }
    
    static {
        AGE = BlockStateProperties.AGE_25;
        SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 9.0, 16.0);
    }
}
