Solved Is Blocks Above

Discussion in 'Plugin Development' started by Ragnazar, Jul 20, 2013.

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

    Ragnazar

    I need method that returns is there any block except AIR above another block
    That what i have
    Code:java
    1. public boolean isBlocksAbove(Location loc){
    2. Block curr = loc.getBlock().getRelative(BlockFace.UP);
    3. while(curr.getType()==Material.AIR || curr.getY()<254){
    4. curr = curr.getRelative(BlockFace.UP);
    5. }
    6. return curr.getY()!=254;
    7. }

    But server stucks while i using this method
    Howq to do same thing correctly?
     
  2. Offline

    Bart

    Surely you mean != in the while loop?
     
  3. Your code barely makes sense. I like to just make a new location with the block above.
    Code:
    public boolean isBlockAbove(Location loc) {
    Location newLoc = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ());
    if (newLoc.getBlock() == null) return false;
    return newLoc.getBlock().getType() != Material.AIR;
    } 
     
  4. Offline

    Ragnazar

    Bart
    ehm, no. I mean while loop will get block above until hit Y=254 or block isnt AIR
    KingFaris11
    I mean not just one block above, but is there any blocks beteen block and sky
    (isBlocksAbove)
    KingFaris11
    Code:java
    1. [syntax=java][/syntax]
     
  5. Offline

    Bart

    Ragnazar - Oh sorry.

    You could just do

    Code:java
    1. if(loc.getWorld().getHighestBlockAt(loc).equals(loc.getBlock()) {
    2. // the block is the highest one, meaning there is only air above it.
    3. }
    4.  

    Or even save some time and write a function to do it:

    Code:java
    1.  
    2. public void isHighestBlock(Location l) {
    3. if(l.getWorld().getHighestBlockAt(l).equals(l.getBlock()) {
    4. return true;
    5. }
    6. return false;
    7. }
     
  6. Offline

    Ragnazar

    Bart
    Thanks! But getHighestBlockYAt returns int so correct use is
    Code:java
    1. public boolean isBlocksAbove(Location loc){
    2. return loc.getWorld().getHighestBlockYAt(loc)==254;
    3. }


    254 is max world Y, right? Or is it 255?
     
  7. Offline

    Bart

    My bad! Use the code I put but rename getHighestBlockYAt to just getHighestBlockAt. Editing into my previous post.
     
  8. Offline

    Delocaz

    Try this:

    Code:java
    1.  
    2. public boolean isHighestBlock(Location loc) {
    3. return loc.getWorld().getHighestBlockAt(loc) == loc.getBlock();
    4. }
    5.  
    6. And to use:
    7.  
    8. if (isHighestBlock(loc)) {
    9. //is highest
    10. }
    11.  
     
  9. Offline

    CrazyGuy3000

    Well, you could get all the blocks within 200 blocks above and if there != null or Material.air than add that, I don't have my SDK open atm :l, if you need the code leave a reply ill be bothered to code it together then.
     
Thread Status:
Not open for further replies.

Share This Page