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.chat.Component;
import net.minecraft.network.protocol.game.ClientboundChatPacket;
import net.minecraft.Util;
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.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Block;

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