| 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.LevelReader;
|
| 11 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 12 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 15 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 16 |
|
| 17 | /**
|
| 18 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 19 | */
|
| 20 | public class ChorusPlantBlock extends PipeBlock {
|
| 21 | /**
|
| 22 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 23 | */
|
| 24 | public ChorusPlantBlock(final BlockBehaviour.Properties properties) {
|
| 25 | super(10.0F, properties);
|
| 26 | this.registerDefaultState(
|
| 27 | this.stateDefinition
|
| 28 | .any()
|
| 29 | .setValue(NORTH, false)
|
| 30 | .setValue(EAST, false)
|
| 31 | .setValue(SOUTH, false)
|
| 32 | .setValue(WEST, false)
|
| 33 | .setValue(UP, false)
|
| 34 | .setValue(DOWN, false)
|
| 35 | );
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 40 | return getStateWithConnections(context.getLevel(), context.getClickedPos(), this.defaultBlockState());
|
| 41 | }
|
| 42 |
|
| 43 | public static BlockState getStateWithConnections(final BlockGetter level, final BlockPos pos, final BlockState defaultState) {
|
| 44 | BlockState down = level.getBlockState(pos.below());
|
| 45 | BlockState up = level.getBlockState(pos.above());
|
| 46 | BlockState north = level.getBlockState(pos.north());
|
| 47 | BlockState east = level.getBlockState(pos.east());
|
| 48 | BlockState south = level.getBlockState(pos.south());
|
| 49 | BlockState west = level.getBlockState(pos.west());
|
| 50 | Block block = defaultState.getBlock();
|
| 51 | return defaultState.trySetValue(DOWN, down.is(block) || down.is(Blocks.CHORUS_FLOWER) || down.is(BlockTags.SUPPORTS_CHORUS_PLANT))
|
| 52 | .trySetValue(UP, up.is(block) || up.is(Blocks.CHORUS_FLOWER))
|
| 53 | .trySetValue(NORTH, north.is(block) || north.is(Blocks.CHORUS_FLOWER))
|
| 54 | .trySetValue(EAST, east.is(block) || east.is(Blocks.CHORUS_FLOWER))
|
| 55 | .trySetValue(SOUTH, south.is(block) || south.is(Blocks.CHORUS_FLOWER))
|
| 56 | .trySetValue(WEST, west.is(block) || west.is(Blocks.CHORUS_FLOWER));
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected BlockState updateShape(
|
| 61 | final BlockState state,
|
| 62 | final LevelReader level,
|
| 63 | final ScheduledTickAccess ticks,
|
| 64 | final BlockPos pos,
|
| 65 | final Direction directionToNeighbour,
|
| 66 | final BlockPos neighbourPos,
|
| 67 | final BlockState neighbourState,
|
| 68 | final RandomSource random
|
| 69 | ) {
|
| 70 | if (!state.canSurvive(level, pos)) {
|
| 71 | ticks.scheduleTick(pos, this, 1);
|
| 72 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 73 | } else {
|
| 74 | boolean connect = neighbourState.is(this)
|
| 75 | || neighbourState.is(Blocks.CHORUS_FLOWER)
|
| 76 | || directionToNeighbour == Direction.DOWN && neighbourState.is(BlockTags.SUPPORTS_CHORUS_PLANT);
|
| 77 | return state.setValue(PROPERTY_BY_DIRECTION.get(directionToNeighbour), connect);
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 83 | if (!state.canSurvive(level, pos)) {
|
| 84 | level.destroyBlock(pos, true);
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 90 | BlockState belowState = level.getBlockState(pos.below());
|
| 91 | boolean blockAboveOrBelow = !level.getBlockState(pos.above()).isAir() && !belowState.isAir();
|
| 92 |
|
| 93 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 94 | BlockPos neighborPos = pos.relative(direction);
|
| 95 | BlockState neighborState = level.getBlockState(neighborPos);
|
| 96 | if (neighborState.is(this)) {
|
| 97 | if (blockAboveOrBelow) {
|
| 98 | return false;
|
| 99 | }
|
| 100 |
|
| 101 | BlockState below = level.getBlockState(neighborPos.below());
|
| 102 | if (below.is(this) || below.is(BlockTags.SUPPORTS_CHORUS_PLANT)) {
|
| 103 | return true;
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | return belowState.is(this) || belowState.is(BlockTags.SUPPORTS_CHORUS_PLANT);
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 113 | builder.add(NORTH, EAST, SOUTH, WEST, UP, DOWN);
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 118 | return false;
|
| 119 | }
|
| 120 | }
|
| 121 |
|