Getting blocks of a chunk

Discussion in 'Plugin Development' started by Anrza, Sep 13, 2014.

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

    Anrza

    So, I wanna get all blocks(locations) of a chunk, to get them, I have this method:

    Code:java
    1. ArrayList<Location> locations = new ArrayList<Location>();
    2. Integer x = chunk.getX() * 16;
    3. Integer z = chunk.getZ() * 16;
    4. Integer y = 0;
    5. Boolean keepOnDoingShit = true;
    6. while (keepOnDoingShit) {
    7. Location location = new Location(chunk.getWorld(), x, y, z);
    8. locations.add(location);
    9. if (x < 15) {
    10. x = x + 1;
    11. } else if (z < 15) {
    12. x = 0;
    13. z = z + 1;
    14. } else {
    15. x = 0;
    16. z = 0;
    17. y = y + 1;
    18. if (y == chunk.getWorld().getMaxHeight() + 1) {
    19. keepOnDoingShit = false;
    20. }
    21. }
    22. }


    I haven't tried it, but I think it should work.
    Can anyone see any obvious flaws, or do you perhaps know a better/more efficient/cooler way of doing this?
     
  2. Offline

    adam753

    Personally I'd use the Chunk.getBlock() method. It gives you a block location relative to that chunk, so you could make the code as simple as this:
    Code:
    for(int x=0; x<=15; x++) {
      for(int y=0; y<=maxHeight; y++) {
        for(int z=0; z<=15; x++) {
    
          locations.add(chunk.getBlock(x,y,z).getLocation());
        }
      }
    }
    
    Haven't checked it but you get the idea.
     
  3. Offline

    Anrza

    adam753 Oh... yea... looks very simple... I'll try it out, thanks!
     
Thread Status:
Not open for further replies.

Share This Page