Solved Block Above Player

Discussion in 'Plugin Development' started by iWareWolf, May 17, 2014.

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

    iWareWolf

    How can I check if there is a block above a player? I want to make rain poisonous.
     
  2. Offline

    Insomniac10102

    If you want to check if there is a block directly above the player's head, you can use:

    Code:java
    1. Block above = player.getEyeLocation().add(0,1,0).getBlock();
    2.  
    3. if(above.type != Material.AIR)
    4. {
    5. //There's a block above player. Do something.
    6. }


    If you want to do a more extensive search to see if there's any block above the player whatsoever, you'll need a slightly more complex approach:

    Code:java
    1. Location loc = player.getEyeLocation().add(0,1,0);
    2.  
    3. while(loc.getY() < 256)
    4. {
    5. if(loc.getBlock().getType() != Material.air)
    6. {
    7. //There's a block above player. Do something.
    8. break;
    9. }
    10. loc.add(0,1,0);
    11. }
     
  3. Offline

    rfsantos1996

    getWorld().getHighestBlockAt(<player's X>, <player's Z>).getY() -> if player.getEyeLocation().getY() is greater than the block Y, there isn't a block above his head
     
  4. Offline

    Azubuso

    iWareWolf Returns the block above the player:
    PHP:
    /**
        *
        * @param player The player to run the check against
        * @return The block or null
        */
        
    public static Block getBlockAboveHead(Player player) {
            
    World world player.getWorld();
            
    Block above world.getBlockAt(player.getLocation().getX(), player.getEyeHeight() + 1,            player.getLocation().getZ());
            return 
    above;
    }
     
  5. Offline

    iWareWolf

    I mean if a block is above player at all. It could be 4 blocks above and such.
     
  6. Offline

    rsod

    iWareWolf
    Code:
    p.getLocation().getBlockY() < p.getWorld().getHighestBlockYAt(p.getLocation())
    will return false if there is a block above
     
  7. Offline

    iWareWolf

    Basically if a player is under a roof or not
     
  8. Offline

    Insomniac10102

    You'll have to set some standard for what you consider "being under a roof". what rsod suggested will check every single block above the player until max server height. If you only want to consider it a roof if the block is within 4 or so blocks, you can modify my original code to stop at your max roof height.
     
  9. Offline

    iWareWolf

    Insomniac10102

    Sorry for the misunderstanding. I would like there to be no max limit. As long as there is a block above a player at any distance. I thought it was clear since I said if the player was exposed to rain,sunlight.
     
  10. Offline

    NathanWolf


    iWareWolf , rfsantos already gave you what you need. getWorld().getHighestBlockAt() is the key.
     
    rfsantos1996 likes this.
  11. Offline

    xTigerRebornx

    iWareWolf If you want that, then simply get the highest block at the Player's x and z coordinates, and then see if that Block's Y coordinates are higher (above) or lower (below) the Player's Y coordinates. You can get the highest block's Y coord using World#getHighestBlockYAt() and then compare it as you see needed.
     
  12. Offline

    iWareWolf

    xTigerRebornx Would getHighestBlockAt(Location loc) go to the highest possible place which means this can return air?
     
  13. Offline

    rsod

    iWareWolf
    This will return first not air block from the top. The method is self-explaining, it will give you highest block at the following location.
     
  14. Offline

    iWareWolf

    rsod
    Thanks the code I used was:

    Code:
                        if (player.getLocation().getBlockY() + 1 > player.getWorld().getHighestBlockYAt(player.getLocation())) {
                            player.damage(.5);
                        }
     
Thread Status:
Not open for further replies.

Share This Page