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 b005834..fffa129 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt @@ -2,6 +2,7 @@ package net.trivernis.chunkmaster.lib.generation import io.papermc.lib.PaperLib import net.trivernis.chunkmaster.Chunkmaster +import org.bukkit.Chunk import org.bukkit.Server import org.bukkit.World @@ -83,13 +84,14 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server center: ChunkCoordinates, last: ChunkCoordinates, id: Int, - stopAfter: Int = -1 + stopAfter: Int = -1, + delay: Long = 200L ) { if (!paused) { chunkmaster.logger.info(chunkmaster.langManager.getLocalized("RESUME_FOR_WORLD", world.name)) val generationTask = createGenerationTask(world, center, last, stopAfter) val task = server.scheduler.runTaskTimer( - chunkmaster, generationTask, 200, // 10 sec delay + chunkmaster, generationTask, delay, // 10 sec delay chunkmaster.config.getLong("generation.period") ) tasks.add(RunningTaskEntry(id, task, generationTask)) @@ -149,11 +151,14 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server * Stops all generation tasks */ fun stopAll() { - saveProgress() val removalSet = HashSet() for (task in tasks) { - task.generationTask.cancel() + val lastChunk = task.generationTask.lastChunkCoords + val id = task.id + chunkmaster.logger.info(chunkmaster.langManager.getLocalized("SAVING_TASK_PROGRESS", task.id)) + saveProgressToDatabase(lastChunk, id) task.task.cancel() + task.generationTask.cancel() if (task.task.isCancelled) { removalSet.add(task) } @@ -167,7 +172,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server */ fun startAll() { chunkmaster.sqliteManager.executeStatement("SELECT * FROM generation_tasks", HashMap()) { res -> + var count = 0 while (res.next()) { + count++ try { val id = res.getInt("id") val world = server.getWorld(res.getString("world")) @@ -175,7 +182,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server val last = ChunkCoordinates(res.getInt("last_x"), res.getInt("last_z")) val stopAfter = res.getInt("stop_after") if (this.tasks.find { it.id == id } == null) { - resumeTask(world!!, center, last, id, stopAfter) + resumeTask(world!!, center, last, id, stopAfter, 200L + count) } } catch (error: NullPointerException) { chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("TASK_LOAD_FAILED", res.getInt("id"))) @@ -238,20 +245,27 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server speed, genTask.lastChunk.x, genTask.lastChunk.z)) - chunkmaster.sqliteManager.executeStatement( - """ - UPDATE generation_tasks SET last_x = ?, last_z = ? - WHERE id = ? - """.trimIndent(), - HashMap(mapOf(1 to genTask.lastChunk.x, 2 to genTask.lastChunk.z, 3 to task.id)), - null - ) + saveProgressToDatabase(genTask.lastChunkCoords, task.id) } catch (error: Exception) { chunkmaster.logger.warning(chunkmaster.langManager.getLocalized("TASK_SAVE_FAILED", error.toString())) } } } + /** + * Saves the generation progress to the database + */ + private fun saveProgressToDatabase(lastChunk: ChunkCoordinates, id: Int) { + chunkmaster.sqliteManager.executeStatement( + """ + UPDATE generation_tasks SET last_x = ?, last_z = ? + WHERE id = ? + """.trimIndent(), + HashMap(mapOf(1 to lastChunk.x, 2 to lastChunk.z, 3 to id)), + null + ) + } + /** * Creates a new generation task. This method is used to create a task depending * on the server type (Paper/Spigot). 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 92f3ea5..7a2f1bd 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTask.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTask.kt @@ -17,7 +17,8 @@ abstract class GenerationTask(plugin: Chunkmaster, centerChunk: ChunkCoordinates protected val spiral: Spiral = Spiral(Pair(centerChunk.x, centerChunk.z), Pair(startChunk.x, startChunk.z)) protected val loadedChunks: HashSet = HashSet() - protected var lastChunkCoords = ChunkCoordinates(startChunk.x, startChunk.z) + var lastChunkCoords = ChunkCoordinates(startChunk.x, startChunk.z) + protected set protected val chunkSkips = plugin.config.getInt("generation.chunk-skips-per-step") protected val msptThreshold = plugin.config.getLong("generation.mspt-pause-threshold") protected val maxLoadedChunks = plugin.config.getInt("generation.max-loaded-chunks") 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 cb0f9b1..6f93203 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskSpigot.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationTaskSpigot.kt @@ -2,6 +2,7 @@ package net.trivernis.chunkmaster.lib.generation import net.trivernis.chunkmaster.Chunkmaster import org.bukkit.World +import java.lang.Exception class GenerationTaskSpigot( private val plugin: Chunkmaster, override val world: World, @@ -62,7 +63,11 @@ class GenerationTaskSpigot( override fun cancel() { for (chunk in loadedChunks) { if (chunk.isLoaded) { - chunk.unload(true) + try { + chunk.unload(true) + } catch (e: Exception) { + plugin.logger.severe(e.toString()) + } } } } diff --git a/src/main/resources/i18n/DEFAULT.i18n.properties b/src/main/resources/i18n/DEFAULT.i18n.properties index a692811..12530dd 100644 --- a/src/main/resources/i18n/DEFAULT.i18n.properties +++ b/src/main/resources/i18n/DEFAULT.i18n.properties @@ -35,6 +35,7 @@ NO_PERMISSION = §cYou do not have the permission for this command! SUBCOMMAND_NOT_FOUND = §cSubcommand §2%s §cnot found! STOPPING_ALL_TASKS = Stopping all generation tasks... +SAVING_TASK_PROGRESS = Saving the progress of task #%s to the database... DB_INIT = Initializing database... DB_INIT_FINISHED = Database fully initialized. DB_INIT_EROR = Failed to init database: %s. diff --git a/src/main/resources/i18n/de.i18n.properties b/src/main/resources/i18n/de.i18n.properties index 307800b..78e0547 100644 --- a/src/main/resources/i18n/de.i18n.properties +++ b/src/main/resources/i18n/de.i18n.properties @@ -35,6 +35,7 @@ NO_PERMISSION = §cDu hast nicht die Rechte für diesen Befehl! SUBCOMMAND_NOT_FOUND = §cUnteraktion §2%s §cwurde nicht gefunden! STOPPING_ALL_TASKS = Stoppt alle Aufgaben... +SAVING_TASK_PROGRESS = Speichert den Fortschritt der Aufgabe #%s in der Datenbank... DB_INIT = Initialisiere Datenbank... DB_INIT_FINISHED = Die Datenbank wurde initialisiert. DB_INIT_EROR = Fehler beim Initalisieren der Datenbank: %s. diff --git a/src/main/resources/i18n/en.i18n.properties b/src/main/resources/i18n/en.i18n.properties index a692811..12530dd 100644 --- a/src/main/resources/i18n/en.i18n.properties +++ b/src/main/resources/i18n/en.i18n.properties @@ -35,6 +35,7 @@ NO_PERMISSION = §cYou do not have the permission for this command! SUBCOMMAND_NOT_FOUND = §cSubcommand §2%s §cnot found! STOPPING_ALL_TASKS = Stopping all generation tasks... +SAVING_TASK_PROGRESS = Saving the progress of task #%s to the database... DB_INIT = Initializing database... DB_INIT_FINISHED = Database fully initialized. DB_INIT_EROR = Failed to init database: %s.