Get the block in front of player

Discussion in 'Plugin Development' started by BloodShura, Feb 21, 2012.

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

    BloodShura

    Is there a way to get the block in front of player? I tried using locations, get relatives, but nothing. ._.
     
  2. Offline

    dillyg10

    Just get the location of the block, and then get the location of the player, compare the values, and yeah :D
     
  3. Offline

    BloodShura

    But I don't have the location of the block, I have to get the location of the block. ._.
     
  4. Offline

    dillyg10

    WHy do you need to get the pluck directly in front of the player?
     
  5. Offline

    BloodShura

    Because I need. ¬¬
     
  6. Offline

    Firefly

    It depends which block in front of the player. I'll leave out a bit of code...

    PHP:
    Location playerLoc player.getLocation();
    playerLoc.setX(playerLoc.getX() + 1);
    //Yada yada yada
    However, now that I look at this it would matter which direction the player was facing. I'm not familiar with getDirection(), but you would have to do some Vector math in order to properly get the block in front of the player no matter which direction they are facing. Tell me if you figure this out, I'd like to know.
     
  7. Offline

    BloodShura

    Firefly
    Yes, because the front block can be X+ or Z+.

    I'll try using some vectors and blockface now.
     
  8. Offline

    dillyg10

    Yeah, me too :D...
     
  9. Offline

    BloodShura

    Ah, yea, one question: the player location, is the location of the legs/feet of the player, or body/head?
     
  10. Offline

    dillyg10

    That is a great question...
     
  11. Offline

    Firefly

    I believe its the feet. You can always do a quick test with...

    PHP:
    Location loc player.getLocation();
    loc.setY(loc.getY() - 1);
    Block block getBlockAtLocation(loc);
    block.setType(Material.STONE);
    If the block is right below the feet, then you know player.getLocation() returns the location of the block in the players legs :)
     
  12. Offline

    dillyg10

    Bukkit really should add this to the API's ...
     
  13. Offline

    Firefly

    I agree. Directions, Vectors, Velocity, etc really messes me up and is rather confusing.
     
  14. Offline

    BloodShura

    I do (player.getLocation().getBlock().setTypeId(5);) and YES, the location is in the legs/feet. =]

    If they add BlockFace.FRONT, that would be VERY GREAT! =]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  15. Offline

    mushroomhostage

    Any reason you can't use player.getTargetBlock()? This will return the block in the player's crosshairs.
     
  16. Offline

    BloodShura

    Well, while I don't have a solution for this, I'll use targetblock. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page