minecraft-src/net/minecraft/world/level/block/BambooStalkBlock.java
2025-07-04 01:41:11 +03:00

236 lines
8.8 KiB
Java

package net.minecraft.world.level.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.SwordItem;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
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.BambooLeaves;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class BambooStalkBlock extends Block implements BonemealableBlock {
public static final MapCodec<BambooStalkBlock> CODEC = simpleCodec(BambooStalkBlock::new);
protected static final float SMALL_LEAVES_AABB_OFFSET = 3.0F;
protected static final float LARGE_LEAVES_AABB_OFFSET = 5.0F;
protected static final float COLLISION_AABB_OFFSET = 1.5F;
protected static final VoxelShape SMALL_SHAPE = Block.box(5.0, 0.0, 5.0, 11.0, 16.0, 11.0);
protected static final VoxelShape LARGE_SHAPE = Block.box(3.0, 0.0, 3.0, 13.0, 16.0, 13.0);
protected static final VoxelShape COLLISION_SHAPE = Block.box(6.5, 0.0, 6.5, 9.5, 16.0, 9.5);
public static final IntegerProperty AGE = BlockStateProperties.AGE_1;
public static final EnumProperty<BambooLeaves> LEAVES = BlockStateProperties.BAMBOO_LEAVES;
public static final IntegerProperty STAGE = BlockStateProperties.STAGE;
public static final int MAX_HEIGHT = 16;
public static final int STAGE_GROWING = 0;
public static final int STAGE_DONE_GROWING = 1;
public static final int AGE_THIN_BAMBOO = 0;
public static final int AGE_THICK_BAMBOO = 1;
@Override
public MapCodec<BambooStalkBlock> codec() {
return CODEC;
}
public BambooStalkBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0).setValue(LEAVES, BambooLeaves.NONE).setValue(STAGE, 0));
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(AGE, LEAVES, STAGE);
}
@Override
protected boolean propagatesSkylightDown(BlockState state, BlockGetter level, BlockPos pos) {
return true;
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
VoxelShape voxelShape = state.getValue(LEAVES) == BambooLeaves.LARGE ? LARGE_SHAPE : SMALL_SHAPE;
Vec3 vec3 = state.getOffset(level, pos);
return voxelShape.move(vec3.x, vec3.y, vec3.z);
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
@Override
protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
Vec3 vec3 = state.getOffset(level, pos);
return COLLISION_SHAPE.move(vec3.x, vec3.y, vec3.z);
}
@Override
protected boolean isCollisionShapeFullBlock(BlockState state, BlockGetter level, BlockPos pos) {
return false;
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos());
if (!fluidState.isEmpty()) {
return null;
} else {
BlockState blockState = context.getLevel().getBlockState(context.getClickedPos().below());
if (blockState.is(BlockTags.BAMBOO_PLANTABLE_ON)) {
if (blockState.is(Blocks.BAMBOO_SAPLING)) {
return this.defaultBlockState().setValue(AGE, 0);
} else if (blockState.is(Blocks.BAMBOO)) {
int i = blockState.getValue(AGE) > 0 ? 1 : 0;
return this.defaultBlockState().setValue(AGE, i);
} else {
BlockState blockState2 = context.getLevel().getBlockState(context.getClickedPos().above());
return blockState2.is(Blocks.BAMBOO)
? this.defaultBlockState().setValue(AGE, (Integer)blockState2.getValue(AGE))
: Blocks.BAMBOO_SAPLING.defaultBlockState();
}
} else {
return null;
}
}
}
@Override
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (!state.canSurvive(level, pos)) {
level.destroyBlock(pos, true);
}
}
@Override
protected boolean isRandomlyTicking(BlockState state) {
return (Integer)state.getValue(STAGE) == 0;
}
@Override
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if ((Integer)state.getValue(STAGE) == 0) {
if (random.nextInt(3) == 0 && level.isEmptyBlock(pos.above()) && level.getRawBrightness(pos.above(), 0) >= 9) {
int i = this.getHeightBelowUpToMax(level, pos) + 1;
if (i < 16) {
this.growBamboo(state, level, pos, random, i);
}
}
}
}
@Override
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
return level.getBlockState(pos.below()).is(BlockTags.BAMBOO_PLANTABLE_ON);
}
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
if (!state.canSurvive(level, pos)) {
level.scheduleTick(pos, this, 1);
}
if (direction == Direction.UP && neighborState.is(Blocks.BAMBOO) && (Integer)neighborState.getValue(AGE) > (Integer)state.getValue(AGE)) {
level.setBlock(pos, state.cycle(AGE), 2);
}
return super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
@Override
public boolean isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) {
int i = this.getHeightAboveUpToMax(level, pos);
int j = this.getHeightBelowUpToMax(level, pos);
return i + j + 1 < 16 && (Integer)level.getBlockState(pos.above(i)).getValue(STAGE) != 1;
}
@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 = this.getHeightAboveUpToMax(level, pos);
int j = this.getHeightBelowUpToMax(level, pos);
int k = i + j + 1;
int l = 1 + random.nextInt(2);
for (int m = 0; m < l; m++) {
BlockPos blockPos = pos.above(i);
BlockState blockState = level.getBlockState(blockPos);
if (k >= 16 || (Integer)blockState.getValue(STAGE) == 1 || !level.isEmptyBlock(blockPos.above())) {
return;
}
this.growBamboo(blockState, level, blockPos, random, k);
i++;
k++;
}
}
@Override
protected float getDestroyProgress(BlockState state, Player player, BlockGetter level, BlockPos pos) {
return player.getMainHandItem().getItem() instanceof SwordItem ? 1.0F : super.getDestroyProgress(state, player, level, pos);
}
protected void growBamboo(BlockState state, Level level, BlockPos pos, RandomSource random, int age) {
BlockState blockState = level.getBlockState(pos.below());
BlockPos blockPos = pos.below(2);
BlockState blockState2 = level.getBlockState(blockPos);
BambooLeaves bambooLeaves = BambooLeaves.NONE;
if (age >= 1) {
if (!blockState.is(Blocks.BAMBOO) || blockState.getValue(LEAVES) == BambooLeaves.NONE) {
bambooLeaves = BambooLeaves.SMALL;
} else if (blockState.is(Blocks.BAMBOO) && blockState.getValue(LEAVES) != BambooLeaves.NONE) {
bambooLeaves = BambooLeaves.LARGE;
if (blockState2.is(Blocks.BAMBOO)) {
level.setBlock(pos.below(), blockState.setValue(LEAVES, BambooLeaves.SMALL), 3);
level.setBlock(blockPos, blockState2.setValue(LEAVES, BambooLeaves.NONE), 3);
}
}
}
int i = state.getValue(AGE) != 1 && !blockState2.is(Blocks.BAMBOO) ? 0 : 1;
int j = (age < 11 || !(random.nextFloat() < 0.25F)) && age != 15 ? 0 : 1;
level.setBlock(pos.above(), this.defaultBlockState().setValue(AGE, i).setValue(LEAVES, bambooLeaves).setValue(STAGE, j), 3);
}
protected int getHeightAboveUpToMax(BlockGetter level, BlockPos pos) {
int i = 0;
while (i < 16 && level.getBlockState(pos.above(i + 1)).is(Blocks.BAMBOO)) {
i++;
}
return i;
}
protected int getHeightBelowUpToMax(BlockGetter level, BlockPos pos) {
int i = 0;
while (i < 16 && level.getBlockState(pos.below(i + 1)).is(Blocks.BAMBOO)) {
i++;
}
return i;
}
}