Using a BlockIterator to check above the player's head?

Discussion in 'Plugin Development' started by DrBoweNur, Jul 19, 2011.

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

    DrBowe

    Long story short, I'm pretty much brain dead when it comes to anything involving directional equations...

    So, what would be the best way to set up a BlockIterator that gets any blocks directly above the player? In essence, I'm just trying to check to see if they're outside.
    I know I saw a thread on this like...2 days ago, but after about 30mins of searching I've yet to find it.
     
  2. Offline

    loganwm

    This should work rather cleanly.

    Code:
    public boolean isPlayerOutside(Player player)
    {
    for (int by = player.getLocation().getBlockY(); by <= 127; by++)
    {
    if (player.getWorld().getBlockTypeIdAt(player.getLocation().getBlockX(), by, player.getLocation().getBlockZ() != 0) //if any block above the player isn't air, then we're not outside, so we can bail safely.
    {
    return false;
    }
    }
    
    return true;
    }
     
  3. Offline

    DrBowe

    *salutes*
    Thank you, my good sir.
     
  4. Offline

    Shamebot

    Code:java
    1. private boolean isOutside(Player player)
    2. {
    3. return player.getLocation().getBlockY() >= player.getWorld().getHighestBlockYAt(player.getLocation());
    4. }

    Should do the same but has better performance.
     
  5. Offline

    DrBowe

    @Shamebot
    Ah, thanks. I'll save both of these.
    Your code is simpler, and his allows me to add additional blocks to ignore (such as leaves on trees)

    Both are very helpful. :)
     
  6. Offline

    Shamebot

    You could mix them, iterating from the highest block down to the player. But probably won't make a difference.
     
Thread Status:
Not open for further replies.

Share This Page