Best way to loop through all blocks UPWARDS

Discussion in 'Plugin Development' started by arjanforgames, Dec 10, 2013.

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

    arjanforgames

    What is the best way to get all blocks above a location and put them all in a list until you reach an "air block".
    I will use this for simulating certain blocks falling after explosions.
     
  2. Offline

    Warreo

    I believe this should work :)

    Code:java
    1. public ArrayList<Location> getBlocksAbove(Location loc){
    2. ArrayList<Location> locations = new ArrayList();
    3. while(loc.getBlockY() <= 256){
    4. if(loc.getBlock().getType() == Material.AIR){
    5. break;
    6. }
    7. locations.add(loc);
    8. loc.add(0, 1, 0);
    9. }
    10. return locations;
    11. }
     
  3. Offline

    JPG2000

    Warreo That won't work, you never actually loop through the block. You just create a while statement and never actually loop.
     
  4. Offline

    mydeblob

    arjanforgames
    You'r going to need to create a complete location and loop through it. That's pretty easy, just make a few for loops
    Code:java
    1. for(int x = minx; x<maxx; x++){
    2. for(int z = minz; z<maxz; z++){
    3. for(int y = miny; y<maxy; y++){
    4. if(world.getBlockAt(x, y, z).getType() == Material.AIR) {
    5. return;
    6. }else{
    7. //Do what you need with the blocks here
    8. }
    9. }
    10. }
    11. }

    The variables used in the for loops (minx, maxx, minz, maxz, miny, maxy) are just integer variables of the location. Adjust accordingly. I didn't actually test this code, so there may be some bugs
     
Thread Status:
Not open for further replies.

Share This Page