Solved Get face of block player is looking at?

Discussion in 'Spigot Plugin Development' started by Vhbob, Jul 13, 2018.

  1. Title says it all. I need the BlockFace the player is looking at.
    I have tried:
    Code (Text):
    p.getLocation().getBlock().setType(Material.DIRT);
                                BlockFace blockFace = b.getFace(p.getLocation().getBlock());
                                p.getLocation().getBlock().setType(Material.AIR);
    But blockFace is null
     
  2. Hello! After doing some quick googling, I found this same question asked in pieces on a few different forums. The following code should solve your issue.

    The code you need to get the direction:
    Code (Java):
    float dir = (float) Math.toDegrees(Math.atan2(p.getLocation().getBlockX() - b.getX(), b.getZ() - p.getLocation().getBlockZ()));
    BlockFace face = getClosestFace(dir);
    The code you need to get the BlockFace from that direction.
    Code (Java):
        public BlockFace getClosestFace(float direction) {

            direction = direction % 360;

            if (direction < 0)
                direction += 360;

            direction = Math.round(direction / 45);

            switch ((int) direction) {
            case 0:
                return BlockFace.WEST;
            case 1:
                return BlockFace.NORTH_WEST;
            case 2:
                return BlockFace.NORTH;
            case 3:
                return BlockFace.NORTH_EAST;
            case 4:
                return BlockFace.EAST;
            case 5:
                return BlockFace.SOUTH_EAST;
            case 6:
                return BlockFace.SOUTH;
            case 7:
                return BlockFace.SOUTH_WEST;
            default:
                return BlockFace.WEST;
            }
        }
    Source: https://bukkit.org/threads/from-which-direction-is-player-looking-at-the-block.14153/
     
    • Agree Agree x 1
    • Like Like x 1
    • Winner Winner x 1
  3. It is still giving me a null pointer when i check if the block face is north/south:
    Code (Text):
    BlockFace blockFace = utils.getBlockFace(p);
                                if (blockFace.equals(BlockFace.NORTH) || blockFace.equals(BlockFace.SOUTH)) {
                                    b.setData((byte) 1);
                                } else {
                                    b.setData((byte) 0);
                                }

    Code (Text):

    public BlockFace getBlockFace(Player player) {
            List<Block> lastTwoTargetBlocks = player.getLastTwoTargetBlocks(null, 100);
            if (lastTwoTargetBlocks.size() != 2 || !lastTwoTargetBlocks.get(1).getType().isOccluding()) {
                return null;
            }
            Block targetBlock = lastTwoTargetBlocks.get(1);
            Block adjacentBlock = lastTwoTargetBlocks.get(0);
            return targetBlock.getFace(adjacentBlock);
        }
     
     
  4. It is because this:
    Code (Text):
    public BlockFace getBlockFace(Player player) {
            List<Block> lastTwoTargetBlocks = player.getLastTwoTargetBlocks(null, 100);
            if (lastTwoTargetBlocks.size() != 2 || !lastTwoTargetBlocks.get(1).getType().isOccluding()) {
                player.sendMessage("test");
                return null;
            }
            player.sendMessage("test12");
            Block targetBlock = lastTwoTargetBlocks.get(1);
            Block adjacentBlock = lastTwoTargetBlocks.get(0);
            return targetBlock.getFace(adjacentBlock);
        }
     
    is returning null. Anyone know why?
    Figure I should tag @Benz56 since he is the one who created this.
    Edit: It's because my block, an anvil, is no occluding. Any idea if removing this check will cause errors? I understand it might make it intuitive but?
     
  5. Ended up keeping the isOccluding line and just adding a message in for when the block is not occluding!