| 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.util.Util;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 13 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 14 | import net.minecraft.world.level.material.FluidState;
|
| 15 | import net.minecraft.world.level.material.Fluids;
|
| 16 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 17 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 18 | import net.minecraft.world.phys.shapes.Shapes;
|
| 19 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 20 |
|
| 21 | public abstract class CrossCollisionBlock extends Block implements SimpleWaterloggedBlock {
|
| 22 | public static final BooleanProperty NORTH = PipeBlock.NORTH;
|
| 23 | public static final BooleanProperty EAST = PipeBlock.EAST;
|
| 24 | public static final BooleanProperty SOUTH = PipeBlock.SOUTH;
|
| 25 | public static final BooleanProperty WEST = PipeBlock.WEST;
|
| 26 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 27 | public static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = PipeBlock.PROPERTY_BY_DIRECTION
|
| 28 | .entrySet()
|
| 29 | .stream()
|
| 30 | .filter(e -> e.getKey().getAxis().isHorizontal())
|
| 31 | .collect(Util.toMap());
|
| 32 | private final Function<BlockState, VoxelShape> collisionShapes;
|
| 33 | private final Function<BlockState, VoxelShape> shapes;
|
| 34 |
|
| 35 | protected CrossCollisionBlock(
|
| 36 | final float postWidth,
|
| 37 | final float postHeight,
|
| 38 | final float wallWidth,
|
| 39 | final float wallHeight,
|
| 40 | final float collisionHeight,
|
| 41 | final BlockBehaviour.Properties properties
|
| 42 | ) {
|
| 43 | super(properties);
|
| 44 | this.collisionShapes = this.makeShapes(postWidth, collisionHeight, wallWidth, 0.0F, collisionHeight);
|
| 45 | this.shapes = this.makeShapes(postWidth, postHeight, wallWidth, 0.0F, wallHeight);
|
| 46 | }
|
| 47 |
|
| 48 | protected Function<BlockState, VoxelShape> makeShapes(
|
| 49 | final float postWidth, final float postHeight, final float wallWidth, final float wallBottom, final float wallTop
|
| 50 | ) {
|
| 51 | VoxelShape post = Block.column(postWidth, 0.0, postHeight);
|
| 52 | Map<Direction, VoxelShape> arms = Shapes.rotateHorizontal(Block.boxZ(wallWidth, wallBottom, wallTop, 0.0, 8.0));
|
| 53 | return this.getShapeForEachState(state -> {
|
| 54 | VoxelShape shape = post;
|
| 55 |
|
| 56 | for (Entry<Direction, BooleanProperty> entry : PROPERTY_BY_DIRECTION.entrySet()) {
|
| 57 | if (state.getValue(entry.getValue())) {
|
| 58 | shape = Shapes.or(shape, arms.get(entry.getKey()));
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | return shape;
|
| 63 | }, WATERLOGGED);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 68 | return !state.getValue(WATERLOGGED);
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 73 | return this.shapes.apply(state);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 78 | return this.collisionShapes.apply(state);
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected FluidState getFluidState(final BlockState state) {
|
| 83 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 88 | return false;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 93 | return switch (rotation) {
|
| 94 | case CLOCKWISE_180 -> (BlockState)state.setValue(NORTH, state.getValue(SOUTH))
|
| 95 | .setValue(EAST, state.getValue(WEST))
|
| 96 | .setValue(SOUTH, state.getValue(NORTH))
|
| 97 | .setValue(WEST, state.getValue(EAST));
|
| 98 | case COUNTERCLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(EAST))
|
| 99 | .setValue(EAST, state.getValue(SOUTH))
|
| 100 | .setValue(SOUTH, state.getValue(WEST))
|
| 101 | .setValue(WEST, state.getValue(NORTH));
|
| 102 | case CLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(WEST))
|
| 103 | .setValue(EAST, state.getValue(NORTH))
|
| 104 | .setValue(SOUTH, state.getValue(EAST))
|
| 105 | .setValue(WEST, state.getValue(SOUTH));
|
| 106 | default -> state;
|
| 107 | };
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 112 | switch (mirror) {
|
| 113 | case LEFT_RIGHT:
|
| 114 | return state.setValue(NORTH, state.getValue(SOUTH)).setValue(SOUTH, state.getValue(NORTH));
|
| 115 | case FRONT_BACK:
|
| 116 | return state.setValue(EAST, state.getValue(WEST)).setValue(WEST, state.getValue(EAST));
|
| 117 | default:
|
| 118 | return super.mirror(state, mirror);
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|