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.block.state.StateDefinition;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.entity.monster.Ravager;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.LevelReader;
import net.minecraft.util.Mth;
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.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 CropBlock extends BushBlock implements BonemealableBlock {
    public static final IntegerProperty AGE;
    private static final VoxelShape[] SHAPE_BY_AGE;
    
    protected CropBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)this.getAgeProperty(), 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return CropBlock.SHAPE_BY_AGE[byj.<Integer>getValue((Property<Integer>)this.getAgeProperty())];
    }
    
    @Override
    protected boolean mayPlaceOn(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return byj.getBlock() == Blocks.FARMLAND;
    }
    
    public IntegerProperty getAgeProperty() {
        return CropBlock.AGE;
    }
    
    public int getMaxAge() {
        return 7;
    }
    
    protected int getAge(final BlockState byj) {
        return byj.<Integer>getValue((Property<Integer>)this.getAgeProperty());
    }
    
    public BlockState getStateForAge(final int integer) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)this.getAgeProperty(), integer);
    }
    
    public boolean isMaxAge(final BlockState byj) {
        return byj.<Integer>getValue((Property<Integer>)this.getAgeProperty()) >= this.getMaxAge();
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        super.tick(byj, xd, fk, random);
        if (xd.getRawBrightness(fk, 0) >= 9) {
            final int integer6 = this.getAge(byj);
            if (integer6 < this.getMaxAge()) {
                final float float7 = getGrowthSpeed(this, xd, fk);
                if (random.nextInt((int)(25.0f / float7) + 1) == 0) {
                    xd.setBlock(fk, this.getStateForAge(integer6 + 1), 2);
                }
            }
        }
    }
    
    public void growCrops(final Level bjw, final BlockPos fk, final BlockState byj) {
        int integer5 = this.getAge(byj) + this.getBonemealAgeIncrease(bjw);
        final int integer6 = this.getMaxAge();
        if (integer5 > integer6) {
            integer5 = integer6;
        }
        bjw.setBlock(fk, this.getStateForAge(integer5), 2);
    }
    
    protected int getBonemealAgeIncrease(final Level bjw) {
        return Mth.nextInt(bjw.random, 2, 5);
    }
    
    protected static float getGrowthSpeed(final Block bph, final BlockGetter bjg, final BlockPos fk) {
        float float4 = 1.0f;
        final BlockPos fk2 = fk.below();
        for (int integer6 = -1; integer6 <= 1; ++integer6) {
            for (int integer7 = -1; integer7 <= 1; ++integer7) {
                float float5 = 0.0f;
                final BlockState byj9 = bjg.getBlockState(fk2.offset(integer6, 0, integer7));
                if (byj9.getBlock() == Blocks.FARMLAND) {
                    float5 = 1.0f;
                    if (byj9.<Integer>getValue((Property<Integer>)FarmBlock.MOISTURE) > 0) {
                        float5 = 3.0f;
                    }
                }
                if (integer6 != 0 || integer7 != 0) {
                    float5 /= 4.0f;
                }
                float4 += float5;
            }
        }
        final BlockPos fk3 = fk.north();
        final BlockPos fk4 = fk.south();
        final BlockPos fk5 = fk.west();
        final BlockPos fk6 = fk.east();
        final boolean boolean10 = bph == bjg.getBlockState(fk5).getBlock() || bph == bjg.getBlockState(fk6).getBlock();
        final boolean boolean11 = bph == bjg.getBlockState(fk3).getBlock() || bph == bjg.getBlockState(fk4).getBlock();
        if (boolean10 && boolean11) {
            float4 /= 2.0f;
        }
        else {
            final boolean boolean12 = bph == bjg.getBlockState(fk5.north()).getBlock() || bph == bjg.getBlockState(fk6.north()).getBlock() || bph == bjg.getBlockState(fk6.south()).getBlock() || bph == bjg.getBlockState(fk5.south()).getBlock();
            if (boolean12) {
                float4 /= 2.0f;
            }
        }
        return float4;
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        return (bjz.getRawBrightness(fk, 0) >= 8 || bjz.canSeeSky(fk)) && super.canSurvive(byj, bjz, fk);
    }
    
    @Override
    public void entityInside(final BlockState byj, final Level bjw, final BlockPos fk, final Entity akq) {
        if (akq instanceof Ravager && bjw.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
            bjw.destroyBlock(fk, true, akq);
        }
        super.entityInside(byj, bjw, fk, akq);
    }
    
    protected ItemLike getBaseSeedId() {
        return Items.WHEAT_SEEDS;
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bjg, final BlockPos fk, final BlockState byj) {
        return new ItemStack(this.getBaseSeedId());
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        return !this.isMaxAge(byj);
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bjw, final Random random, final BlockPos fk, final BlockState byj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel xd, final Random random, final BlockPos fk, final BlockState byj) {
        this.growCrops(xd, fk, byj);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(CropBlock.AGE);
    }
    
    static {
        AGE = BlockStateProperties.AGE_7;
        SHAPE_BY_AGE = new VoxelShape[] { Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 4.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 6.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 8.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 10.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 12.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 14.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 16.0) };
    }
}
