Check to see if chunk exists/has been generated or not

Discussion in 'Plugin Development' started by aciid, Sep 30, 2012.

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

    aciid

    Hi guys, I'm struggling to create a method to check if a chunk exists (check if it has been generated)

    So far I've tried;
    • chunk.load(false) - I assumed it would return false if it was unsuccessfull trying to load an ungenerated chunk
    • ChunkSnapshot.isSectionEmpty(y) - Assumed it would return true if chunk wasn't generated..
    • ChunkSnapshot.getBlockTypeId(x,y,z) - Thought maybe it would error/return null if I tried to get the blockType inside an ungenerated chunk
    However nothing I've tried works.. all return the proper values (as if the chunk was already generated) even though I've checked chunks from empty worlds at positions that no way could be generated yet.

    Any help please? Thank you in advance
     
  2. Offline

    sd5

    Maybe try World.getChunkAt(int x, int y) and check whether the chunk is null...
     
  3. Offline

    aciid

    Considering all the checks I've been doing so far are using getChunkAt(int x, int y) at some point, I doubt it will work very much but will check anyway

    Edit: no good, any other ideas short of parsing the .mca files >.<
     
  4. Offline

    Courier

    I would look into NMS code before I'd parse .mca files.
     
  5. Offline

    aciid

    What's NMS, sorry?
     
  6. Offline

    sd5

    net.minecraft.server
    The classes from the original minecraft server.
    but I can't find anything...
     
  7. Offline

    aciid

    Damn, me neither. Does anybody else have any ideas?

    Thanks for the replies so far.

    Poke

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  8. Offline

    The_Coder

  9. Offline

    Comphenix

    I think the isChunkLoaded-method should do the trick:
    Code:java
    1. public static boolean isChunkLoaded(WorldServer worldServer, int x, int z)
    2. {
    3. return worldServer.chunkProvider.isChunkLoaded(x, z);
    4. }

    You can get the WorldServer instance by calling "getHandle" on CraftWorld.
     
  10. Offline

    aciid

    isLoaded() isn't what I'm looking for here.

    No idea how I never realised before but the MC anvil format has seperate files for each region, so I've just gone with parsing the file names in the world folder.. (edit: oops OK I found out regions are not chunks >.<)

    Thanks for your replies everyone

    OK turns out I jumped the gun, it's the region names that the files are named after. Each region contains 32x32 columns and each column contains 16 chunks.

    If anyone knows anything about reading these mca files please help me out I'd really appreciate it, not too sure where to start!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  11. I know there are libs to read files from .mcr, but I dont know anny for .mca still, but I think they exist
     
  12. Offline

    aciid

    I've been looking but can't find aaaaanything :(
     
  13. Offline

    aciid

    I'm trying to get jnbt to work with mca files but I think it's only for the old schematics??
    I keep getting "java.io.IOException: TAG_End found without a TAG_Compound/TAG_
    List tag preceding it."
     
  14. a .mca or .mcr isn't an nbt file, like a winrar cannot been openen whit the program used to access the file whitin, you need to add another layer to open those mcr/mca files, try seeing if this worlds: http://pastebin.com/niWTqLvk
     
  15. Offline

    aciid

    Not having any luck :(
     
  16. Offline

    Karl Marx

    By default, non-existent chunks are generated when they are requested.

    There is a method in org.bukkit.World, loadChunk(int x, int z, boolean generate), which solves this. Just pass "false" for the generate parameter, and it will return false if the chunk doesn't exist.

    Of course, this loads the chunks that do exist, eating lots of memory. so you should also use the "safe" variant of unloadChunk for any chunk that does exist.

    If- for some reason- that doesn't work, you can browse around CraftChunk for ideas that use the stock minecraft classes:
    https://github.com/Bukkit/CraftBukk...n/java/org/bukkit/craftbukkit/CraftWorld.java
    I would avoid using non-bukkit functionality, though, if possible.
     
    kroltan, aciid and fromgate like this.
  17. Offline

    aciid

    Isn't that the same as chunk.load(false)?

    Edit: nvm I get it, thanks this gets the job done, shame there is no easy way to get a list of existing chunks/their co-ords, just gotta iterate through lots of chunks and count them up :(

    Hi guys, I'm using the size of the world/region/*.mca files to help calculate how deep to search for chunks.

    Would anyone happen to know the maximum and minimum amount of chunks that could be stored within 1024KB worth of region files??

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  18. Offline

    Karl Marx

    A real rough estimate would be anywhere from 1 (there's no limit to how many mobs / items can be in a chunk) to about 900 (if the chunks are totally empty.) Each .mca file can hold up to 1024 chunks, though, and there's no such thing as an "average chunk", so I'd just assume the worst case at all times (1024.) I'm curious why you would need to know "how deep to search", though, as the only real answer to that is "completely". Unless you just need to know how big of a set to initialize your lists/maps for, don't guess.

    If you only care about getting "most" of the chunks (i.e., you don't mind missing unloaded chunks that are disconnected from the rest of the map by nether portals / teleporting) then I would strongly recommend using a general pathfinding algorithm (like A*) as a starting point to determine which chunks exist, and not touching the map files at all.

    Unfortunately, if you need to be certain you've found every chunk, then it looks like reading the files is your only option (although, I haven't looked too hard at how Minecraft handles unloaded chunks internally, so there may be hope through reflection.) Even still, there's no guarantee that the game won't generate new chunks while you're parsing the files, so you may miss chunks anyways.

    That said, there's no need to guess from filesize. Each region file has a table of which chunks exist in it, which should be trivial to parse for an exact count & list. As long as you're using flexible data structures, (which you should be,) then you shouldn't have to know anything before opening the file.
     
    aciid likes this.
  19. Offline

    aciid

    Thanks for the reply :) great info
     
Thread Status:
Not open for further replies.

Share This Page