Grabbing blocks from a chunk

Discussion in 'Plugin Development' started by nossr50, Jan 21, 2011.

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

    nossr50

    What would be the best way to grab all the blocks in a specific chunk?
     
  2. Offline

    LRFLEW

    I'm not sure specifically, but if you knew what size a chunk is and where the origin is, you could use a triple array of block from
    Code:
    blocks [Chunk.getX()] [0] [Chunk.getZ()]
    to
    Code:
    blocks [Chunk.getX() + sizeOfChunks] [127] [Chunk.getZ() + sizeOfChunks]
    implying you're using blocks[x][y][z] as your variable.

    I wouldn't know how big a chunk is, though.
     
  3. Offline

    Mixcoatl

    At the present time a chunk is 16m x 16m x 128m. You may be able to calculate which blocks to retrieve using the chunk's coordinates. Something like:
    Code:
    // Get the X and Z position of the chunk.
    final int chunkX = world.getChunk(someX, someZ).getX();
    final int chunkZ = world.getChunk(someX, someZ).getZ();
    // Enough space for one chunk worth of blocks.
    final int blocks[] = new int[16 * 16 * 128];
    
    for (int y = 0; y < 128; ++y) {
        for (int x = chunkX; x < chunkX + 16; ++x) {
            for (int z = chunkZ; z < chunkZ + 16; ++z) {
                // Calculate the offset into the blocks array.
                final int offset = (y * 256) + (x * 16) + z;
                // Get the type ID of the block and store it in the array.
                blocks[offset] = world.getBlockTypeIdAt(x, y z);
            }
        }
    }
    If you want block objects instead of type IDs you could use a Set<Block> instead and call world.getBlockAt() instead of world.getBlockTypeIdAt().
     
  4. Offline

    nossr50

    Thanks a lot Mixcoatl, that will surely help.
     
Thread Status:
Not open for further replies.

Share This Page