@ -2,6 +2,7 @@ package net.trivernis.chunkmaster.lib.generation
import io.papermc.lib.PaperLib
import io.papermc.lib.PaperLib
import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.Chunkmaster
import org.bukkit.Chunk
import org.bukkit.Server
import org.bukkit.Server
import org.bukkit.World
import org.bukkit.World
@ -83,13 +84,14 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
center : ChunkCoordinates ,
center : ChunkCoordinates ,
last : ChunkCoordinates ,
last : ChunkCoordinates ,
id : Int ,
id : Int ,
stopAfter : Int = - 1
stopAfter : Int = - 1 ,
delay : Long = 200L
) {
) {
if ( ! paused ) {
if ( ! paused ) {
chunkmaster . logger . info ( chunkmaster . langManager . getLocalized ( " RESUME_FOR_WORLD " , world . name ) )
chunkmaster . logger . info ( chunkmaster . langManager . getLocalized ( " RESUME_FOR_WORLD " , world . name ) )
val generationTask = createGenerationTask ( world , center , last , stopAfter )
val generationTask = createGenerationTask ( world , center , last , stopAfter )
val task = server . scheduler . runTaskTimer (
val task = server . scheduler . runTaskTimer (
chunkmaster , generationTask , 200 , // 10 sec delay
chunkmaster , generationTask , delay , // 10 sec delay
chunkmaster . config . getLong ( " generation.period " )
chunkmaster . config . getLong ( " generation.period " )
)
)
tasks . add ( RunningTaskEntry ( id , task , generationTask ) )
tasks . add ( RunningTaskEntry ( id , task , generationTask ) )
@ -149,11 +151,14 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
* Stops all generation tasks
* Stops all generation tasks
* /
* /
fun stopAll ( ) {
fun stopAll ( ) {
saveProgress ( )
val removalSet = HashSet < RunningTaskEntry > ( )
val removalSet = HashSet < RunningTaskEntry > ( )
for ( task in tasks ) {
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 . task . cancel ( )
task . generationTask . cancel ( )
if ( task . task . isCancelled ) {
if ( task . task . isCancelled ) {
removalSet . add ( task )
removalSet . add ( task )
}
}
@ -167,7 +172,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
* /
* /
fun startAll ( ) {
fun startAll ( ) {
chunkmaster . sqliteManager . executeStatement ( " SELECT * FROM generation_tasks " , HashMap ( ) ) { res ->
chunkmaster . sqliteManager . executeStatement ( " SELECT * FROM generation_tasks " , HashMap ( ) ) { res ->
var count = 0
while ( res . next ( ) ) {
while ( res . next ( ) ) {
count ++
try {
try {
val id = res . getInt ( " id " )
val id = res . getInt ( " id " )
val world = server . getWorld ( res . getString ( " world " ) )
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 last = ChunkCoordinates ( res . getInt ( " last_x " ) , res . getInt ( " last_z " ) )
val stopAfter = res . getInt ( " stop_after " )
val stopAfter = res . getInt ( " stop_after " )
if ( this . tasks . find { it . id == id } == null ) {
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 ) {
} catch ( error : NullPointerException ) {
chunkmaster . logger . severe ( chunkmaster . langManager . getLocalized ( " TASK_LOAD_FAILED " , res . getInt ( " id " ) ) )
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 ,
speed ,
genTask . lastChunk . x ,
genTask . lastChunk . x ,
genTask . lastChunk . z ) )
genTask . lastChunk . z ) )
chunkmaster . sqliteManager . executeStatement (
saveProgressToDatabase ( genTask . lastChunkCoords , task . id )
"""
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
)
} catch ( error : Exception ) {
} catch ( error : Exception ) {
chunkmaster . logger . warning ( chunkmaster . langManager . getLocalized ( " TASK_SAVE_FAILED " , error . toString ( ) ) )
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
* Creates a new generation task . This method is used to create a task depending
* on the server type ( Paper / Spigot ) .
* on the server type ( Paper / Spigot ) .