| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.sounds.SoundSource;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.InteractionHand;
|
| 9 | import net.minecraft.world.InteractionResult;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.EntityTypes;
|
| 12 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 13 | import net.minecraft.world.entity.LivingEntity;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.Items;
|
| 17 | import net.minecraft.world.level.BlockGetter;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.LevelReader;
|
| 20 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 23 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 24 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 25 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 26 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 27 | import net.minecraft.world.phys.BlockHitResult;
|
| 28 | import net.minecraft.world.phys.Vec3;
|
| 29 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 30 | import net.minecraft.world.phys.shapes.Shapes;
|
| 31 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 32 |
|
| 33 | public class SweetBerryBushBlock extends VegetationBlock implements BonemealableBlock {
|
| 34 | private static final float HURT_SPEED_THRESHOLD = 0.003F;
|
| 35 | public static final int MAX_AGE = 3;
|
| 36 | public static final IntegerProperty AGE = BlockStateProperties.AGE_3;
|
| 37 | private static final VoxelShape SHAPE_SAPLING = Block.column(10.0, 0.0, 8.0);
|
| 38 | private static final VoxelShape SHAPE_GROWING = Block.column(14.0, 0.0, 16.0);
|
| 39 |
|
| 40 | public SweetBerryBushBlock(final BlockBehaviour.Properties properties) {
|
| 41 | super(properties);
|
| 42 | this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0));
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 47 | return new ItemStack(Items.SWEET_BERRIES);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 52 | return switch (state.getValue(AGE)) {
|
| 53 | case 0 -> SHAPE_SAPLING;
|
| 54 | case 3 -> Shapes.block();
|
| 55 | default -> SHAPE_GROWING;
|
| 56 | };
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 61 | return state.getValue(AGE) < 3;
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 66 | int age = state.getValue(AGE);
|
| 67 | if (age < 3 && random.nextInt(5) == 0 && level.getRawBrightness(pos.above(), 0) >= 9) {
|
| 68 | BlockState newState = state.setValue(AGE, age + 1);
|
| 69 | level.setBlock(pos, newState, 2);
|
| 70 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(newState));
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected void entityInside(
|
| 76 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 77 | ) {
|
| 78 | if (entity instanceof LivingEntity && !entity.is(EntityTypes.FOX) && !entity.is(EntityTypes.BEE)) {
|
| 79 | entity.makeStuckInBlock(state, new Vec3(0.8F, 0.75, 0.8F));
|
| 80 | if (level instanceof ServerLevel serverLevel && state.getValue(AGE) != 0) {
|
| 81 | Vec3 movement = entity.isClientAuthoritative() ? entity.getKnownMovement() : entity.oldPosition().subtract(entity.position());
|
| 82 | if (movement.horizontalDistanceSqr() > 0.0) {
|
| 83 | double xs = Math.abs(movement.x());
|
| 84 | double zs = Math.abs(movement.z());
|
| 85 | if (xs >= 0.003F || zs >= 0.003F) {
|
| 86 | entity.hurtServer(serverLevel, level.damageSources().sweetBerryBush(), 1.0F);
|
| 87 | }
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected InteractionResult useItemOn(
|
| 95 | final ItemStack itemStack,
|
| 96 | final BlockState state,
|
| 97 | final Level level,
|
| 98 | final BlockPos pos,
|
| 99 | final Player player,
|
| 100 | final InteractionHand hand,
|
| 101 | final BlockHitResult hitResult
|
| 102 | ) {
|
| 103 | int age = state.getValue(AGE);
|
| 104 | boolean isMaxAge = age == 3;
|
| 105 | return !isMaxAge && itemStack.is(Items.BONE_MEAL) ? InteractionResult.PASS : super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 110 | if (state.getValue(AGE) > 1) {
|
| 111 | if (level instanceof ServerLevel serverLevel) {
|
| 112 | Block.dropFromBlockInteractLootTable(
|
| 113 | serverLevel,
|
| 114 | BuiltInLootTables.HARVEST_SWEET_BERRY_BUSH,
|
| 115 | pos,
|
| 116 | state,
|
| 117 | level.getBlockEntity(pos),
|
| 118 | null,
|
| 119 | player,
|
| 120 | (serverlvl, itemStack) -> Block.popResource(serverlvl, pos, itemStack)
|
| 121 | );
|
| 122 | serverLevel.playSound(null, pos, SoundEvents.SWEET_BERRY_BUSH_PICK_BERRIES, SoundSource.BLOCKS, 1.0F, 0.8F + serverLevel.getRandom().nextFloat() * 0.4F);
|
| 123 | BlockState newState = state.setValue(AGE, 1);
|
| 124 | serverLevel.setBlock(pos, newState, 2);
|
| 125 | serverLevel.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(player, newState));
|
| 126 | }
|
| 127 |
|
| 128 | return InteractionResult.SUCCESS;
|
| 129 | } else {
|
| 130 | return super.useWithoutItem(state, level, pos, player, hitResult);
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 136 | builder.add(AGE);
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 141 | return state.getValue(AGE) < 3;
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 146 | return true;
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 151 | int newAge = Math.min(3, state.getValue(AGE) + 1);
|
| 152 | level.setBlock(pos, state.setValue(AGE, newAge), 2);
|
| 153 | }
|
| 154 | }
|
| 155 |
|