package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.block.state.StateDefinition;
import java.util.Iterator;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.block.piston.MovingPistonBlock;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class FarmBlock extends Block {
    public static final IntegerProperty MOISTURE;
    protected static final VoxelShape SHAPE;
    
    protected FarmBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)FarmBlock.MOISTURE, 0));
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (fz == Direction.UP && !cfj1.canSurvive(bqc, fu5)) {
            bqc.getBlockTicks().scheduleTick(fu5, this, 1);
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        final BlockState cfj2 = bqd.getBlockState(fu.above());
        return !cfj2.getMaterial().isSolid() || cfj2.getBlock() instanceof FenceGateBlock || cfj2.getBlock() instanceof MovingPistonBlock;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        if (!this.defaultBlockState().canSurvive(bin.getLevel(), bin.getClickedPos())) {
            return Blocks.DIRT.defaultBlockState();
        }
        return super.getStateForPlacement(bin);
    }
    
    @Override
    public boolean useShapeForLightOcclusion(final BlockState cfj) {
        return true;
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return FarmBlock.SHAPE;
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (!cfj.canSurvive(zd, fu)) {
            turnToDirt(cfj, zd, fu);
        }
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        final int integer6 = cfj.<Integer>getValue((Property<Integer>)FarmBlock.MOISTURE);
        if (isNearWater(zd, fu) || zd.isRainingAt(fu.above())) {
            if (integer6 < 7) {
                zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)FarmBlock.MOISTURE, 7), 2);
            }
        }
        else if (integer6 > 0) {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)FarmBlock.MOISTURE, integer6 - 1), 2);
        }
        else if (!isUnderCrops(zd, fu)) {
            turnToDirt(cfj, zd, fu);
        }
    }
    
    @Override
    public void fallOn(final Level bqb, final BlockPos fu, final Entity aom, final float float4) {
        if (!bqb.isClientSide && bqb.random.nextFloat() < float4 - 0.5f && aom instanceof LivingEntity && (aom instanceof Player || bqb.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) && aom.getBbWidth() * aom.getBbWidth() * aom.getBbHeight() > 0.512f) {
            turnToDirt(bqb.getBlockState(fu), bqb, fu);
        }
        super.fallOn(bqb, fu, aom, float4);
    }
    
    public static void turnToDirt(final BlockState cfj, final Level bqb, final BlockPos fu) {
        bqb.setBlockAndUpdate(fu, Block.pushEntitiesUp(cfj, Blocks.DIRT.defaultBlockState(), bqb, fu));
    }
    
    private static boolean isUnderCrops(final BlockGetter bpg, final BlockPos fu) {
        final Block bvr3 = bpg.getBlockState(fu.above()).getBlock();
        return bvr3 instanceof CropBlock || bvr3 instanceof StemBlock || bvr3 instanceof AttachedStemBlock;
    }
    
    private static boolean isNearWater(final LevelReader bqd, final BlockPos fu) {
        for (final BlockPos fu2 : BlockPos.betweenClosed(fu.offset(-4, 0, -4), fu.offset(4, 1, 4))) {
            if (bqd.getFluidState(fu2).is(FluidTags.WATER)) {
                return true;
            }
        }
        return false;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(FarmBlock.MOISTURE);
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    static {
        MOISTURE = BlockStateProperties.MOISTURE;
        SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 15.0, 16.0);
    }
}
