| 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.world.level.BlockGetter;
|
| 11 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 14 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 15 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 16 | import net.minecraft.world.phys.shapes.Shapes;
|
| 17 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 18 |
|
| 19 | public abstract class PipeBlock extends Block {
|
| 20 | public static final BooleanProperty NORTH = BlockStateProperties.NORTH;
|
| 21 | public static final BooleanProperty EAST = BlockStateProperties.EAST;
|
| 22 | public static final BooleanProperty SOUTH = BlockStateProperties.SOUTH;
|
| 23 | public static final BooleanProperty WEST = BlockStateProperties.WEST;
|
| 24 | public static final BooleanProperty UP = BlockStateProperties.UP;
|
| 25 | public static final BooleanProperty DOWN = BlockStateProperties.DOWN;
|
| 26 | public static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = ImmutableMap.copyOf(
|
| 27 | Maps.newEnumMap(Map.of(Direction.NORTH, NORTH, Direction.EAST, EAST, Direction.SOUTH, SOUTH, Direction.WEST, WEST, Direction.UP, UP, Direction.DOWN, DOWN))
|
| 28 | );
|
| 29 | private final Function<BlockState, VoxelShape> shapes;
|
| 30 |
|
| 31 | protected PipeBlock(final float size, final BlockBehaviour.Properties properties) {
|
| 32 | super(properties);
|
| 33 | this.shapes = this.makeShapes(size);
|
| 34 | }
|
| 35 |
|
| 36 | private Function<BlockState, VoxelShape> makeShapes(final float size) {
|
| 37 | VoxelShape core = Block.cube(size);
|
| 38 | Map<Direction, VoxelShape> shapes = Shapes.rotateAll(Block.boxZ(size, 0.0, 8.0));
|
| 39 | return this.getShapeForEachState(state -> {
|
| 40 | VoxelShape shape = core;
|
| 41 |
|
| 42 | for (Entry<Direction, BooleanProperty> entry : PROPERTY_BY_DIRECTION.entrySet()) {
|
| 43 | if (state.getValue(entry.getValue())) {
|
| 44 | shape = Shapes.or(shapes.get(entry.getKey()), shape);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | return shape;
|
| 49 | });
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 54 | return false;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 59 | return this.shapes.apply(state);
|
| 60 | }
|
| 61 | }
|
| 62 |
|