Get all chunks within a x, z region

Discussion in 'Plugin Development' started by escortkeel, Aug 22, 2012.

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

    escortkeel

    Would anybody have any code lying around which retrieves all chunks within two pairs of x,z coords? It would just save me having to re-invent the wheel.

    Thanks in advance,
    Keeley :D
     
  2. Offline

    sd5

    Should work:
    Code:
    List<Chunk> chunks = new ArrayList<Chunk>();
    for(int x = x1; x < x2; x++) {
        for(int z = z1; z < z2; z++) {
            chunks.add(world.getChunkAt(x, z));
        }
    }
    But you have to check that x1 < x2
     
    OHQCraft likes this.
  3. Offline

    andf54

    Code:
    HashSet<Chunk> chunks = new HashSet<Chunk>();
           
            for (int x = xmin; x <= xmax; x++) {
           
                for (int z = zmin; z <= zmax; z++) {
                    chunks.add(world.getChunkAt(x, z));
                }
               
            }
    
    There is really no point in asking. You loose more time by waiting for the answer.
     
  4. Offline

    escortkeel

    Thx! I believe arraylists can have dupe entries though, so I'll have to use a non-dupe accepting implementation like a HashSet. Thanks again! :D

    EDIT: Haha, you beat me to posting that by like, 1 second andf54. ;)
     
  5. Offline

    desht

    Add 16 to x and z each iteration, not 1. Otherwise you'll just be adding a load of duplicate chunks, not to mention wasting CPU cycles. No need to use a Set here, a List is fine (unless you plan to use contains() a lot).
     
Thread Status:
Not open for further replies.

Share This Page