| 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.tags.BlockTags;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.Level;
|
| 11 | import net.minecraft.world.level.LevelReader;
|
| 12 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 13 | import net.minecraft.world.level.block.grower.TreeGrower;
|
| 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.level.block.state.properties.IntegerProperty;
|
| 20 | import net.minecraft.world.level.material.FluidState;
|
| 21 | import net.minecraft.world.level.material.Fluids;
|
| 22 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 23 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public class MangrovePropaguleBlock extends SaplingBlock implements SimpleWaterloggedBlock {
|
| 27 | public static final IntegerProperty AGE = BlockStateProperties.AGE_4;
|
| 28 | public static final int MAX_AGE = 4;
|
| 29 | private static final int[] SHAPE_MIN_Y = new int[]{13, 10, 7, 3, 0};
|
| 30 | private static final VoxelShape[] SHAPE_PER_AGE = Block.boxes(4, age -> Block.column(2.0, SHAPE_MIN_Y[age], 16.0));
|
| 31 | private static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 32 | public static final BooleanProperty HANGING = BlockStateProperties.HANGING;
|
| 33 |
|
| 34 | public MangrovePropaguleBlock(final TreeGrower treeGrower, final BlockBehaviour.Properties properties) {
|
| 35 | super(treeGrower, properties);
|
| 36 | this.registerDefaultState(this.stateDefinition.any().setValue(STAGE, 0).setValue(AGE, 0).setValue(WATERLOGGED, false).setValue(HANGING, false));
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 41 | builder.add(STAGE).add(AGE).add(WATERLOGGED).add(HANGING);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 46 | return state.is(BlockTags.SUPPORTS_MANGROVE_PROPAGULE);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 51 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 52 | boolean isWaterSource = replacedFluidState.is(Fluids.WATER);
|
| 53 | return super.getStateForPlacement(context).setValue(WATERLOGGED, isWaterSource).setValue(AGE, 4);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 58 | int age = state.getValue(HANGING) ? state.getValue(AGE) : 4;
|
| 59 | return SHAPE_PER_AGE[age].move(state.getOffset(pos));
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 64 | return isHanging(state) ? level.getBlockState(pos.above()).is(BlockTags.SUPPORTS_HANGING_MANGROVE_PROPAGULE) : super.canSurvive(state, level, pos);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected BlockState updateShape(
|
| 69 | final BlockState state,
|
| 70 | final LevelReader level,
|
| 71 | final ScheduledTickAccess ticks,
|
| 72 | final BlockPos pos,
|
| 73 | final Direction directionToNeighbour,
|
| 74 | final BlockPos neighbourPos,
|
| 75 | final BlockState neighbourState,
|
| 76 | final RandomSource random
|
| 77 | ) {
|
| 78 | if (state.getValue(WATERLOGGED)) {
|
| 79 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 80 | }
|
| 81 |
|
| 82 | return directionToNeighbour == Direction.UP && !state.canSurvive(level, pos)
|
| 83 | ? Blocks.AIR.defaultBlockState()
|
| 84 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected FluidState getFluidState(final BlockState state) {
|
| 89 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 94 | if (!isHanging(state)) {
|
| 95 | if (random.nextInt(7) == 0) {
|
| 96 | this.advanceTree(level, pos, state, random);
|
| 97 | }
|
| 98 | } else {
|
| 99 | if (!isFullyGrown(state)) {
|
| 100 | level.setBlock(pos, state.cycle(AGE), 2);
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 107 | return !isHanging(state) || !isFullyGrown(state);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 112 | return isHanging(state) ? !isFullyGrown(state) : super.isBonemealSuccess(level, random, pos, state, source);
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 117 | if (isHanging(state) && !isFullyGrown(state)) {
|
| 118 | level.setBlock(pos, state.cycle(AGE), 2);
|
| 119 | } else {
|
| 120 | super.performBonemeal(level, random, pos, state, source);
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | private static boolean isHanging(final BlockState state) {
|
| 125 | return state.getValue(HANGING);
|
| 126 | }
|
| 127 |
|
| 128 | private static boolean isFullyGrown(final BlockState state) {
|
| 129 | return state.getValue(AGE) == 4;
|
| 130 | }
|
| 131 |
|
| 132 | public static BlockState createNewHangingPropagule() {
|
| 133 | return createNewHangingPropagule(0);
|
| 134 | }
|
| 135 |
|
| 136 | public static BlockState createNewHangingPropagule(final int age) {
|
| 137 | return Blocks.MANGROVE_PROPAGULE.defaultBlockState().setValue(HANGING, true).setValue(AGE, age);
|
| 138 | }
|
| 139 | }
|
| 140 |
|