Solved Blocks above player

Discussion in 'Plugin Development' started by joshzen, Jul 2, 2015.

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

    joshzen

    Hey guys,
    I'm making a Vampire plugin and I need to check if there are any blocks above the player.
    Is there anyone that knows how to do that?

    Thx for reading
     
  2. there are different ways to do it, you can get the player location and just add something in the Y axis and then get the block, and if you want it to be a bit more accurate, then you can check if he's near an edge of a block and also check the block on the other side of the edge. Or if you want it to get all the blocks above you and not just the one that you get with adding something to the player location, you can get the AxisAlignedBB bounding box of the player and then grow the bounding box in the Y axis and then check in a 3x3 pattern above the player if the blocks collide with the bounding box and then you have the blocks that are above you. The last one is pretty complicated and I recommend to use the first one if that's enough.
     
  3. @joshzen Try something like this:
    Code:
    public void onMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        Location loc = new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY() + 2, player.getLocation().getZ());
    if (player.getWorld().getBlockAt(loc).getType() == Material.AIR) {
            // There isn't a block
        } else {
            // There is a block
        }
    }
    I haven't tested it, so if there's any errors, just tag me. Anyway, I hope this helped. :)
     
    joshzen likes this.
  4. Offline

    _Error

    My code might be more simple.

    Code:
    @EventHandler
    public void MoveEvent(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    Location loc = p.getLocation();
    loc.setY(loc.getY() - 2);
    if(p.getWorld().getBlockAt(loc).getType() == Material.AIR){
    //there is a block
    }else{
    //there is no block
    }
    
    }
    
     
    joshzen likes this.
  5. @_Error Yeah, I see what you mean, but there's still some flaw.
    1. You're trying to get the Y coordinate under the player. (loc.setY(loc.getY() - 2)
    2. You switched it around, it should be like:
    Code:
    if (p.getWorld().getBlockAt(loc).getType() == Material.AIR) {
    // there isn't a block
    } else {
    // there is a block
    }
    I know, no one's perfect. Just wanted to point it out so OP isn't confused. :)
     
    joshzen likes this.
  6. Offline

    _Error

    Weird enough, - 2 is above the player, already tested, under him will be - -2 I don't understand why either.
     
    joshzen likes this.
  7. To see if their is a block above then you can just assign a b variable and get the block above.
    Block b = e.getPlayer().getLocation().add(0, 1, 0).getBlock(); //Args: X, Y, Z - This will get the block above the player, then just tweak the Y value to how many blocks above you want to check. After this you may have to do a null check.
    After that you can just return a value.

    Sample code:
    Code:
    //This method returns if there is a block above
    public boolean isBlockAbove(Player player){
        //Getting the block 5 blocks above the player
        Block b = player.getLocation().add(0, 5, 0).getBlock();
        //If the block is not air it will return true, otherwise return false
        return b.getType() != Material.AIR ? true : false;
        /*
        * You can also use this one:
        * if(b.getType() != Material.AIR){
        *     return true;
        * }else{
        *     return false;
        * }
        */
    }
     
    joshzen likes this.
  8. Offline

    Ruptur

    @bwfcwalshy
    Not sure this will work because it only checks at a specific coord above the player.

    @joshzen
    You need to itelerate through all the blocks above the players head and check if its air or not
    Code:
        public boolean isBlockAbove(Player player) {
            Location location = player.getLocation(); //location to start searching from
            int highest = location.getWorld().getHighestBlockYAt(location); // the world's highest block
          
            for (int y = location.getBlockY(); y <= highest;y++) {
                location.setY(y); // set the location y coord.
                Block block = location.getBlock(); // get the new block at the new y coord
              
                if (block.getType() != Material.AIR) //return true if the block is not air
                    return true;
              
            }
            return false;
        }
    
    If you want to check if their is a block above a player's head for a certain raduis (like what bwfcwalshy was trying to show)
    Code:
        public boolean isBlockAbove(Player player, int radius) {
            Location location = player.getLocation();
            int current = location.getBlockY();
            int highest = location.getWorld().getHighestBlockYAt(location);
          
            highest = (radius+current) > highest ? highest : current + radius; //same as normal but here the highest if the raduis + the player's current y coord. 
    
            for (int y = location.getBlockY(); y <= highest;y++) {
                location.setY(y);
                Block block = location.getBlock();
    
                if (block.getType() != Material.AIR)
                    return true;
    
            }
            return false;
        }
    
    Leave a like if you feel i helped :)
     
    Last edited: Jul 2, 2015
    joshzen likes this.
  9. Offline

    joshzen

    Thx guys!
     
  10. Offline

    Ruptur

    @joshzen
    Dont forget to set the thread prefix to solved
     
Thread Status:
Not open for further replies.

Share This Page