Sign Positioning

Discussion in 'Plugin Development' started by TheNewTao, Jul 17, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    TheNewTao

    Hi,

    So I am trying to make a simple command, when you do /createsign it places a sign on the block you are looking at. I thought it was going to be simple, but it has been really complicated for me. I'll explain my struggle:

    This is my code first of all for the command:

    Code:
           if (!(sender instanceof Player))
                return false;
            if (!cmd.getName().equals("createsign"))
                return false;
            Player p = (Player) sender;
            List<Block> blocks = p.getLastTwoTargetBlocks((HashSet<Byte>) null, 100);
            for (Block b : blocks) {
                b.setType(Material.WALL_SIGN);
    
                float dir = (float) Math.toDegrees(
                        Math.atan2(p.getLocation().getBlockX() - b.getX(), b.getZ() - p.getLocation().getBlockZ()));
    
                BlockFace face = getClosestFace(dir);
    
                // Get the block state as org.bukkit.block.Sign (you can also use
                // this to set text on the sign)
                Sign sign = (Sign) b.getState();
    
                // Create sign facing data with org.bukkit.material.Sign
                org.bukkit.material.Sign signData = new org.bukkit.material.Sign(Material.SIGN);
                Bukkit.broadcastMessage(face.getOppositeFace().name());
                signData.setFacingDirection(face);
                sign.setData(signData);
                b.getState().update();
                sign.update();
                break;
            }
    So what I am doing is I am getting the two last blocks where the player is looking and using the one that would be air and setting it to a WALL_SIGN, the sign stuff starts here:
    Code:
               b.setType(Material.WALL_SIGN);
    
                float dir = (float) Math.toDegrees(
                        Math.atan2(p.getLocation().getBlockX() - b.getX(), b.getZ() - p.getLocation().getBlockZ()));
    
                BlockFace face = getClosestFace(dir);
    
                // Get the block state as org.bukkit.block.Sign (you can also use
                // this to set text on the sign)
                Sign sign = (Sign) b.getState();
    
                // Create sign facing data with org.bukkit.material.Sign
                org.bukkit.material.Sign signData = new org.bukkit.material.Sign(Material.SIGN);
                Bukkit.broadcastMessage(face.getOppositeFace().name());
                signData.setFacingDirection(face.getOppositeFace());
                sign.setData(signData);
                b.getState().update();
                sign.update();
                break;
    It works perfectly when the sign is facing NORTH, when the sign is facing any other direction, it just breaks:
    [​IMG]

    So the player is facing EAST so the sign should be facing WEST, which is the debug message I am broadcasting, yet it still breaks. I don't know why, I believe it should work but it doesn't.

    Does anyone know what I am doing wrong or if there is a better approach to this issue?

    PS: the code for the getClosestFace() method, which just gives out the face the player is facing, is (it is not my code, it is someone else's code):
    Code:
        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.SOUTH; //
            case 1:
                return BlockFace.SOUTH_WEST; //
            case 2:
                return BlockFace.WEST; //
            case 3:
                return BlockFace.NORTH_WEST; //
            case 4:
                return BlockFace.NORTH; //
            case 5:
                return BlockFace.NORTH_EAST;
            case 6:
                return BlockFace.EAST; //
            case 7:
                return BlockFace.SOUTH_EAST;
            default:
                return BlockFace.SOUTH; //
    
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page