Wait until chunks around player have loaded

Discussion in 'Plugin Development' started by THEK, Dec 2, 2012.

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

    THEK

    I have a Runnable class and within that I want to put the Thread to sleep so the player can read some text before moving to another location.

    How do I sleep the thread only once the chunks around a player have loaded? I sleep the thread for 3 seconds but if the world loads slowly it moves on before the chunks have loaded. But if I run an isChunkLoaded() within an if statement it is only called once.

    Anyone? I'm sure this has been done before, just can't find anything on here.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. use the ChunkLoadEvent mayby for your problem?
     
  3. Offline

    THEK

    So would I just check for a certain chunk is loaded every time a chunk loads?
     
  4. whit that event, you get messages if there is a chunk annywhere in every world is loaded to the memoy, you can use this to detect when al chunks are loaded around the player
     
  5. Offline

    THEK

    OK, I understand your idea. However, if I want to check chunks X:20 Z:20 to X:30 to Z:30, surely this will cause unnecessary lag because the loop would run every time a chunk is loaded?
     
  6. Offline

    bergerkiller

    THEK
    Don't use the Thread.sleep for this, it will stall the entire server for several seconds. This includes chunk loads. You don't have to wait for all chunks to be loaded, because (by default) all chunks nearby are loaded instantly in one tick when the player joins. There is nothing you can improve here other than loading the chunks and then performing teleport().

    If you wish for a delay and then teleport, schedule a sync delayed task (Bukkit.getScheduler().scheduleSyncDelayedTask(new Runnable() {public void run()}, <delayinticks>);

    If you are manually loading these chunks 'slowly' just check every so often whether all chunks in the vicinity have loaded. Don't use chunk.isChunkLoaded for this! It doesn't work!

    Use:
    Code:
    World.isChunkLoaded(cx, cz);
    Where cx and cz is the chunk. Whatever you do, do NOT cause a getChunk because that will load the chunk. Use the above in a for loop like so:

    Code:
    final int view = Bukkit.getViewDistance();
    for int dx = -view; dx <= view; dx++) {
        for int dz = -view; dz <= view; dz++) {
            if (!world.isChunkLoaded(cx + dx, cz + dz)) {
                return false; //still loading
            }
        }
    }
     
  7. Offline

    THEK

    Ah ok, that is very helpful, Thanks :). I'm now running the task in an Asynchronous Task, so it should be safe to Thread.sleep right?
     
  8. its safe to use Thread.sleep() there, but its unsafe to use world.isCHunkLoaded() there, because of the thread corrunty affects, it may not work, or even worse, crash the server
     
  9. Offline

    bergerkiller

    ferrybig
    Actually, isChunkLoaded is multithreading safe. It doesn't modify the chunks collection, it only reads them. There is no harm to the main thread to read collections from another thread, but it may cause errors on the thread you are doing the reading on. (maybe while checking if it's loaded the chunks collection changes?)
     
Thread Status:
Not open for further replies.

Share This Page