package net.minecraft.world.item;

import javax.annotation.Nullable;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.ScaffoldingBlock;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundChatPacket;
import net.minecraft.network.chat.ChatType;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Block;

public class ScaffoldingBlockItem extends BlockItem {
    public ScaffoldingBlockItem(final Block bph, final Properties a) {
        super(bph, a);
    }
    
    @Nullable
    @Override
    public BlockPlaceContext updatePlacementContext(final BlockPlaceContext bcq) {
        final BlockPos fk3 = bcq.getClickedPos();
        final Level bjw4 = bcq.getLevel();
        BlockState byj5 = bjw4.getBlockState(fk3);
        final Block bph6 = this.getBlock();
        if (byj5.getBlock() == bph6) {
            Direction fp7;
            if (bcq.isSecondaryUseActive()) {
                fp7 = (bcq.isInside() ? bcq.getClickedFace().getOpposite() : bcq.getClickedFace());
            }
            else {
                fp7 = ((bcq.getClickedFace() == Direction.UP) ? bcq.getHorizontalDirection() : Direction.UP);
            }
            int integer8 = 0;
            final BlockPos.MutableBlockPos a9 = new BlockPos.MutableBlockPos(fk3).move(fp7);
            while (integer8 < 7) {
                if (!bjw4.isClientSide && !Level.isInWorldBounds(a9)) {
                    final Player ayj10 = bcq.getPlayer();
                    final int integer9 = bjw4.getMaxBuildHeight();
                    if (ayj10 instanceof ServerPlayer && a9.getY() >= integer9) {
                        final ClientboundChatPacket mm12 = new ClientboundChatPacket(new TranslatableComponent("build.tooHigh", new Object[] { integer9 }).withStyle(ChatFormatting.RED), ChatType.GAME_INFO);
                        ((ServerPlayer)ayj10).connection.send(mm12);
                        break;
                    }
                    break;
                }
                else {
                    byj5 = bjw4.getBlockState(a9);
                    if (byj5.getBlock() != this.getBlock()) {
                        if (byj5.canBeReplaced(bcq)) {
                            return BlockPlaceContext.at(bcq, a9, fp7);
                        }
                        break;
                    }
                    else {
                        a9.move(fp7);
                        if (!fp7.getAxis().isHorizontal()) {
                            continue;
                        }
                        ++integer8;
                    }
                }
            }
            return null;
        }
        if (ScaffoldingBlock.getDistance(bjw4, fk3) == 7) {
            return null;
        }
        return bcq;
    }
    
    @Override
    protected boolean mustSurvive() {
        return false;
    }
}
