Solved How accurate are F3 coorinates compared to block API work?

Discussion in 'Spigot Plugin Development' started by LEOcab, Jan 9, 2020.

  1. I'm having trouble with the getBlockAt() function. The following code is supposed to tell you what block you clicked and what blocks are around it horizontally. Except it's not working, because the Z coordinate is off by one. I stood as close to the center of a block and clicked down.

    Code (Text):
    @EventHandler(priority=EventPriority.HIGH)
        public void onPlayerUse(PlayerInteractEvent event)
        {
            if (event.getHand() != EquipmentSlot.HAND) return; // ignore offhand event
            if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; // we only care about right click block
         
            Block block = event.getClickedBlock();
            Player p = event.getPlayer();
            Material item = p.getInventory().getItemInMainHand().getType();
         
            if (item == Material.ROTTEN_FLESH)
            {
                int x = block.getX(), y = block.getY(), z = block.getZ();
                Block[] adj = new Block[] {
                        block.getWorld().getBlockAt(new Location(block.getWorld(), x-1, y, z)),
                        block.getWorld().getBlockAt(new Location(block.getWorld(), x+1, y, z)),
                        block.getWorld().getBlockAt(new Location(block.getWorld(), x, y, z-1)),
                        block.getWorld().getBlockAt(new Location(block.getWorld(), x, y, z+1))
                    };
             
                String fmt = String.format("You clicked on a " + block.getBlockData().getAsString() + " block at %d,%d,%d.", x, y, z);
                Plugin.sendMessage(p, fmt);
                Plugin.sendMessage(p, "To the WEST, there is a " + adj[0].getBlockData().getAsString() + " block.");
                Plugin.sendMessage(p, "To the EAST, there is a " + adj[1].getBlockData().getAsString() + " block.");
                Plugin.sendMessage(p, "To the NORTH, there is a " + adj[2].getBlockData().getAsString() + " block.");
                Plugin.sendMessage(p, "To the SOUTH, there is a " + adj[3].getBlockData().getAsString() + " block.");
            }
        }
    It returned 1539,70,-597. The coordinates for me on my client were 1539,71,-596. The y=71 makes sense because I was standing on top of the block I clicked, but the X/Z coordinates should've matched...

    EDIT: I forgot to mention - all the block names it spits out are correct. The clicked block, east/west, north/south all report the correct block. It's just the damn Z coord that's off... I thought it might be a bug so I just updated my server, same result...
     
    #1 LEOcab, Jan 9, 2020
    Last edited: Jan 9, 2020
  2. Negative coordinates are offset by -1, just +1 to your calculation
     
  3. Okx

    Okx

    Were you looking at the XYZ: line or the Block: line in F3?
     
  4. Er... Never looked at the block line LOL. I'm stupid. Guess entity coords have negative zero but block coords don't? Lemme call Notch real quick about that.

    Didn't have to lol, once I tried the block line in F3 everything started to make sense. Plus my code was messed up too so I was doubly confused.

    Thanks a lot guys!!
     
  5. It's not negative zero.. it's (for example) negaitive 1/2 which is -0.5. Blocks don't have it because the value is round.
     
  6. this. to elaborate further, its a bit confusing: 0,0 (x,z) is right inbetween 4 blocks. locations without added decimal numbers will be right on a line between two blocks (like a plane grid for example). this should be a given. now, because players can stand between these lines, for instance, -0.5,0, they'd be inbetween -1,0 and 0,0, but they're standing on the -1,0 block. but when it gets rounded by standard java, the decimal gets cut off (this happens if you cast a double to an int).