Check : more than 5 air blocks under player.

Discussion in 'Spigot Plugin Development' started by Switch, May 30, 2016.

  1. Hello !
    I would like to check if there are more than 5 air blocks under player.
    Can you help me plz ? :D
    Switch
     
  2. One way of doing so can be with if
    Example:
    Code (Text):

    if (player.getLocation().add(0, -1, 0).getBlock().getType() == Material.AIR) {
                    if (player.getLocation().add(0, -2, 0).getBlock().getType() == Material.AIR) {
                        if (player.getLocation().add(0, -3, 0).getBlock().getType() == Material.AIR) {
                            if (player.getLocation().add(0, -4, 0).getBlock().getType() == Material.AIR) {
                                if (player.getLocation().add(0, -5, 0).getBlock().getType() == Material.AIR) {
                                    //YOU CODE//
                                }
                            }
                        }
                    }
                }
     
     
  3. Why not use &&
     
  4. If it can be, not I had has
     
  5. Iterate:

    Code (Java):
    Location base = player.getLocation();
    for (int i = 0; i < 5; i++) {
        if (base.subtract(0, 1, 0).getBlock().getType() != Material.AIR) {
            //relevant logic for finding non-air
            break;
        }
    }
    //only air found
    No sense repeating the code a bunch of times, you can just continue searching downwards for whatever limit you want.

    This can also be done using the Block class and using #getRelative with block faces
     
    • Agree Agree x 1
    • Winner Winner x 1