package net.minecraft.world.item;

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

public class BlockPlaceContext extends UseOnContext {
    private final BlockPos relativePos;
    protected boolean replaceClicked;
    
    public BlockPlaceContext(final Player bec, final InteractionHand anf, final ItemStack bki, final BlockHitResult deh) {
        this(bec.level, bec, anf, bki, deh);
    }
    
    public BlockPlaceContext(final UseOnContext blv) {
        this(blv.getLevel(), blv.getPlayer(), blv.getHand(), blv.getItemInHand(), blv.hitResult);
    }
    
    protected BlockPlaceContext(final Level bqb, @Nullable final Player bec, final InteractionHand anf, final ItemStack bki, final BlockHitResult deh) {
        super(bqb, bec, anf, bki, deh);
        this.replaceClicked = true;
        this.relativePos = deh.getBlockPos().relative(deh.getDirection());
        this.replaceClicked = bqb.getBlockState(deh.getBlockPos()).canBeReplaced(this);
    }
    
    public static BlockPlaceContext at(final BlockPlaceContext bin, final BlockPos fu, final Direction fz) {
        return new BlockPlaceContext(bin.getLevel(), bin.getPlayer(), bin.getHand(), bin.getItemInHand(), new BlockHitResult(new Vec3(fu.getX() + 0.5 + fz.getStepX() * 0.5, fu.getY() + 0.5 + fz.getStepY() * 0.5, fu.getZ() + 0.5 + fz.getStepZ() * 0.5), fz, fu, 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 fz3;
        int integer4;
        for (fz3 = this.getClickedFace(), integer4 = 0; integer4 < arr2.length && arr2[integer4] != fz3.getOpposite(); ++integer4) {}
        if (integer4 > 0) {
            System.arraycopy(arr2, 0, arr2, 1, integer4);
            arr2[0] = fz3.getOpposite();
        }
        return arr2;
    }
}
