| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Map.Entry;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.tags.BlockTags;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 13 | import net.minecraft.world.level.BlockGetter;
|
| 14 | import net.minecraft.world.level.LevelReader;
|
| 15 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 16 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 19 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 20 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 21 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 22 | import net.minecraft.world.level.block.state.properties.Property;
|
| 23 | import net.minecraft.world.level.block.state.properties.WallSide;
|
| 24 | import net.minecraft.world.level.material.FluidState;
|
| 25 | import net.minecraft.world.level.material.Fluids;
|
| 26 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 27 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 28 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 29 | import net.minecraft.world.phys.shapes.Shapes;
|
| 30 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 31 |
|
| 32 | public class WallBlock extends Block implements SimpleWaterloggedBlock {
|
| 33 | public static final BooleanProperty UP = BlockStateProperties.UP;
|
| 34 | public static final EnumProperty<WallSide> EAST = BlockStateProperties.EAST_WALL;
|
| 35 | public static final EnumProperty<WallSide> NORTH = BlockStateProperties.NORTH_WALL;
|
| 36 | public static final EnumProperty<WallSide> SOUTH = BlockStateProperties.SOUTH_WALL;
|
| 37 | public static final EnumProperty<WallSide> WEST = BlockStateProperties.WEST_WALL;
|
| 38 | public static final Map<Direction, EnumProperty<WallSide>> PROPERTY_BY_DIRECTION = ImmutableMap.copyOf(
|
| 39 | Maps.newEnumMap(Map.of(Direction.NORTH, NORTH, Direction.EAST, EAST, Direction.SOUTH, SOUTH, Direction.WEST, WEST))
|
| 40 | );
|
| 41 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 42 | private final Function<BlockState, VoxelShape> shapes;
|
| 43 | private final Function<BlockState, VoxelShape> collisionShapes;
|
| 44 | private static final VoxelShape TEST_SHAPE_POST = Block.column(2.0, 0.0, 16.0);
|
| 45 | private static final Map<Direction, VoxelShape> TEST_SHAPES_WALL = Shapes.rotateHorizontal(Block.boxZ(2.0, 16.0, 0.0, 9.0));
|
| 46 |
|
| 47 | public WallBlock(final BlockBehaviour.Properties properties) {
|
| 48 | super(properties);
|
| 49 | this.registerDefaultState(
|
| 50 | this.stateDefinition
|
| 51 | .any()
|
| 52 | .setValue(UP, true)
|
| 53 | .setValue(NORTH, WallSide.NONE)
|
| 54 | .setValue(EAST, WallSide.NONE)
|
| 55 | .setValue(SOUTH, WallSide.NONE)
|
| 56 | .setValue(WEST, WallSide.NONE)
|
| 57 | .setValue(WATERLOGGED, false)
|
| 58 | );
|
| 59 | this.shapes = this.makeShapes(16.0F, 14.0F);
|
| 60 | this.collisionShapes = this.makeShapes(24.0F, 24.0F);
|
| 61 | }
|
| 62 |
|
| 63 | private Function<BlockState, VoxelShape> makeShapes(final float postHeight, final float wallTop) {
|
| 64 | VoxelShape post = Block.column(8.0, 0.0, postHeight);
|
| 65 | int width = 6;
|
| 66 | Map<Direction, VoxelShape> low = Shapes.rotateHorizontal(Block.boxZ(6.0, 0.0, wallTop, 0.0, 11.0));
|
| 67 | Map<Direction, VoxelShape> tall = Shapes.rotateHorizontal(Block.boxZ(6.0, 0.0, postHeight, 0.0, 11.0));
|
| 68 | return this.getShapeForEachState(state -> {
|
| 69 | VoxelShape shape = state.getValue(UP) ? post : Shapes.empty();
|
| 70 |
|
| 71 | for (Entry<Direction, EnumProperty<WallSide>> entry : PROPERTY_BY_DIRECTION.entrySet()) {
|
| 72 | shape = Shapes.or(shape, switch ((WallSide)state.getValue(entry.getValue())) {
|
| 73 | case NONE -> Shapes.empty();
|
| 74 | case LOW -> (VoxelShape)low.get(entry.getKey());
|
| 75 | case TALL -> (VoxelShape)tall.get(entry.getKey());
|
| 76 | });
|
| 77 | }
|
| 78 |
|
| 79 | return shape;
|
| 80 | }, WATERLOGGED);
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 85 | return this.shapes.apply(state);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 90 | return this.collisionShapes.apply(state);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 95 | return false;
|
| 96 | }
|
| 97 |
|
| 98 | private boolean connectsTo(final BlockState state, final boolean faceSolid, final Direction direction) {
|
| 99 | Block block = state.getBlock();
|
| 100 | boolean connectedFenceGate = block instanceof FenceGateBlock && FenceGateBlock.connectsToDirection(state, direction);
|
| 101 | return state.is(BlockTags.WALLS) || !isExceptionForConnection(state) && faceSolid || block instanceof IronBarsBlock || connectedFenceGate;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 106 | LevelReader level = context.getLevel();
|
| 107 | BlockPos pos = context.getClickedPos();
|
| 108 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 109 | BlockPos northPos = pos.north();
|
| 110 | BlockPos eastPos = pos.east();
|
| 111 | BlockPos southPos = pos.south();
|
| 112 | BlockPos westPos = pos.west();
|
| 113 | BlockPos topPos = pos.above();
|
| 114 | BlockState northState = level.getBlockState(northPos);
|
| 115 | BlockState eastState = level.getBlockState(eastPos);
|
| 116 | BlockState southState = level.getBlockState(southPos);
|
| 117 | BlockState westState = level.getBlockState(westPos);
|
| 118 | BlockState topState = level.getBlockState(topPos);
|
| 119 | boolean north = this.connectsTo(northState, northState.isFaceSturdy(level, northPos, Direction.SOUTH), Direction.SOUTH);
|
| 120 | boolean east = this.connectsTo(eastState, eastState.isFaceSturdy(level, eastPos, Direction.WEST), Direction.WEST);
|
| 121 | boolean south = this.connectsTo(southState, southState.isFaceSturdy(level, southPos, Direction.NORTH), Direction.NORTH);
|
| 122 | boolean west = this.connectsTo(westState, westState.isFaceSturdy(level, westPos, Direction.EAST), Direction.EAST);
|
| 123 | BlockState state = this.defaultBlockState().setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 124 | return this.updateShape(level, state, topPos, topState, north, east, south, west);
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | protected BlockState updateShape(
|
| 129 | final BlockState state,
|
| 130 | final LevelReader level,
|
| 131 | final ScheduledTickAccess ticks,
|
| 132 | final BlockPos pos,
|
| 133 | final Direction directionToNeighbour,
|
| 134 | final BlockPos neighbourPos,
|
| 135 | final BlockState neighbourState,
|
| 136 | final RandomSource random
|
| 137 | ) {
|
| 138 | if (state.getValue(WATERLOGGED)) {
|
| 139 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 140 | }
|
| 141 |
|
| 142 | if (directionToNeighbour == Direction.DOWN) {
|
| 143 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 144 | } else {
|
| 145 | return directionToNeighbour == Direction.UP
|
| 146 | ? this.topUpdate(level, state, neighbourPos, neighbourState)
|
| 147 | : this.sideUpdate(level, pos, state, neighbourPos, neighbourState, directionToNeighbour);
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | private static boolean isConnected(final BlockState state, final Property<WallSide> northWall) {
|
| 152 | return state.getValue(northWall) != WallSide.NONE;
|
| 153 | }
|
| 154 |
|
| 155 | private static boolean isCovered(final VoxelShape aboveShape, final VoxelShape testShape) {
|
| 156 | return !Shapes.joinIsNotEmpty(testShape, aboveShape, BooleanOp.ONLY_FIRST);
|
| 157 | }
|
| 158 |
|
| 159 | private BlockState topUpdate(final LevelReader level, final BlockState state, final BlockPos topPos, final BlockState topNeighbour) {
|
| 160 | boolean north = isConnected(state, NORTH);
|
| 161 | boolean east = isConnected(state, EAST);
|
| 162 | boolean south = isConnected(state, SOUTH);
|
| 163 | boolean west = isConnected(state, WEST);
|
| 164 | return this.updateShape(level, state, topPos, topNeighbour, north, east, south, west);
|
| 165 | }
|
| 166 |
|
| 167 | private BlockState sideUpdate(
|
| 168 | final LevelReader level, final BlockPos pos, final BlockState state, final BlockPos neighbourPos, final BlockState neighbour, final Direction direction
|
| 169 | ) {
|
| 170 | Direction opposite = direction.getOpposite();
|
| 171 | boolean isNorthConnected = direction == Direction.NORTH
|
| 172 | ? this.connectsTo(neighbour, neighbour.isFaceSturdy(level, neighbourPos, opposite), opposite)
|
| 173 | : isConnected(state, NORTH);
|
| 174 | boolean isEastConnected = direction == Direction.EAST
|
| 175 | ? this.connectsTo(neighbour, neighbour.isFaceSturdy(level, neighbourPos, opposite), opposite)
|
| 176 | : isConnected(state, EAST);
|
| 177 | boolean isSouthConnected = direction == Direction.SOUTH
|
| 178 | ? this.connectsTo(neighbour, neighbour.isFaceSturdy(level, neighbourPos, opposite), opposite)
|
| 179 | : isConnected(state, SOUTH);
|
| 180 | boolean isWestConnected = direction == Direction.WEST
|
| 181 | ? this.connectsTo(neighbour, neighbour.isFaceSturdy(level, neighbourPos, opposite), opposite)
|
| 182 | : isConnected(state, WEST);
|
| 183 | BlockPos above = pos.above();
|
| 184 | BlockState aboveState = level.getBlockState(above);
|
| 185 | return this.updateShape(level, state, above, aboveState, isNorthConnected, isEastConnected, isSouthConnected, isWestConnected);
|
| 186 | }
|
| 187 |
|
| 188 | private BlockState updateShape(
|
| 189 | final LevelReader level,
|
| 190 | final BlockState state,
|
| 191 | final BlockPos topPos,
|
| 192 | final BlockState topNeighbour,
|
| 193 | final boolean north,
|
| 194 | final boolean east,
|
| 195 | final boolean south,
|
| 196 | final boolean west
|
| 197 | ) {
|
| 198 | VoxelShape aboveShape = topNeighbour.getCollisionShape(level, topPos).getFaceShape(Direction.DOWN);
|
| 199 | BlockState sidesUpdatedState = this.updateSides(state, north, east, south, west, aboveShape);
|
| 200 | return sidesUpdatedState.setValue(UP, this.shouldRaisePost(sidesUpdatedState, topNeighbour, aboveShape));
|
| 201 | }
|
| 202 |
|
| 203 | private boolean shouldRaisePost(final BlockState state, final BlockState topNeighbour, final VoxelShape aboveShape) {
|
| 204 | boolean topNeighbourHasPost = topNeighbour.getBlock() instanceof WallBlock && topNeighbour.getValue(UP);
|
| 205 | if (topNeighbourHasPost) {
|
| 206 | return true;
|
| 207 | }
|
| 208 |
|
| 209 | WallSide northWall = state.getValue(NORTH);
|
| 210 | WallSide southWall = state.getValue(SOUTH);
|
| 211 | WallSide eastWall = state.getValue(EAST);
|
| 212 | WallSide westWall = state.getValue(WEST);
|
| 213 | boolean southNone = southWall == WallSide.NONE;
|
| 214 | boolean westNone = westWall == WallSide.NONE;
|
| 215 | boolean eastNone = eastWall == WallSide.NONE;
|
| 216 | boolean northNone = northWall == WallSide.NONE;
|
| 217 | boolean hasCorner = northNone && southNone && westNone && eastNone || northNone != southNone || westNone != eastNone;
|
| 218 | if (hasCorner) {
|
| 219 | return true;
|
| 220 | }
|
| 221 |
|
| 222 | boolean hasHighWall = northWall == WallSide.TALL && southWall == WallSide.TALL || eastWall == WallSide.TALL && westWall == WallSide.TALL;
|
| 223 | return hasHighWall ? false : topNeighbour.is(BlockTags.WALL_POST_OVERRIDE) || isCovered(aboveShape, TEST_SHAPE_POST);
|
| 224 | }
|
| 225 |
|
| 226 | private BlockState updateSides(
|
| 227 | final BlockState state,
|
| 228 | final boolean northConnection,
|
| 229 | final boolean eastConnection,
|
| 230 | final boolean southConnection,
|
| 231 | final boolean westConnection,
|
| 232 | final VoxelShape aboveShape
|
| 233 | ) {
|
| 234 | return state.setValue(NORTH, this.makeWallState(northConnection, aboveShape, TEST_SHAPES_WALL.get(Direction.NORTH)))
|
| 235 | .setValue(EAST, this.makeWallState(eastConnection, aboveShape, TEST_SHAPES_WALL.get(Direction.EAST)))
|
| 236 | .setValue(SOUTH, this.makeWallState(southConnection, aboveShape, TEST_SHAPES_WALL.get(Direction.SOUTH)))
|
| 237 | .setValue(WEST, this.makeWallState(westConnection, aboveShape, TEST_SHAPES_WALL.get(Direction.WEST)));
|
| 238 | }
|
| 239 |
|
| 240 | private WallSide makeWallState(final boolean connectsToSide, final VoxelShape aboveShape, final VoxelShape testShape) {
|
| 241 | if (connectsToSide) {
|
| 242 | return isCovered(aboveShape, testShape) ? WallSide.TALL : WallSide.LOW;
|
| 243 | } else {
|
| 244 | return WallSide.NONE;
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | @Override
|
| 249 | protected FluidState getFluidState(final BlockState state) {
|
| 250 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 251 | }
|
| 252 |
|
| 253 | @Override
|
| 254 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 255 | return !state.getValue(WATERLOGGED);
|
| 256 | }
|
| 257 |
|
| 258 | @Override
|
| 259 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 260 | builder.add(UP, NORTH, EAST, WEST, SOUTH, WATERLOGGED);
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 265 | return switch (rotation) {
|
| 266 | case CLOCKWISE_180 -> (BlockState)state.setValue(NORTH, state.getValue(SOUTH))
|
| 267 | .setValue(EAST, state.getValue(WEST))
|
| 268 | .setValue(SOUTH, state.getValue(NORTH))
|
| 269 | .setValue(WEST, state.getValue(EAST));
|
| 270 | case COUNTERCLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(EAST))
|
| 271 | .setValue(EAST, state.getValue(SOUTH))
|
| 272 | .setValue(SOUTH, state.getValue(WEST))
|
| 273 | .setValue(WEST, state.getValue(NORTH));
|
| 274 | case CLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(WEST))
|
| 275 | .setValue(EAST, state.getValue(NORTH))
|
| 276 | .setValue(SOUTH, state.getValue(EAST))
|
| 277 | .setValue(WEST, state.getValue(SOUTH));
|
| 278 | default -> state;
|
| 279 | };
|
| 280 | }
|
| 281 |
|
| 282 | @Override
|
| 283 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 284 | switch (mirror) {
|
| 285 | case LEFT_RIGHT:
|
| 286 | return state.setValue(NORTH, state.getValue(SOUTH)).setValue(SOUTH, state.getValue(NORTH));
|
| 287 | case FRONT_BACK:
|
| 288 | return state.setValue(EAST, state.getValue(WEST)).setValue(WEST, state.getValue(EAST));
|
| 289 | default:
|
| 290 | return super.mirror(state, mirror);
|
| 291 | }
|
| 292 | }
|
| 293 | }
|
| 294 |
|