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.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.level.block.state.BlockBehaviour;
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 cbq, final Properties c) {
        super(c);
        this.fruit = cbq;
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return StemBlock.SHAPE_BY_AGE[cfj.<Integer>getValue((Property<Integer>)StemBlock.AGE)];
    }
    
    @Override
    protected boolean mayPlaceOn(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return cfj.is(Blocks.FARMLAND);
    }
    
    @Override
    public void randomTick(BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (zd.getRawBrightness(fu, 0) < 9) {
            return;
        }
        final float float6 = CropBlock.getGrowthSpeed(this, zd, fu);
        if (random.nextInt((int)(25.0f / float6) + 1) == 0) {
            final int integer7 = cfj.<Integer>getValue((Property<Integer>)StemBlock.AGE);
            if (integer7 < 7) {
                cfj = ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, integer7 + 1);
                zd.setBlock(fu, cfj, 2);
            }
            else {
                final Direction fz8 = Direction.Plane.HORIZONTAL.getRandomDirection(random);
                final BlockPos fu2 = fu.relative(fz8);
                final BlockState cfj2 = zd.getBlockState(fu2.below());
                if (zd.getBlockState(fu2).isAir() && (cfj2.is(Blocks.FARMLAND) || cfj2.is(Blocks.DIRT) || cfj2.is(Blocks.COARSE_DIRT) || cfj2.is(Blocks.PODZOL) || cfj2.is(Blocks.GRASS_BLOCK))) {
                    zd.setBlockAndUpdate(fu2, this.fruit.defaultBlockState());
                    zd.setBlockAndUpdate(fu, ((StateHolder<O, BlockState>)this.fruit.getAttachedStem().defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)HorizontalDirectionalBlock.FACING, fz8));
                }
            }
        }
    }
    
    @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 bpg, final BlockPos fu, final BlockState cfj) {
        final Item bke5 = this.getSeedItem();
        return (bke5 == null) ? ItemStack.EMPTY : new ItemStack(bke5);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bpg, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        return cfj.<Integer>getValue((Property<Integer>)StemBlock.AGE) != 7;
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bqb, final Random random, final BlockPos fu, final BlockState cfj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel zd, final Random random, final BlockPos fu, final BlockState cfj) {
        final int integer6 = Math.min(7, cfj.<Integer>getValue((Property<Integer>)StemBlock.AGE) + Mth.nextInt(zd.random, 2, 5));
        final BlockState cfj2 = ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, integer6);
        zd.setBlock(fu, cfj2, 2);
        if (integer6 == 7) {
            cfj2.randomTick(zd, fu, zd.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) };
    }
}
