| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.sounds.SoundSource;
|
| 8 | import net.minecraft.tags.BlockTags;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.level.BlockGetter;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 | import net.minecraft.world.level.LevelReader;
|
| 13 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 14 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 17 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 18 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 19 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 20 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 21 |
|
| 22 | public class HangingMossBlock extends Block implements BonemealableBlock {
|
| 23 | private static final VoxelShape SHAPE_BASE = Block.column(14.0, 0.0, 16.0);
|
| 24 | private static final VoxelShape SHAPE_TIP = Block.column(14.0, 2.0, 16.0);
|
| 25 | public static final BooleanProperty TIP = BlockStateProperties.TIP;
|
| 26 |
|
| 27 | public HangingMossBlock(final BlockBehaviour.Properties properties) {
|
| 28 | super(properties);
|
| 29 | this.registerDefaultState(this.stateDefinition.any().setValue(TIP, true));
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 34 | return state.getValue(TIP) ? SHAPE_TIP : SHAPE_BASE;
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 39 | if (random.nextInt(500) == 0) {
|
| 40 | BlockState above = level.getBlockState(pos.above());
|
| 41 | if (above.is(BlockTags.PALE_OAK_LOGS) || above.is(Blocks.PALE_OAK_LEAVES)) {
|
| 42 | level.playLocalSound(pos.getX(), pos.getY(), pos.getZ(), SoundEvents.PALE_HANGING_MOSS_IDLE, SoundSource.AMBIENT, 1.0F, 1.0F, false);
|
| 43 | }
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 49 | return true;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 54 | return this.canStayAtPosition(level, pos);
|
| 55 | }
|
| 56 |
|
| 57 | private boolean canStayAtPosition(final BlockGetter level, final BlockPos pos) {
|
| 58 | BlockPos neighbourPos = pos.relative(Direction.UP);
|
| 59 | BlockState blockState = level.getBlockState(neighbourPos);
|
| 60 | return MultifaceBlock.canAttachTo(level, Direction.UP, neighbourPos, blockState) || blockState.is(this);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected BlockState updateShape(
|
| 65 | final BlockState state,
|
| 66 | final LevelReader level,
|
| 67 | final ScheduledTickAccess ticks,
|
| 68 | final BlockPos pos,
|
| 69 | final Direction directionToNeighbour,
|
| 70 | final BlockPos neighbourPos,
|
| 71 | final BlockState neighbourState,
|
| 72 | final RandomSource random
|
| 73 | ) {
|
| 74 | if (!this.canStayAtPosition(level, pos)) {
|
| 75 | ticks.scheduleTick(pos, this, 1);
|
| 76 | }
|
| 77 |
|
| 78 | return state.setValue(TIP, !level.getBlockState(pos.below()).is(this));
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 83 | if (!this.canStayAtPosition(level, pos)) {
|
| 84 | level.destroyBlock(pos, true);
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 90 | builder.add(TIP);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 95 | BlockPos growPos = this.getTip(level, pos).below();
|
| 96 | return this.canGrowInto(level.getBlockState(growPos)) && level.isInsideBuildHeight(growPos);
|
| 97 | }
|
| 98 |
|
| 99 | private boolean canGrowInto(final BlockState state) {
|
| 100 | return state.isAir();
|
| 101 | }
|
| 102 |
|
| 103 | public BlockPos getTip(final BlockGetter level, final BlockPos pos) {
|
| 104 | BlockPos.MutableBlockPos forwardPos = pos.mutable();
|
| 105 |
|
| 106 | BlockState forwardState;
|
| 107 | do {
|
| 108 | forwardPos.move(Direction.DOWN);
|
| 109 | forwardState = level.getBlockState(forwardPos);
|
| 110 | } while (forwardState.is(this));
|
| 111 |
|
| 112 | return forwardPos.relative(Direction.UP).immutable();
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 117 | return true;
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 122 | BlockPos tipPos = this.getTip(level, pos).below();
|
| 123 | if (this.canGrowInto(level.getBlockState(tipPos))) {
|
| 124 | level.setBlockAndUpdate(tipPos, state.setValue(TIP, true));
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|