Is it worth looking at the McRegion files?

Discussion in 'Plugin Development' started by _Zenith_, Sep 7, 2011.

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

    _Zenith_

    I'm working on a plugin that has to do with saving and loading worlds. I'd like to be able to search through the world and be able to record all of the locations of a certain kind of block (for example a sign).

    Code:
    for(Chunk c : world.getLoadedChunks()){
                for(int x = 0; x < 16; x++){
                    for(int y = 0; y < 128; y++){
                        for(int z = 0; z < 16; z++){
                            Block b = c.getBlock(x, y, z);
                            if(b.getTypeId() == 63 || b.getTypeId() == 68){
                                signSet.add(b);
                            }
                        }
                    }
                }
            }
    I know that if I use something like what is written above, it will take forever and perhaps even crash the server given the size of the map.

    Would it be faster to search directly through the McRegion files stored in the region folder? If so, how would I go about doing this?

    I read up on how the data is stored here: http://www.minecraftwiki.net/wiki/Beta_Level_Format

    but I'm still not sure about how to go about analyzing the data.
    Code:
    try{
                InputStream in = new FileInputStream(src);
    
                System.out.println(src.getName());
    
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                while (in.read(buf) > 0) {
                    //What do I do here...?
                }
                in.close();
            }catch(Exception e){
                System.out.println("Could not read file "+src);
            }
    Any help or direction would be much appreciated :)
     
  2. Offline

    fullwall

    You'd be best off using JNBT to read the actual chunk data if you wanted to go down this route.
     
  3. Offline

    Timberjaw

    fullwall's suggestion is a good one, but you will have to parse the MCRegion format before you can get at the NBT data for the chunks. The region files themselves are not in NBT format.
     
  4. Offline

    _Zenith_

    Ah, thanks for letting me know. Do you know of any examples of how to search for blocks of a certain type this way? or something similar?
     
  5. Offline

    Timberjaw

    @_Zenith_ once you've retrieved the block array, you just need to run through it with a simple loop and look for the block ID(s) you're interested in.

    I'm not aware of someone doing this specifically, so unfortunately I can't be of help there. Parsing the MCRegion file will probably be the hard part though; accessing data from the NBT block should be pretty easy with the JNBT libary.

    If you need an example of using the JNBT library, I have some code for my world generator that might be useful. It's not the regular chunk format, but it loads an NBT file and retrieves an array of block IDs (among other things).
    https://github.com/Timberjaw/Dungeo...eonator/generator/DungeonRoomEditor.java#L241
     
  6. Offline

    _Zenith_

    Thanks, I'll check it out :)
     
  7. Offline

    fullwall

    sk89q has some mcregion parsing code located inside worldedit. You could check his source out on github...
     
  8. Offline

    MikeA

    I agree, sk89q has quite a bit of useful things on github.
     
Thread Status:
Not open for further replies.

Share This Page