@ -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 < RunningTaskEntry > ( )
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,18 +245,25 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
speed ,
genTask . lastChunk . x ,
genTask . lastChunk . z ) )
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 genTask . lastChunk . x , 2 to genTask . lastChunk . z , 3 to task . id ) ) ,
HashMap ( mapOf ( 1 to lastChunk. x , 2 to lastChunk. z , 3 to id ) ) ,
null
)
} catch ( error : Exception ) {
chunkmaster . logger . warning ( chunkmaster . langManager . getLocalized ( " TASK_SAVE_FAILED " , error . toString ( ) ) )
}
}
}
/ * *