Solved .getTargetBlock() deprecated?

Discussion in 'Plugin Development' started by Maurdekye, Jan 10, 2014.

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

    Maurdekye

    Apparently Player.getTargetBlock() is a deprecated method. I haven't been able to find an equivalent to use instead, so I'm wondering how else I would get the block the player is looking at.
     
  2. Offline

    JRL1004

    Maurdekye You need to use a BlockIterator, like this:
    Code:java
    1. public static final Block getTarget(Player player, Integer range) {
    2. BlockIterator iter = new BlockIterator(player, range);
    3. Block lastBlock = iter.next();
    4. while (iter.hasNext()) {
    5. lastBlock = iter.next();
    6. if (lastBlock.getType() == Material.AIR)
    7. continue;
    8. break;
    9. }
    10. return lastBlock;
    11. }

    This method could throw an NPE if used wrong.
     
  3. Offline

    Maurdekye

    So the BlockIterator reads blocks down the line of sight of tbr player like a Scanner reads through files? Interesting. Also, under what circumstances could it throw an error, if you try to read a block outside of the world?

    Thank you nonetheless, that works the same and doesn't give deprecation warnings.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page