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