From be130adc6fed907c4c9b2bddc552edf15d76c38b Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 14 Apr 2020 22:34:31 +0200 Subject: [PATCH] Improve spigot chunk generation Add skipping of already generated chunks. An issue remaining is that some chunks are still not generated and the shape doesn't always match. --- .../lib/generation/GenerationManager.kt | 2 +- .../lib/generation/GenerationTask.kt | 14 ++--------- .../lib/generation/GenerationTaskPaper.kt | 12 ++++----- .../lib/generation/GenerationTaskSpigot.kt | 25 ++++++++++++------- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt index cb82eb4..509d910 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt @@ -351,7 +351,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server return if (PaperLib.isPaper()) { GenerationTaskPaper(chunkmaster, world, start, radius, shape) } else { - GenerationTaskSpigot(chunkmaster, world, center, start, radius, shape) + GenerationTaskSpigot(chunkmaster, world, start, radius, shape) } } } \ No newline at end of file diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTask.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTask.kt index 9178372..539a8da 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTask.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTask.kt @@ -55,17 +55,6 @@ abstract class GenerationTask( return ChunkCoordinates(nextChunkCoords.first, nextChunkCoords.second) } - val lastChunk: Chunk - get() { - return world.getChunkAt(lastChunkCoords.x, lastChunkCoords.z) - } - - val nextChunk: Chunk - get() { - val next = nextChunkCoordinates - return world.getChunkAt(next.x, next.z) - } - /** * Checks if the World border or the maximum chunk setting for the task is reached. */ @@ -126,7 +115,7 @@ abstract class GenerationTask( /** * Handles the invocation of the end reached callback and additional logic */ - protected fun setEndReached() { + private fun setEndReached() { endReached = true count = shape.count endReachedCallback?.invoke(this) @@ -140,6 +129,7 @@ abstract class GenerationTask( protected fun borderReachedCheck(): Boolean { val done = borderReached() if (done) { + unloadLoadedChunks() setEndReached() } return done diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskPaper.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskPaper.kt index 41549c5..a349c07 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskPaper.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskPaper.kt @@ -26,19 +26,19 @@ class GenerationTaskPaper( } /** - * Runs the generation task. Every Iteration the next chunk will be generated if - * it hasn't been generated already. - * After 10 chunks have been generated, they will all be unloaded and saved. + * Runs the generation task. Every Iteration the next chunks will be generated if + * they haven't been generated already + * After a configured number of chunks chunks have been generated, they will all be unloaded and saved. */ override fun run() { - if (plugin.mspt < msptThreshold) { // pause when tps < 2 + if (plugin.mspt < msptThreshold) { if (loadedChunks.size > maxLoadedChunks) { unloadLoadedChunks() - } else if (pendingChunks.size < maxPendingChunks) { // if more than 10 chunks are pending, wait. + } else if (pendingChunks.size < maxPendingChunks) { if (borderReachedCheck()) return var chunk = nextChunkCoordinates - for (i in 1 until chunkSkips) { + for (i in 0 until chunkSkips) { if (world.isChunkGenerated(chunk.x, chunk.z)) { chunk = nextChunkCoordinates } else { diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskSpigot.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskSpigot.kt index 836edc5..8e3b64a 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskSpigot.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskSpigot.kt @@ -6,8 +6,9 @@ import org.bukkit.World import java.lang.Exception class GenerationTaskSpigot( - private val plugin: Chunkmaster, override val world: World, - centerChunk: ChunkCoordinates, private val startChunk: ChunkCoordinates, + private val plugin: Chunkmaster, + override val world: World, + startChunk: ChunkCoordinates, override val radius: Int = -1, shape: Shape ) : GenerationTask(plugin, startChunk, shape) { @@ -21,21 +22,28 @@ class GenerationTaskSpigot( } /** - * Runs the generation task. Every Iteration the next chunk will be generated if - * it hasn't been generated already. - * After 10 chunks have been generated, they will all be unloaded and saved. + * Runs the generation task. Every Iteration the next chunks will be generated if + * they haven't been generated already + * After a configured number of chunks chunks have been generated, they will all be unloaded and saved. */ override fun run() { - if (plugin.mspt < msptThreshold) { // pause when tps < 2 + if (plugin.mspt < msptThreshold) { if (loadedChunks.size > maxLoadedChunks) { unloadLoadedChunks() } else { if (borderReachedCheck()) return var chunk = nextChunkCoordinates + for (i in 0 until chunkSkips) { + if (world.isChunkGenerated(chunk.x, chunk.z)) { + chunk = nextChunkCoordinates + } else { + break + } + } if (!world.isChunkGenerated(chunk.x, chunk.z)) { - for (i in 0 until minOf(chunksPerStep, radius - shape.currentRadius())) { + for (i in 0 until chunksPerStep) { if (borderReached()) break val chunkInstance = world.getChunkAt(chunk.x, chunk.z) chunkInstance.load(true) @@ -45,10 +53,9 @@ class GenerationTaskSpigot( val chunkInstance = world.getChunkAt(chunk.x, chunk.z) chunkInstance.load(true) loadedChunks.add(chunkInstance) - } lastChunkCoords = chunk - count = shape.count // set the count to the more accurate spiral count + count = shape.count } } }