| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.world.entity.item.FallingBlockEntity;
|
| 8 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.Level;
|
| 11 | import net.minecraft.world.level.LevelReader;
|
| 12 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 13 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 16 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 17 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 18 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 19 | import net.minecraft.world.level.material.FluidState;
|
| 20 | import net.minecraft.world.level.material.Fluids;
|
| 21 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 22 | import net.minecraft.world.phys.shapes.Shapes;
|
| 23 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 24 |
|
| 25 | /**
|
| 26 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 27 | */
|
| 28 | public class ScaffoldingBlock extends Block implements SimpleWaterloggedBlock {
|
| 29 | private static final int TICK_DELAY = 1;
|
| 30 | private static final VoxelShape SHAPE_STABLE = Shapes.or(
|
| 31 | Block.column(16.0, 14.0, 16.0), Shapes.rotateHorizontal(Block.box(0.0, 0.0, 0.0, 2.0, 16.0, 2.0)).values().stream().reduce(Shapes.empty(), Shapes::or)
|
| 32 | );
|
| 33 | private static final VoxelShape SHAPE_UNSTABLE_BOTTOM = Block.column(16.0, 0.0, 2.0);
|
| 34 | private static final VoxelShape SHAPE_UNSTABLE = Shapes.or(
|
| 35 | SHAPE_STABLE, SHAPE_UNSTABLE_BOTTOM, Shapes.rotateHorizontal(Block.boxZ(16.0, 0.0, 2.0, 0.0, 2.0)).values().stream().reduce(Shapes.empty(), Shapes::or)
|
| 36 | );
|
| 37 | private static final VoxelShape SHAPE_BELOW_BLOCK = Shapes.block().move(0.0, -1.0, 0.0).optimize();
|
| 38 | public static final int STABILITY_MAX_DISTANCE = 7;
|
| 39 | public static final IntegerProperty DISTANCE = BlockStateProperties.STABILITY_DISTANCE;
|
| 40 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 41 | public static final BooleanProperty BOTTOM = BlockStateProperties.BOTTOM;
|
| 42 |
|
| 43 | /**
|
| 44 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 45 | */
|
| 46 | public ScaffoldingBlock(final BlockBehaviour.Properties properties) {
|
| 47 | super(properties);
|
| 48 | this.registerDefaultState(this.stateDefinition.any().setValue(DISTANCE, 7).setValue(WATERLOGGED, false).setValue(BOTTOM, false));
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 53 | builder.add(DISTANCE, WATERLOGGED, BOTTOM);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 58 | if (!context.isHoldingItem(state.getBlock().asItem())) {
|
| 59 | return state.getValue(BOTTOM) ? SHAPE_UNSTABLE : SHAPE_STABLE;
|
| 60 | } else {
|
| 61 | return Shapes.block();
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected VoxelShape getInteractionShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 67 | return Shapes.block();
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
|
| 72 | return context.getItemInHand().is(this.asItem());
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 77 | BlockPos pos = context.getClickedPos();
|
| 78 | Level level = context.getLevel();
|
| 79 | int distance = getDistance(level, pos);
|
| 80 | return this.defaultBlockState()
|
| 81 | .setValue(WATERLOGGED, level.getFluidState(pos).is(Fluids.WATER))
|
| 82 | .setValue(DISTANCE, distance)
|
| 83 | .setValue(BOTTOM, this.isBottom(level, pos, distance));
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 88 | if (!level.isClientSide()) {
|
| 89 | level.scheduleTick(pos, this, 1);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected BlockState updateShape(
|
| 95 | final BlockState state,
|
| 96 | final LevelReader level,
|
| 97 | final ScheduledTickAccess ticks,
|
| 98 | final BlockPos pos,
|
| 99 | final Direction directionToNeighbour,
|
| 100 | final BlockPos neighbourPos,
|
| 101 | final BlockState neighbourState,
|
| 102 | final RandomSource random
|
| 103 | ) {
|
| 104 | if (state.getValue(WATERLOGGED)) {
|
| 105 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 106 | }
|
| 107 |
|
| 108 | if (!level.isClientSide()) {
|
| 109 | ticks.scheduleTick(pos, this, 1);
|
| 110 | }
|
| 111 |
|
| 112 | return state;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 117 | int distance = getDistance(level, pos);
|
| 118 | BlockState newState = state.setValue(DISTANCE, distance).setValue(BOTTOM, this.isBottom(level, pos, distance));
|
| 119 | if (newState.getValue(DISTANCE) == 7) {
|
| 120 | if (state.getValue(DISTANCE) == 7) {
|
| 121 | FallingBlockEntity.fall(level, pos, newState);
|
| 122 | } else {
|
| 123 | level.destroyBlock(pos, true);
|
| 124 | }
|
| 125 | } else if (state != newState) {
|
| 126 | level.setBlockAndUpdate(pos, newState);
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 132 | return getDistance(level, pos) < 7;
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 137 | if (context.isPlacement()) {
|
| 138 | return Shapes.empty();
|
| 139 | } else if (context.isAbove(Shapes.block(), pos, true) && !context.isDescending()) {
|
| 140 | return SHAPE_STABLE;
|
| 141 | } else {
|
| 142 | return state.getValue(DISTANCE) != 0 && state.getValue(BOTTOM) && context.isAbove(SHAPE_BELOW_BLOCK, pos, true) ? SHAPE_UNSTABLE_BOTTOM : Shapes.empty();
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | protected FluidState getFluidState(final BlockState state) {
|
| 148 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 149 | }
|
| 150 |
|
| 151 | private boolean isBottom(final BlockGetter level, final BlockPos pos, final int distance) {
|
| 152 | return distance > 0 && !level.getBlockState(pos.below()).is(this);
|
| 153 | }
|
| 154 |
|
| 155 | public static int getDistance(final BlockGetter level, final BlockPos pos) {
|
| 156 | BlockPos.MutableBlockPos relativePos = pos.mutable().move(Direction.DOWN);
|
| 157 | BlockState belowState = level.getBlockState(relativePos);
|
| 158 | int distance = 7;
|
| 159 | if (belowState.is(Blocks.SCAFFOLDING)) {
|
| 160 | distance = belowState.getValue(DISTANCE);
|
| 161 | } else if (belowState.isFaceSturdy(level, relativePos, Direction.UP)) {
|
| 162 | return 0;
|
| 163 | }
|
| 164 |
|
| 165 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 166 | BlockState relativeState = level.getBlockState(relativePos.setWithOffset(pos, direction));
|
| 167 | if (relativeState.is(Blocks.SCAFFOLDING)) {
|
| 168 | distance = Math.min(distance, relativeState.getValue(DISTANCE) + 1);
|
| 169 | if (distance == 1) {
|
| 170 | break;
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | return distance;
|
| 176 | }
|
| 177 | }
|
| 178 |
|