Improves chunk unloading

Adds all chunk unloading to a queue
Adds a thread which unloads chunks
Updates chunk unload requests such that a chunk won't be unloaded twice,
and an old unloading request cannot unload a chunk too soon
This commit is contained in:
2021-10-10 22:33:30 +02:00
parent 69a62c921c
commit 38ea543b80
4 changed files with 123 additions and 16 deletions

View File

@ -0,0 +1,36 @@
package net.knarcraft.stargate.thread;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.container.ChunkUnloadRequest;
import org.bukkit.Chunk;
import java.util.Queue;
/**
* Unloads chunks which should no longer be forced to stay loaded
*/
public class ChunkUnloadThread implements Runnable {
@Override
public void run() {
long systemNanoTime = System.nanoTime();
Queue<ChunkUnloadRequest> unloadQueue = Stargate.getChunkUnloadQueue();
//Peek at the first element to check if the chunk should be unloaded
ChunkUnloadRequest firstElement = unloadQueue.peek();
if (firstElement != null) {
Stargate.debug("ChunkUnloadThread", "Found chunk unload request: " + firstElement);
Stargate.debug("ChunkUnloadThread", "Current time: " + systemNanoTime);
}
//Repeat until all un-loadable chunks have been processed
while (firstElement != null && firstElement.getUnloadNanoTime() < systemNanoTime) {
unloadQueue.remove();
Chunk chunkToUnload = firstElement.getChunkToUnload();
//Allow the chunk to be unloaded
chunkToUnload.removePluginChunkTicket(Stargate.stargate);
Stargate.debug("ChunkUnloadThread", "Unloaded chunk " + chunkToUnload);
firstElement = unloadQueue.peek();
}
}
}