Get Block Behind player?

Discussion in 'Plugin Development' started by ron975, Aug 15, 2012.

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

    ron975

    I need a way to do this.. Which values should I modify with player.getLocation()?
     
  2. Offline

    Chiller

    Take the yaw % 90
    That should give you (round it): 1, 2, 3, or 4
    Each number stands for like -x or +x or -z or +z, but you gotta figure that out
     
  3. Offline

    ron975

    Uh.. Can I get some examples? I'm not very experienced in Java or Bukkit..
     
  4. Offline

    kyle1320

    After a bit of testing:

    -snip-

    Edit: Wait, don't use that code. For some reason yaw will go positive or negative, so I had to change it a bit:
    Code:
    World world = player.getWorld();
    Location loc = player.getLocation();
    Block behind = loc.getBlock();
    int direction = (int)loc.getYaw();
     
    if(direction < 0) {
        direction += 360;
        direction = (direction + 45) / 90;
    }else {
        direction = (direction + 45) / 90;
    }
     
    switch (direction) {
        case 1:
            behind = world.getBlockAt(behind.getX() + 1, behind.getY(), behind.getZ());
            break;
        case 2:
            behind = world.getBlockAt(behind.getX(), behind.getY(), behind.getZ() + 1);
            break;
        case 3:
            behind = world.getBlockAt(behind.getX() - 1, behind.getY(), behind.getZ());
            break;
        case 4:
            behind = world.getBlockAt(behind.getX(), behind.getY(), behind.getZ() - 1);
            break;
        case 0:
            behind = world.getBlockAt(behind.getX(), behind.getY(), behind.getZ() - 1);
            break;
        default:
            break;
    }
     
Thread Status:
Not open for further replies.

Share This Page