144 lines
5.7 KiB
Java
144 lines
5.7 KiB
Java
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.InsideBlockEffectApplier;
|
|
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.Builder;
|
|
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.Shapes;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
|
|
public class SweetBerryBushBlock extends VegetationBlock implements BonemealableBlock {
|
|
public static final MapCodec<SweetBerryBushBlock> 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 SHAPE_SAPLING = Block.column(10.0, 0.0, 8.0);
|
|
private static final VoxelShape SHAPE_GROWING = Block.column(14.0, 0.0, 16.0);
|
|
|
|
@Override
|
|
public MapCodec<SweetBerryBushBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
public SweetBerryBushBlock(BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0));
|
|
}
|
|
|
|
@Override
|
|
protected ItemStack getCloneItemStack(LevelReader level, BlockPos pos, BlockState state, boolean includeData) {
|
|
return new ItemStack(Items.SWEET_BERRIES);
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
return switch (state.getValue(AGE)) {
|
|
case 0 -> SHAPE_SAPLING;
|
|
case 3 -> Shapes.block();
|
|
default -> SHAPE_GROWING;
|
|
};
|
|
}
|
|
|
|
@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, InsideBlockEffectApplier effectApplier) {
|
|
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.isClientAuthoritative() ? 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 stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult
|
|
) {
|
|
int i = (Integer)state.getValue(AGE);
|
|
boolean bl = i == 3;
|
|
return (InteractionResult)(!bl && stack.is(Items.BONE_MEAL) ? InteractionResult.PASS : super.useItemOn(stack, state, level, pos, player, hand, hitResult));
|
|
}
|
|
|
|
@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(Builder<Block, BlockState> 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);
|
|
}
|
|
}
|