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.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.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(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)SweetBerryBushBlock.AGE, 0));
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bjg, final BlockPos fk, final BlockState byj) {
        return new ItemStack(Items.SWEET_BERRIES);
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        if (byj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) == 0) {
            return SweetBerryBushBlock.SAPLING_SHAPE;
        }
        if (byj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) < 3) {
            return SweetBerryBushBlock.MID_GROWTH_SHAPE;
        }
        return super.getShape(byj, bjg, fk, cvq);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        super.tick(byj, xd, fk, random);
        final int integer6 = byj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE);
        if (integer6 < 3 && random.nextInt(5) == 0 && xd.getRawBrightness(fk.above(), 0) >= 9) {
            xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)SweetBerryBushBlock.AGE, integer6 + 1), 2);
        }
    }
    
    @Override
    public void entityInside(final BlockState byj, final Level bjw, final BlockPos fk, final Entity akq) {
        if (!(akq instanceof LivingEntity) || akq.getType() == EntityType.FOX || akq.getType() == EntityType.BEE) {
            return;
        }
        akq.makeStuckInBlock(byj, new Vec3(0.800000011920929, 0.75, 0.800000011920929));
        if (!bjw.isClientSide && byj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) > 0 && (akq.xOld != akq.getX() || akq.zOld != akq.getZ())) {
            final double double6 = Math.abs(akq.getX() - akq.xOld);
            final double double7 = Math.abs(akq.getZ() - akq.zOld);
            if (double6 >= 0.003000000026077032 || double7 >= 0.003000000026077032) {
                akq.hurt(DamageSource.SWEET_BERRY_BUSH, 1.0f);
            }
        }
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        final int integer8 = byj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE);
        final boolean boolean9 = integer8 == 3;
        if (!boolean9 && ayj.getItemInHand(ajk).getItem() == Items.BONE_MEAL) {
            return InteractionResult.PASS;
        }
        if (integer8 > 1) {
            final int integer9 = 1 + bjw.random.nextInt(2);
            Block.popResource(bjw, fk, new ItemStack(Items.SWEET_BERRIES, integer9 + (boolean9 ? 1 : 0)));
            bjw.playSound(null, fk, SoundEvents.SWEET_BERRY_BUSH_PICK_BERRIES, SoundSource.BLOCKS, 1.0f, 0.8f + bjw.random.nextFloat() * 0.4f);
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)SweetBerryBushBlock.AGE, 1), 2);
            return InteractionResult.SUCCESS;
        }
        return super.use(byj, bjw, fk, ayj, ajk, cvg);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(SweetBerryBushBlock.AGE);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        return byj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) < 3;
    }
    
    @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(3, byj.<Integer>getValue((Property<Integer>)SweetBerryBushBlock.AGE) + 1);
        xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<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);
    }
}
