03-10-2014, 05:08 AM
The chunks are only queued for unloading, because otherwise plugins would cause a lot of deadlocks (a plugin waiting for unloading chunks in one world, while the world is waiting for that same plugin to provide a callback value in its tick thread).
There is no way to know that the chunk unloading caused by the QueueUnloadUnusedChunks() has finished, but there are some workarounds. You can use the HOOK_UNLOADING_CHUNK to keep track of when the last chunk was unloaded, if it was more than N ticks ago (consider N = 5, for example), you can assume that the unloading has finished for that world; once it has finished for all worlds, you can send the stats.
FakeTruth's solution with waiting just one tick isn't sufficient - chunk unloading is asynchronous, it may take several ticks to unload a chunk, and each world ticks in a different thread anyway, which is yet different from the server tick thread.
To complicate matters even more, the SaveAllChunks() doesn't actually wait for the chunks to be saved, it queues a save action, so most chunks won't unload anyway, because they won't be saved yet. Therefore I dare say any plugin trying to do chunk maintenance is destined to fail.
There is no way to know that the chunk unloading caused by the QueueUnloadUnusedChunks() has finished, but there are some workarounds. You can use the HOOK_UNLOADING_CHUNK to keep track of when the last chunk was unloaded, if it was more than N ticks ago (consider N = 5, for example), you can assume that the unloading has finished for that world; once it has finished for all worlds, you can send the stats.
FakeTruth's solution with waiting just one tick isn't sufficient - chunk unloading is asynchronous, it may take several ticks to unload a chunk, and each world ticks in a different thread anyway, which is yet different from the server tick thread.
To complicate matters even more, the SaveAllChunks() doesn't actually wait for the chunks to be saved, it queues a save action, so most chunks won't unload anyway, because they won't be saved yet. Therefore I dare say any plugin trying to do chunk maintenance is destined to fail.