| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerPlayer;
|
| 6 | import net.minecraft.world.entity.player.Player;
|
| 7 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 8 | import net.minecraft.world.level.Level;
|
| 9 | import net.minecraft.world.level.block.Block;
|
| 10 | import net.minecraft.world.level.block.ScaffoldingBlock;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public class ScaffoldingBlockItem extends BlockItem {
|
| 15 | public ScaffoldingBlockItem(final Block block, final Item.Properties properties) {
|
| 16 | super(block, properties);
|
| 17 | }
|
| 18 |
|
| 19 | @Override
|
| 20 | public @Nullable BlockPlaceContext updatePlacementContext(final BlockPlaceContext context) {
|
| 21 | BlockPos pos = context.getClickedPos();
|
| 22 | Level level = context.getLevel();
|
| 23 | BlockState replacedState = level.getBlockState(pos);
|
| 24 | Block block = this.getBlock();
|
| 25 | if (!replacedState.is(block)) {
|
| 26 | return ScaffoldingBlock.getDistance(level, pos) == 7 ? null : context;
|
| 27 | }
|
| 28 |
|
| 29 | Direction direction;
|
| 30 | if (context.isSecondaryUseActive()) {
|
| 31 | direction = context.isInside() ? context.getClickedFace().getOpposite() : context.getClickedFace();
|
| 32 | } else {
|
| 33 | direction = context.getClickedFace() == Direction.UP ? context.getHorizontalDirection() : Direction.UP;
|
| 34 | }
|
| 35 |
|
| 36 | int horizontalDistance = 0;
|
| 37 | BlockPos.MutableBlockPos placementPos = pos.mutable().move(direction);
|
| 38 |
|
| 39 | while (horizontalDistance < 7) {
|
| 40 | if (!level.isClientSide() && !level.isInWorldBounds(placementPos)) {
|
| 41 | Player player = context.getPlayer();
|
| 42 | int maxY = level.getMaxY();
|
| 43 | if (player instanceof ServerPlayer serverPlayer && placementPos.getY() > maxY) {
|
| 44 | serverPlayer.sendBuildLimitMessage(true, maxY);
|
| 45 | }
|
| 46 | break;
|
| 47 | }
|
| 48 |
|
| 49 | replacedState = level.getBlockState(placementPos);
|
| 50 | if (!replacedState.is(this.getBlock())) {
|
| 51 | if (replacedState.canBeReplaced(context)) {
|
| 52 | return BlockPlaceContext.at(context, placementPos, direction);
|
| 53 | }
|
| 54 | break;
|
| 55 | }
|
| 56 |
|
| 57 | placementPos.move(direction);
|
| 58 | if (direction.getAxis().isHorizontal()) {
|
| 59 | horizontalDistance++;
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | return null;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected boolean mustSurvive() {
|
| 68 | return false;
|
| 69 | }
|
| 70 | }
|
| 71 |
|