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

147 lines
5.4 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.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.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class CocoaBlock extends HorizontalDirectionalBlock implements BonemealableBlock {
public static final MapCodec<CocoaBlock> CODEC = simpleCodec(CocoaBlock::new);
public static final int MAX_AGE = 2;
public static final IntegerProperty AGE = BlockStateProperties.AGE_2;
protected static final int AGE_0_WIDTH = 4;
protected static final int AGE_0_HEIGHT = 5;
protected static final int AGE_0_HALFWIDTH = 2;
protected static final int AGE_1_WIDTH = 6;
protected static final int AGE_1_HEIGHT = 7;
protected static final int AGE_1_HALFWIDTH = 3;
protected static final int AGE_2_WIDTH = 8;
protected static final int AGE_2_HEIGHT = 9;
protected static final int AGE_2_HALFWIDTH = 4;
protected static final VoxelShape[] EAST_AABB = new VoxelShape[]{
Block.box(11.0, 7.0, 6.0, 15.0, 12.0, 10.0), Block.box(9.0, 5.0, 5.0, 15.0, 12.0, 11.0), Block.box(7.0, 3.0, 4.0, 15.0, 12.0, 12.0)
};
protected static final VoxelShape[] WEST_AABB = new VoxelShape[]{
Block.box(1.0, 7.0, 6.0, 5.0, 12.0, 10.0), Block.box(1.0, 5.0, 5.0, 7.0, 12.0, 11.0), Block.box(1.0, 3.0, 4.0, 9.0, 12.0, 12.0)
};
protected static final VoxelShape[] NORTH_AABB = new VoxelShape[]{
Block.box(6.0, 7.0, 1.0, 10.0, 12.0, 5.0), Block.box(5.0, 5.0, 1.0, 11.0, 12.0, 7.0), Block.box(4.0, 3.0, 1.0, 12.0, 12.0, 9.0)
};
protected static final VoxelShape[] SOUTH_AABB = new VoxelShape[]{
Block.box(6.0, 7.0, 11.0, 10.0, 12.0, 15.0), Block.box(5.0, 5.0, 9.0, 11.0, 12.0, 15.0), Block.box(4.0, 3.0, 7.0, 12.0, 12.0, 15.0)
};
@Override
public MapCodec<CocoaBlock> codec() {
return CODEC;
}
public CocoaBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(AGE, 0));
}
@Override
protected boolean isRandomlyTicking(BlockState state) {
return (Integer)state.getValue(AGE) < 2;
}
@Override
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (level.random.nextInt(5) == 0) {
int i = (Integer)state.getValue(AGE);
if (i < 2) {
level.setBlock(pos, state.setValue(AGE, i + 1), 2);
}
}
}
@Override
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
BlockState blockState = level.getBlockState(pos.relative(state.getValue(FACING)));
return blockState.is(BlockTags.JUNGLE_LOGS);
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
int i = (Integer)state.getValue(AGE);
switch ((Direction)state.getValue(FACING)) {
case SOUTH:
return SOUTH_AABB[i];
case NORTH:
default:
return NORTH_AABB[i];
case WEST:
return WEST_AABB[i];
case EAST:
return EAST_AABB[i];
}
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockState blockState = this.defaultBlockState();
LevelReader levelReader = context.getLevel();
BlockPos blockPos = context.getClickedPos();
for (Direction direction : context.getNearestLookingDirections()) {
if (direction.getAxis().isHorizontal()) {
blockState = blockState.setValue(FACING, direction);
if (blockState.canSurvive(levelReader, blockPos)) {
return blockState;
}
}
}
return null;
}
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
return direction == state.getValue(FACING) && !state.canSurvive(level, pos)
? Blocks.AIR.defaultBlockState()
: super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
@Override
public boolean isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) {
return (Integer)state.getValue(AGE) < 2;
}
@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) {
level.setBlock(pos, state.setValue(AGE, (Integer)state.getValue(AGE) + 1), 2);
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, AGE);
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
}