| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.stream.IntStream;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.tags.BlockTags;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 12 | import net.minecraft.world.level.BlockGetter;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.LevelReader;
|
| 15 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 16 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 19 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 20 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 21 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 22 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 23 | import net.minecraft.world.phys.shapes.Shapes;
|
| 24 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class CocoaBlock extends HorizontalDirectionalBlock implements BonemealableBlock {
|
| 28 | public static final int MAX_AGE = 2;
|
| 29 | public static final IntegerProperty AGE = BlockStateProperties.AGE_2;
|
| 30 | private static final List<Map<Direction, VoxelShape>> SHAPES = IntStream.rangeClosed(0, 2)
|
| 31 | .mapToObj(i -> Shapes.rotateHorizontal(Block.column(4 + i * 2, 7 - i * 2, 12.0).move(0.0, 0.0, (i - 5) / 16.0).optimize()))
|
| 32 | .toList();
|
| 33 |
|
| 34 | public CocoaBlock(final BlockBehaviour.Properties properties) {
|
| 35 | super(properties);
|
| 36 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(AGE, 0));
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 41 | return state.getValue(AGE) < 2;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 46 | if (level.getRandom().nextInt(5) == 0) {
|
| 47 | int age = state.getValue(AGE);
|
| 48 | if (age < 2) {
|
| 49 | level.setBlock(pos, state.setValue(AGE, age + 1), 2);
|
| 50 | }
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 56 | BlockState relativeState = level.getBlockState(pos.relative(state.getValue(FACING)));
|
| 57 | return relativeState.is(BlockTags.SUPPORTS_COCOA);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 62 | return SHAPES.get(state.getValue(AGE)).get(state.getValue(FACING));
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 67 | BlockState state = this.defaultBlockState();
|
| 68 | LevelReader level = context.getLevel();
|
| 69 | BlockPos pos = context.getClickedPos();
|
| 70 |
|
| 71 | for (Direction direction : context.getNearestLookingDirections()) {
|
| 72 | if (direction.getAxis().isHorizontal()) {
|
| 73 | state = state.setValue(FACING, direction);
|
| 74 | if (state.canSurvive(level, pos)) {
|
| 75 | return state;
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return null;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected BlockState updateShape(
|
| 85 | final BlockState state,
|
| 86 | final LevelReader level,
|
| 87 | final ScheduledTickAccess ticks,
|
| 88 | final BlockPos pos,
|
| 89 | final Direction directionToNeighbour,
|
| 90 | final BlockPos neighbourPos,
|
| 91 | final BlockState neighbourState,
|
| 92 | final RandomSource random
|
| 93 | ) {
|
| 94 | return directionToNeighbour == state.getValue(FACING) && !state.canSurvive(level, pos)
|
| 95 | ? Blocks.AIR.defaultBlockState()
|
| 96 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 101 | return state.getValue(AGE) < 2;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 106 | return true;
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 111 | level.setBlock(pos, state.setValue(AGE, state.getValue(AGE) + 1), 2);
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 116 | builder.add(FACING, AGE);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 121 | return false;
|
| 122 | }
|
| 123 | }
|
| 124 |
|