| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import java.util.Map.Entry;
|
| 5 | import java.util.function.Function;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.util.Util;
|
| 11 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 12 | import net.minecraft.world.level.BlockGetter;
|
| 13 | import net.minecraft.world.level.LevelReader;
|
| 14 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 15 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 18 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 19 | import net.minecraft.world.level.gamerules.GameRules;
|
| 20 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 21 | import net.minecraft.world.phys.shapes.Shapes;
|
| 22 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | public class VineBlock extends Block {
|
| 26 | private static final int SEARCH_RADIUS = 4;
|
| 27 | private static final int MAX_NEIGHBORS_TO_GROW = 4;
|
| 28 | public static final BooleanProperty UP = PipeBlock.UP;
|
| 29 | public static final BooleanProperty NORTH = PipeBlock.NORTH;
|
| 30 | public static final BooleanProperty EAST = PipeBlock.EAST;
|
| 31 | public static final BooleanProperty SOUTH = PipeBlock.SOUTH;
|
| 32 | public static final BooleanProperty WEST = PipeBlock.WEST;
|
| 33 | public static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = PipeBlock.PROPERTY_BY_DIRECTION
|
| 34 | .entrySet()
|
| 35 | .stream()
|
| 36 | .filter(e -> e.getKey() != Direction.DOWN)
|
| 37 | .collect(Util.toMap());
|
| 38 | private final Function<BlockState, VoxelShape> shapes;
|
| 39 |
|
| 40 | public VineBlock(final BlockBehaviour.Properties properties) {
|
| 41 | super(properties);
|
| 42 | this.registerDefaultState(
|
| 43 | this.stateDefinition.any().setValue(UP, false).setValue(NORTH, false).setValue(EAST, false).setValue(SOUTH, false).setValue(WEST, false)
|
| 44 | );
|
| 45 | this.shapes = this.makeShapes();
|
| 46 | }
|
| 47 |
|
| 48 | private Function<BlockState, VoxelShape> makeShapes() {
|
| 49 | Map<Direction, VoxelShape> shapes = Shapes.rotateAll(Block.boxZ(16.0, 0.0, 1.0));
|
| 50 | return this.getShapeForEachState(state -> {
|
| 51 | VoxelShape shape = Shapes.empty();
|
| 52 |
|
| 53 | for (Entry<Direction, BooleanProperty> entry : PROPERTY_BY_DIRECTION.entrySet()) {
|
| 54 | if (state.getValue(entry.getValue())) {
|
| 55 | shape = Shapes.or(shape, shapes.get(entry.getKey()));
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | return shape.isEmpty() ? Shapes.block() : shape;
|
| 60 | });
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 65 | return this.shapes.apply(state);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 70 | return true;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 75 | return this.hasFaces(this.getUpdatedState(state, level, pos));
|
| 76 | }
|
| 77 |
|
| 78 | private boolean hasFaces(final BlockState blockState) {
|
| 79 | return this.countFaces(blockState) > 0;
|
| 80 | }
|
| 81 |
|
| 82 | private int countFaces(final BlockState blockState) {
|
| 83 | int count = 0;
|
| 84 |
|
| 85 | for (BooleanProperty property : PROPERTY_BY_DIRECTION.values()) {
|
| 86 | if (blockState.getValue(property)) {
|
| 87 | count++;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | return count;
|
| 92 | }
|
| 93 |
|
| 94 | private boolean canSupportAtFace(final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 95 | if (direction == Direction.DOWN) {
|
| 96 | return false;
|
| 97 | }
|
| 98 |
|
| 99 | BlockPos relative = pos.relative(direction);
|
| 100 | if (isAcceptableNeighbour(level, relative, direction)) {
|
| 101 | return true;
|
| 102 | }
|
| 103 |
|
| 104 | if (direction.getAxis() == Direction.Axis.Y) {
|
| 105 | return false;
|
| 106 | }
|
| 107 |
|
| 108 | BooleanProperty property = PROPERTY_BY_DIRECTION.get(direction);
|
| 109 | BlockState aboveState = level.getBlockState(pos.above());
|
| 110 | return aboveState.is(this) && aboveState.getValue(property);
|
| 111 | }
|
| 112 |
|
| 113 | public static boolean isAcceptableNeighbour(final BlockGetter level, final BlockPos neighbourPos, final Direction directionToNeighbour) {
|
| 114 | return MultifaceBlock.canAttachTo(level, directionToNeighbour, neighbourPos, level.getBlockState(neighbourPos));
|
| 115 | }
|
| 116 |
|
| 117 | private BlockState getUpdatedState(BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 118 | BlockPos abovePos = pos.above();
|
| 119 | if (state.getValue(UP)) {
|
| 120 | state = state.setValue(UP, isAcceptableNeighbour(level, abovePos, Direction.DOWN));
|
| 121 | }
|
| 122 |
|
| 123 | BlockState aboveState = null;
|
| 124 |
|
| 125 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 126 | BooleanProperty property = getPropertyForFace(direction);
|
| 127 | if (state.getValue(property)) {
|
| 128 | boolean canSupport = this.canSupportAtFace(level, pos, direction);
|
| 129 | if (!canSupport) {
|
| 130 | if (aboveState == null) {
|
| 131 | aboveState = level.getBlockState(abovePos);
|
| 132 | }
|
| 133 |
|
| 134 | canSupport = aboveState.is(this) && aboveState.getValue(property);
|
| 135 | }
|
| 136 |
|
| 137 | state = state.setValue(property, canSupport);
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | return state;
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | protected BlockState updateShape(
|
| 146 | final BlockState state,
|
| 147 | final LevelReader level,
|
| 148 | final ScheduledTickAccess ticks,
|
| 149 | final BlockPos pos,
|
| 150 | final Direction directionToNeighbour,
|
| 151 | final BlockPos neighbourPos,
|
| 152 | final BlockState neighbourState,
|
| 153 | final RandomSource random
|
| 154 | ) {
|
| 155 | if (directionToNeighbour == Direction.DOWN) {
|
| 156 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 157 | }
|
| 158 |
|
| 159 | BlockState blockState = this.getUpdatedState(state, level, pos);
|
| 160 | return !this.hasFaces(blockState) ? Blocks.AIR.defaultBlockState() : blockState;
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 165 | if (level.getGameRules().get(GameRules.SPREAD_VINES)) {
|
| 166 | if (random.nextInt(4) == 0) {
|
| 167 | Direction testDirection = Direction.getRandom(random);
|
| 168 | BlockPos abovePos = pos.above();
|
| 169 | if (testDirection.getAxis().isHorizontal() && !state.getValue(getPropertyForFace(testDirection))) {
|
| 170 | if (this.canSpread(level, pos)) {
|
| 171 | BlockPos testPos = pos.relative(testDirection);
|
| 172 | BlockState edgeState = level.getBlockState(testPos);
|
| 173 | if (edgeState.isAir()) {
|
| 174 | Direction cwDirection = testDirection.getClockWise();
|
| 175 | Direction ccwDirection = testDirection.getCounterClockWise();
|
| 176 | boolean cwHasConnectingFace = state.getValue(getPropertyForFace(cwDirection));
|
| 177 | boolean ccwHasConnectingFace = state.getValue(getPropertyForFace(ccwDirection));
|
| 178 | BlockPos cwTestPos = testPos.relative(cwDirection);
|
| 179 | BlockPos ccwTestPos = testPos.relative(ccwDirection);
|
| 180 | if (cwHasConnectingFace && isAcceptableNeighbour(level, cwTestPos, cwDirection)) {
|
| 181 | level.setBlock(testPos, this.defaultBlockState().setValue(getPropertyForFace(cwDirection), true), 2);
|
| 182 | } else if (ccwHasConnectingFace && isAcceptableNeighbour(level, ccwTestPos, ccwDirection)) {
|
| 183 | level.setBlock(testPos, this.defaultBlockState().setValue(getPropertyForFace(ccwDirection), true), 2);
|
| 184 | } else {
|
| 185 | Direction opposite = testDirection.getOpposite();
|
| 186 | if (cwHasConnectingFace && level.isEmptyBlock(cwTestPos) && isAcceptableNeighbour(level, pos.relative(cwDirection), opposite)) {
|
| 187 | level.setBlock(cwTestPos, this.defaultBlockState().setValue(getPropertyForFace(opposite), true), 2);
|
| 188 | } else if (ccwHasConnectingFace && level.isEmptyBlock(ccwTestPos) && isAcceptableNeighbour(level, pos.relative(ccwDirection), opposite)) {
|
| 189 | level.setBlock(ccwTestPos, this.defaultBlockState().setValue(getPropertyForFace(opposite), true), 2);
|
| 190 | } else if (random.nextFloat() < 0.05 && isAcceptableNeighbour(level, testPos.above(), Direction.UP)) {
|
| 191 | level.setBlock(testPos, this.defaultBlockState().setValue(UP, true), 2);
|
| 192 | }
|
| 193 | }
|
| 194 | } else if (isAcceptableNeighbour(level, testPos, testDirection)) {
|
| 195 | level.setBlock(pos, state.setValue(getPropertyForFace(testDirection), true), 2);
|
| 196 | }
|
| 197 | }
|
| 198 | } else {
|
| 199 | if (testDirection == Direction.UP && pos.getY() < level.getMaxY()) {
|
| 200 | if (this.canSupportAtFace(level, pos, testDirection)) {
|
| 201 | level.setBlock(pos, state.setValue(UP, true), 2);
|
| 202 | return;
|
| 203 | }
|
| 204 |
|
| 205 | if (level.isEmptyBlock(abovePos)) {
|
| 206 | if (!this.canSpread(level, pos)) {
|
| 207 | return;
|
| 208 | }
|
| 209 |
|
| 210 | BlockState aboveState = state;
|
| 211 |
|
| 212 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 213 | if (random.nextBoolean() || !isAcceptableNeighbour(level, abovePos.relative(direction), direction)) {
|
| 214 | aboveState = aboveState.setValue(getPropertyForFace(direction), false);
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | if (this.hasHorizontalConnection(aboveState)) {
|
| 219 | level.setBlock(abovePos, aboveState, 2);
|
| 220 | }
|
| 221 |
|
| 222 | return;
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | if (pos.getY() > level.getMinY()) {
|
| 227 | BlockPos belowPos = pos.below();
|
| 228 | BlockState belowState = level.getBlockState(belowPos);
|
| 229 | if (belowState.isAir() || belowState.is(this)) {
|
| 230 | BlockState before = belowState.isAir() ? this.defaultBlockState() : belowState;
|
| 231 | BlockState after = this.copyRandomFaces(state, before, random);
|
| 232 | if (before != after && this.hasHorizontalConnection(after)) {
|
| 233 | level.setBlock(belowPos, after, 2);
|
| 234 | }
|
| 235 | }
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 | private BlockState copyRandomFaces(final BlockState from, BlockState to, final RandomSource random) {
|
| 243 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 244 | if (random.nextBoolean()) {
|
| 245 | BooleanProperty propertyForFace = getPropertyForFace(direction);
|
| 246 | if (from.getValue(propertyForFace)) {
|
| 247 | to = to.setValue(propertyForFace, true);
|
| 248 | }
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | return to;
|
| 253 | }
|
| 254 |
|
| 255 | private boolean hasHorizontalConnection(final BlockState state) {
|
| 256 | return state.getValue(NORTH) || state.getValue(EAST) || state.getValue(SOUTH) || state.getValue(WEST);
|
| 257 | }
|
| 258 |
|
| 259 | private boolean canSpread(final LevelReader level, final BlockPos pos) {
|
| 260 | BlockPos minPos = pos.offset(-4, -1, -4);
|
| 261 | BlockPos maxPos = pos.offset(4, 1, 4);
|
| 262 | return level.findBlocksIn(minPos, maxPos).filterState(state -> state.is(this)).atMostMatched(4);
|
| 263 | }
|
| 264 |
|
| 265 | @Override
|
| 266 | protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
|
| 267 | BlockState clickedState = context.getLevel().getBlockState(context.getClickedPos());
|
| 268 | return clickedState.is(this) ? this.countFaces(clickedState) < PROPERTY_BY_DIRECTION.size() : super.canBeReplaced(state, context);
|
| 269 | }
|
| 270 |
|
| 271 | @Override
|
| 272 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 273 | BlockState clickedState = context.getLevel().getBlockState(context.getClickedPos());
|
| 274 | boolean clickedVine = clickedState.is(this);
|
| 275 | BlockState result = clickedVine ? clickedState : this.defaultBlockState();
|
| 276 |
|
| 277 | for (Direction direction : context.getNearestLookingDirections()) {
|
| 278 | if (direction != Direction.DOWN) {
|
| 279 | BooleanProperty face = getPropertyForFace(direction);
|
| 280 | boolean faceOccupied = clickedVine && clickedState.getValue(face);
|
| 281 | if (!faceOccupied && this.canSupportAtFace(context.getLevel(), context.getClickedPos(), direction)) {
|
| 282 | return result.setValue(face, true);
|
| 283 | }
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | return clickedVine ? result : null;
|
| 288 | }
|
| 289 |
|
| 290 | @Override
|
| 291 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 292 | builder.add(UP, NORTH, EAST, SOUTH, WEST);
|
| 293 | }
|
| 294 |
|
| 295 | @Override
|
| 296 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 297 | return switch (rotation) {
|
| 298 | case CLOCKWISE_180 -> (BlockState)state.setValue(NORTH, state.getValue(SOUTH))
|
| 299 | .setValue(EAST, state.getValue(WEST))
|
| 300 | .setValue(SOUTH, state.getValue(NORTH))
|
| 301 | .setValue(WEST, state.getValue(EAST));
|
| 302 | case COUNTERCLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(EAST))
|
| 303 | .setValue(EAST, state.getValue(SOUTH))
|
| 304 | .setValue(SOUTH, state.getValue(WEST))
|
| 305 | .setValue(WEST, state.getValue(NORTH));
|
| 306 | case CLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(WEST))
|
| 307 | .setValue(EAST, state.getValue(NORTH))
|
| 308 | .setValue(SOUTH, state.getValue(EAST))
|
| 309 | .setValue(WEST, state.getValue(SOUTH));
|
| 310 | default -> state;
|
| 311 | };
|
| 312 | }
|
| 313 |
|
| 314 | @Override
|
| 315 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 316 | switch (mirror) {
|
| 317 | case LEFT_RIGHT:
|
| 318 | return state.setValue(NORTH, state.getValue(SOUTH)).setValue(SOUTH, state.getValue(NORTH));
|
| 319 | case FRONT_BACK:
|
| 320 | return state.setValue(EAST, state.getValue(WEST)).setValue(WEST, state.getValue(EAST));
|
| 321 | default:
|
| 322 | return super.mirror(state, mirror);
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | public static BooleanProperty getPropertyForFace(final Direction direction) {
|
| 327 | return PROPERTY_BY_DIRECTION.get(direction);
|
| 328 | }
|
| 329 | }
|
| 330 |
|