package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.util.RandomSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.level.gameevent.GameEvent.Context; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; public class SweetBerryBushBlock extends BushBlock implements BonemealableBlock { public static final MapCodec CODEC = simpleCodec(SweetBerryBushBlock::new); private static final float HURT_SPEED_THRESHOLD = 0.003F; public static final int MAX_AGE = 3; public static final IntegerProperty AGE = BlockStateProperties.AGE_3; private static final VoxelShape SAPLING_SHAPE = Block.box(3.0, 0.0, 3.0, 13.0, 8.0, 13.0); private static final VoxelShape MID_GROWTH_SHAPE = Block.box(1.0, 0.0, 1.0, 15.0, 16.0, 15.0); @Override public MapCodec codec() { return CODEC; } public SweetBerryBushBlock(BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0)); } @Override public ItemStack getCloneItemStack(LevelReader level, BlockPos pos, BlockState state) { return new ItemStack(Items.SWEET_BERRIES); } @Override protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { if ((Integer)state.getValue(AGE) == 0) { return SAPLING_SHAPE; } else { return state.getValue(AGE) < 3 ? MID_GROWTH_SHAPE : super.getShape(state, level, pos, context); } } @Override protected boolean isRandomlyTicking(BlockState state) { return (Integer)state.getValue(AGE) < 3; } @Override protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) { int i = (Integer)state.getValue(AGE); if (i < 3 && random.nextInt(5) == 0 && level.getRawBrightness(pos.above(), 0) >= 9) { BlockState blockState = state.setValue(AGE, i + 1); level.setBlock(pos, blockState, 2); level.gameEvent(GameEvent.BLOCK_CHANGE, pos, Context.of(blockState)); } } @Override protected void entityInside(BlockState state, Level level, BlockPos pos, Entity entity) { if (entity instanceof LivingEntity && entity.getType() != EntityType.FOX && entity.getType() != EntityType.BEE) { entity.makeStuckInBlock(state, new Vec3(0.8F, 0.75, 0.8F)); if (level instanceof ServerLevel serverLevel && (Integer)state.getValue(AGE) != 0) { Vec3 vec3 = entity.isControlledByClient() ? entity.getKnownMovement() : entity.oldPosition().subtract(entity.position()); if (vec3.horizontalDistanceSqr() > 0.0) { double d = Math.abs(vec3.x()); double e = Math.abs(vec3.z()); if (d >= 0.003F || e >= 0.003F) { entity.hurtServer(serverLevel, level.damageSources().sweetBerryBush(), 1.0F); } } } } } @Override protected InteractionResult useItemOn( ItemStack itemStack, BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult ) { int i = (Integer)blockState.getValue(AGE); boolean bl = i == 3; return (InteractionResult)(!bl && itemStack.is(Items.BONE_MEAL) ? InteractionResult.PASS : super.useItemOn(itemStack, blockState, level, blockPos, player, interactionHand, blockHitResult)); } @Override protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) { int i = (Integer)state.getValue(AGE); boolean bl = i == 3; if (i > 1) { int j = 1 + level.random.nextInt(2); popResource(level, pos, new ItemStack(Items.SWEET_BERRIES, j + (bl ? 1 : 0))); level.playSound(null, pos, SoundEvents.SWEET_BERRY_BUSH_PICK_BERRIES, SoundSource.BLOCKS, 1.0F, 0.8F + level.random.nextFloat() * 0.4F); BlockState blockState = state.setValue(AGE, 1); level.setBlock(pos, blockState, 2); level.gameEvent(GameEvent.BLOCK_CHANGE, pos, Context.of(player, blockState)); return InteractionResult.SUCCESS; } else { return super.useWithoutItem(state, level, pos, player, hitResult); } } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { builder.add(AGE); } @Override public boolean isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) { return (Integer)state.getValue(AGE) < 3; } @Override public boolean isBonemealSuccess(Level level, RandomSource random, BlockPos pos, BlockState state) { return true; } @Override public void performBonemeal(ServerLevel level, RandomSource random, BlockPos pos, BlockState state) { int i = Math.min(3, (Integer)state.getValue(AGE) + 1); level.setBlock(pos, state.setValue(AGE, i), 2); } }