package net.minecraft.world.item;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import net.minecraft.core.Direction;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import javax.annotation.Nullable;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.core.BlockPos;

public class BlockPlaceContext extends UseOnContext {
    private final BlockPos relativePos;
    protected boolean replaceClicked;
    
    public BlockPlaceContext(final UseOnContext bfz) {
        this(bfz.getLevel(), bfz.getPlayer(), bfz.getHand(), bfz.getItemInHand(), bfz.hitResult);
    }
    
    protected BlockPlaceContext(final Level bjw, @Nullable final Player ayj, final InteractionHand ajk, final ItemStack ben, final BlockHitResult cvg) {
        super(bjw, ayj, ajk, ben, cvg);
        this.replaceClicked = true;
        this.relativePos = cvg.getBlockPos().relative(cvg.getDirection());
        this.replaceClicked = bjw.getBlockState(cvg.getBlockPos()).canBeReplaced(this);
    }
    
    public static BlockPlaceContext at(final BlockPlaceContext bcq, final BlockPos fk, final Direction fp) {
        return new BlockPlaceContext(bcq.getLevel(), bcq.getPlayer(), bcq.getHand(), bcq.getItemInHand(), new BlockHitResult(new Vec3(fk.getX() + 0.5 + fp.getStepX() * 0.5, fk.getY() + 0.5 + fp.getStepY() * 0.5, fk.getZ() + 0.5 + fp.getStepZ() * 0.5), fp, fk, false));
    }
    
    @Override
    public BlockPos getClickedPos() {
        return this.replaceClicked ? super.getClickedPos() : this.relativePos;
    }
    
    public boolean canPlace() {
        return this.replaceClicked || this.getLevel().getBlockState(this.getClickedPos()).canBeReplaced(this);
    }
    
    public boolean replacingClickedOnBlock() {
        return this.replaceClicked;
    }
    
    public Direction getNearestLookingDirection() {
        return Direction.orderedByNearest(this.player)[0];
    }
    
    public Direction[] getNearestLookingDirections() {
        final Direction[] arr2 = Direction.orderedByNearest(this.player);
        if (this.replaceClicked) {
            return arr2;
        }
        Direction fp3;
        int integer4;
        for (fp3 = this.getClickedFace(), integer4 = 0; integer4 < arr2.length && arr2[integer4] != fp3.getOpposite(); ++integer4) {}
        if (integer4 > 0) {
            System.arraycopy(arr2, 0, arr2, 1, integer4);
            arr2[0] = fp3.getOpposite();
        }
        return arr2;
    }
}
