| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import java.util.function.Function;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 7 | import net.minecraft.world.level.block.state.BlockState;
|
| 8 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 9 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 10 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 11 | import net.minecraft.world.phys.shapes.Shapes;
|
| 12 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 13 |
|
| 14 | public interface SegmentableBlock {
|
| 15 | int MIN_SEGMENT = 1;
|
| 16 | int MAX_SEGMENT = 4;
|
| 17 | IntegerProperty AMOUNT = BlockStateProperties.SEGMENT_AMOUNT;
|
| 18 |
|
| 19 | default Function<BlockState, VoxelShape> getShapeCalculator(final EnumProperty<Direction> facing, final IntegerProperty amount) {
|
| 20 | Map<Direction, VoxelShape> shapes = Shapes.rotateHorizontal(Block.box(0.0, 0.0, 0.0, 8.0, this.getShapeHeight(), 8.0));
|
| 21 | return state -> {
|
| 22 | VoxelShape shape = Shapes.empty();
|
| 23 | Direction direction = state.getValue(facing);
|
| 24 | int count = state.getValue(amount);
|
| 25 |
|
| 26 | for (int i = 0; i < count; i++) {
|
| 27 | shape = Shapes.or(shape, shapes.get(direction));
|
| 28 | direction = direction.getCounterClockWise();
|
| 29 | }
|
| 30 |
|
| 31 | return shape.singleEncompassing();
|
| 32 | };
|
| 33 | }
|
| 34 |
|
| 35 | default IntegerProperty getSegmentAmountProperty() {
|
| 36 | return AMOUNT;
|
| 37 | }
|
| 38 |
|
| 39 | default double getShapeHeight() {
|
| 40 | return 1.0;
|
| 41 | }
|
| 42 |
|
| 43 | default boolean canBeReplaced(final BlockState state, final BlockPlaceContext context, final IntegerProperty segment) {
|
| 44 | return !context.isSecondaryUseActive() && context.getItemInHand().is(state.getBlock().asItem()) && state.getValue(segment) < 4;
|
| 45 | }
|
| 46 |
|
| 47 | default BlockState getStateForPlacement(
|
| 48 | final BlockPlaceContext context, final Block block, final IntegerProperty segment, final EnumProperty<Direction> facing
|
| 49 | ) {
|
| 50 | BlockState state = context.getLevel().getBlockState(context.getClickedPos());
|
| 51 | return state.is(block)
|
| 52 | ? state.setValue(segment, Math.min(4, state.getValue(segment) + 1))
|
| 53 | : block.defaultBlockState().setValue(facing, context.getHorizontalDirection().getOpposite());
|
| 54 | }
|
| 55 | }
|
| 56 |
|