1.15.2 Avoid accidental chunk loading

Discussion in 'Spigot Plugin Development' started by Buam, Jan 10, 2020.

  1. Hello,

    I have a plugin which has a task that has very bad timings (it takes about 3 seconds to execute!). Looking at the timings, I can see that the main event that takes so long is Minecraft::world - chunkAwait (Chunk loading, right?). However, I don't want to touch any chunk that isn't loaded to be loaded because I don't care about chunks were no player is.

    So, how can I avoid accidentally loading chunks? Does a block update lead to a chunk loading?
  2. Yes. Calling any hard linked Object will load it.
    Getting a Block will load the chunk and update the Block in that chunk then unload the chunk again.
    If the chunk is not generated jet it will even do that.

    To prevent that you can use:
    World#isChunkLoaded(x, z)

    The chunk-x and chunk-z coordinates of a Block (or location) are calculated like this:

    Code (Java):
        public static boolean isChunkLoaded(Location loc) {
            int chunkX = loc.getBlockX() >> 4;
            int chunkZ = loc.getBlockZ() >> 4;
            return loc.getWorld().isChunkLoaded(chunkX, chunkZ);
    PS Shifting right by 4 is pretty much just a fancy way of dividing by 16
  3. Thanks!
    Location#getBlock() also loads the chunk right?
    • Agree Agree x 2