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.util.Mth;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Item;
import net.minecraft.core.Direction;
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 StemBlock extends BushBlock implements BonemealableBlock {
    public static final IntegerProperty AGE;
    protected static final VoxelShape[] SHAPE_BY_AGE;
    private final StemGrownBlock fruit;
    
    protected StemBlock(final StemGrownBlock buv, final Properties c) {
        super(c);
        this.fruit = buv;
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return StemBlock.SHAPE_BY_AGE[byj.<Integer>getValue((Property<Integer>)StemBlock.AGE)];
    }
    
    @Override
    protected boolean mayPlaceOn(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return byj.getBlock() == Blocks.FARMLAND;
    }
    
    @Override
    public void tick(BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        super.tick(byj, xd, fk, random);
        if (xd.getRawBrightness(fk, 0) < 9) {
            return;
        }
        final float float6 = CropBlock.getGrowthSpeed(this, xd, fk);
        if (random.nextInt((int)(25.0f / float6) + 1) == 0) {
            final int integer7 = byj.<Integer>getValue((Property<Integer>)StemBlock.AGE);
            if (integer7 < 7) {
                byj = ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, integer7 + 1);
                xd.setBlock(fk, byj, 2);
            }
            else {
                final Direction fp8 = Direction.Plane.HORIZONTAL.getRandomDirection(random);
                final BlockPos fk2 = fk.relative(fp8);
                final Block bph10 = xd.getBlockState(fk2.below()).getBlock();
                if (xd.getBlockState(fk2).isAir() && (bph10 == Blocks.FARMLAND || bph10 == Blocks.DIRT || bph10 == Blocks.COARSE_DIRT || bph10 == Blocks.PODZOL || bph10 == Blocks.GRASS_BLOCK)) {
                    xd.setBlockAndUpdate(fk2, this.fruit.defaultBlockState());
                    xd.setBlockAndUpdate(fk, ((AbstractStateHolder<O, BlockState>)this.fruit.getAttachedStem().defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)HorizontalDirectionalBlock.FACING, fp8));
                }
            }
        }
    }
    
    @Nullable
    protected Item getSeedItem() {
        if (this.fruit == Blocks.PUMPKIN) {
            return Items.PUMPKIN_SEEDS;
        }
        if (this.fruit == Blocks.MELON) {
            return Items.MELON_SEEDS;
        }
        return null;
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bjg, final BlockPos fk, final BlockState byj) {
        final Item bei5 = this.getSeedItem();
        return (bei5 == null) ? ItemStack.EMPTY : new ItemStack(bei5);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        return byj.<Integer>getValue((Property<Integer>)StemBlock.AGE) != 7;
    }
    
    @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) {
        final int integer6 = Math.min(7, byj.<Integer>getValue((Property<Integer>)StemBlock.AGE) + Mth.nextInt(xd.random, 2, 5));
        final BlockState byj2 = ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, integer6);
        xd.setBlock(fk, byj2, 2);
        if (integer6 == 7) {
            byj2.tick(xd, fk, xd.random);
        }
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(StemBlock.AGE);
    }
    
    public StemGrownBlock getFruit() {
        return this.fruit;
    }
    
    static {
        AGE = BlockStateProperties.AGE_7;
        SHAPE_BY_AGE = new VoxelShape[] { Block.box(7.0, 0.0, 7.0, 9.0, 2.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 4.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 6.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 8.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 10.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 12.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 14.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 16.0, 9.0) };
    }
}
