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.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.EntityType;
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.ItemLike;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
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 SweetBerryBushBlock extends BushBlock implements BonemealableBlock {
    public static final IntegerProperty AGE;
    private static final VoxelShape SAPLING_SHAPE;
    private static final VoxelShape MID_GROWTH_SHAPE;
    
    public SweetBerryBushBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)SweetBerryBushBlock.AGE, 0));
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bpg, final BlockPos fu, final BlockState cfj) {
        return new ItemStack(Items.SWEET_BERRIES);
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        if (cfj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) == 0) {
            return SweetBerryBushBlock.SAPLING_SHAPE;
        }
        if (cfj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) < 3) {
            return SweetBerryBushBlock.MID_GROWTH_SHAPE;
        }
        return super.getShape(cfj, bpg, fu, der);
    }
    
    @Override
    public boolean isRandomlyTicking(final BlockState cfj) {
        return cfj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) < 3;
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        final int integer6 = cfj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE);
        if (integer6 < 3 && random.nextInt(5) == 0 && zd.getRawBrightness(fu.above(), 0) >= 9) {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)SweetBerryBushBlock.AGE, integer6 + 1), 2);
        }
    }
    
    @Override
    public void entityInside(final BlockState cfj, final Level bqb, final BlockPos fu, final Entity aom) {
        if (!(aom instanceof LivingEntity) || aom.getType() == EntityType.FOX || aom.getType() == EntityType.BEE) {
            return;
        }
        aom.makeStuckInBlock(cfj, new Vec3(0.800000011920929, 0.75, 0.800000011920929));
        if (!bqb.isClientSide && cfj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) > 0 && (aom.xOld != aom.getX() || aom.zOld != aom.getZ())) {
            final double double6 = Math.abs(aom.getX() - aom.xOld);
            final double double7 = Math.abs(aom.getZ() - aom.zOld);
            if (double6 >= 0.003000000026077032 || double7 >= 0.003000000026077032) {
                aom.hurt(DamageSource.SWEET_BERRY_BUSH, 1.0f);
            }
        }
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        final int integer8 = cfj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE);
        final boolean boolean9 = integer8 == 3;
        if (!boolean9 && bec.getItemInHand(anf).getItem() == Items.BONE_MEAL) {
            return InteractionResult.PASS;
        }
        if (integer8 > 1) {
            final int integer9 = 1 + bqb.random.nextInt(2);
            Block.popResource(bqb, fu, new ItemStack(Items.SWEET_BERRIES, integer9 + (boolean9 ? 1 : 0)));
            bqb.playSound(null, fu, SoundEvents.SWEET_BERRY_BUSH_PICK_BERRIES, SoundSource.BLOCKS, 1.0f, 0.8f + bqb.random.nextFloat() * 0.4f);
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)SweetBerryBushBlock.AGE, 1), 2);
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        return super.use(cfj, bqb, fu, bec, anf, deh);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(SweetBerryBushBlock.AGE);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bpg, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        return cfj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) < 3;
    }
    
    @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(3, cfj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) + 1);
        zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)SweetBerryBushBlock.AGE, integer6), 2);
    }
    
    static {
        AGE = BlockStateProperties.AGE_3;
        SAPLING_SHAPE = Block.box(3.0, 0.0, 3.0, 13.0, 8.0, 13.0);
        MID_GROWTH_SHAPE = Block.box(1.0, 0.0, 1.0, 15.0, 16.0, 15.0);
    }
}
