|
|
@ -10,7 +10,15 @@ import java.lang.NullPointerException
|
|
|
|
|
|
|
|
|
|
|
|
class GenerationManager(private val chunkmaster: Chunkmaster, private val server: Server) {
|
|
|
|
class GenerationManager(private val chunkmaster: Chunkmaster, private val server: Server) {
|
|
|
|
|
|
|
|
|
|
|
|
val tasks: HashSet<TaskEntry> = HashSet()
|
|
|
|
val tasks: HashSet<RunningTaskEntry> = HashSet()
|
|
|
|
|
|
|
|
val pausedTasks: HashSet<PausedTaskEntry> = HashSet()
|
|
|
|
|
|
|
|
val allTasks: HashSet<TaskEntry>
|
|
|
|
|
|
|
|
get() {
|
|
|
|
|
|
|
|
val all = HashSet<TaskEntry>()
|
|
|
|
|
|
|
|
all.addAll(pausedTasks)
|
|
|
|
|
|
|
|
all.addAll(tasks)
|
|
|
|
|
|
|
|
return all
|
|
|
|
|
|
|
|
}
|
|
|
|
var paused = false
|
|
|
|
var paused = false
|
|
|
|
private set
|
|
|
|
private set
|
|
|
|
|
|
|
|
|
|
|
@ -18,6 +26,8 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
* Adds a generation task
|
|
|
|
* Adds a generation task
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun addTask(world: World, stopAfter: Int = -1): Int {
|
|
|
|
fun addTask(world: World, stopAfter: Int = -1): Int {
|
|
|
|
|
|
|
|
val foundTask = allTasks.find { it.generationTask.world == world }
|
|
|
|
|
|
|
|
if (foundTask == null) {
|
|
|
|
val centerChunk = world.getChunkAt(world.spawnLocation)
|
|
|
|
val centerChunk = world.getChunkAt(world.spawnLocation)
|
|
|
|
val generationTask = createGenerationTask(world, centerChunk, centerChunk, stopAfter)
|
|
|
|
val generationTask = createGenerationTask(world, centerChunk, centerChunk, stopAfter)
|
|
|
|
|
|
|
|
|
|
|
@ -44,13 +54,23 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
insertStatement.close()
|
|
|
|
insertStatement.close()
|
|
|
|
getIdStatement.close()
|
|
|
|
getIdStatement.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generationTask.onEndReached {
|
|
|
|
|
|
|
|
server.consoleSender.sendMessage("Task #${id} finished after ${generationTask.count} chunks.")
|
|
|
|
|
|
|
|
removeTask(id)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!paused) {
|
|
|
|
if (!paused) {
|
|
|
|
val task = server.scheduler.runTaskTimer(chunkmaster, generationTask, 10,
|
|
|
|
val task = server.scheduler.runTaskTimer(chunkmaster, generationTask, 10,
|
|
|
|
chunkmaster.config.getLong("generation.period"))
|
|
|
|
chunkmaster.config.getLong("generation.period"))
|
|
|
|
tasks.add(TaskEntry(id, task, generationTask))
|
|
|
|
tasks.add(RunningTaskEntry(id, task, generationTask))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
pausedTasks.add(PausedTaskEntry(id, generationTask))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return id
|
|
|
|
return id
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return foundTask.id
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -62,7 +82,11 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
val generationTask = createGenerationTask(world, center, last, stopAfter)
|
|
|
|
val generationTask = createGenerationTask(world, center, last, stopAfter)
|
|
|
|
val task = server.scheduler.runTaskTimer(chunkmaster, generationTask, 10,
|
|
|
|
val task = server.scheduler.runTaskTimer(chunkmaster, generationTask, 10,
|
|
|
|
chunkmaster.config.getLong("generation.period"))
|
|
|
|
chunkmaster.config.getLong("generation.period"))
|
|
|
|
tasks.add(TaskEntry(id, task, generationTask))
|
|
|
|
tasks.add(RunningTaskEntry(id, task, generationTask))
|
|
|
|
|
|
|
|
generationTask.onEndReached {
|
|
|
|
|
|
|
|
server.consoleSender.sendMessage("Task #${id} finished after ${generationTask.count} chunks.")
|
|
|
|
|
|
|
|
removeTask(id)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -70,19 +94,27 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
* Stops a running generation task.
|
|
|
|
* Stops a running generation task.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun removeTask(id: Int): Boolean {
|
|
|
|
fun removeTask(id: Int): Boolean {
|
|
|
|
val taskEntry = this.tasks.find {it.id == id}
|
|
|
|
val taskEntry: TaskEntry? = if (this.paused) {
|
|
|
|
|
|
|
|
this.pausedTasks.find {it.id == id}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.tasks.find {it.id == id}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (taskEntry != null) {
|
|
|
|
if (taskEntry != null) {
|
|
|
|
taskEntry.generationTask.cancel()
|
|
|
|
taskEntry.cancel()
|
|
|
|
taskEntry.task.cancel()
|
|
|
|
|
|
|
|
val deleteTask = chunkmaster.sqliteConnection.prepareStatement("""
|
|
|
|
val deleteTask = chunkmaster.sqliteConnection.prepareStatement("""
|
|
|
|
DELETE FROM generation_tasks WHERE id = ?;
|
|
|
|
DELETE FROM generation_tasks WHERE id = ?;
|
|
|
|
""".trimIndent())
|
|
|
|
""".trimIndent())
|
|
|
|
deleteTask.setInt(1, taskEntry.id)
|
|
|
|
deleteTask.setInt(1, taskEntry.id)
|
|
|
|
deleteTask.execute()
|
|
|
|
deleteTask.execute()
|
|
|
|
deleteTask.close()
|
|
|
|
deleteTask.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (taskEntry is RunningTaskEntry) {
|
|
|
|
if (taskEntry.task.isCancelled) {
|
|
|
|
if (taskEntry.task.isCancelled) {
|
|
|
|
tasks.remove(taskEntry)
|
|
|
|
tasks.remove(taskEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (taskEntry is PausedTaskEntry) {
|
|
|
|
|
|
|
|
pausedTasks.remove(taskEntry)
|
|
|
|
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
return false
|
|
|
@ -109,7 +141,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun stopAll() {
|
|
|
|
fun stopAll() {
|
|
|
|
saveProgress()
|
|
|
|
saveProgress()
|
|
|
|
val removalSet = HashSet<TaskEntry>()
|
|
|
|
val removalSet = HashSet<RunningTaskEntry>()
|
|
|
|
for (task in tasks) {
|
|
|
|
for (task in tasks) {
|
|
|
|
task.generationTask.cancel()
|
|
|
|
task.generationTask.cancel()
|
|
|
|
task.task.cancel()
|
|
|
|
task.task.cancel()
|
|
|
@ -154,6 +186,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun pauseAll() {
|
|
|
|
fun pauseAll() {
|
|
|
|
paused = true
|
|
|
|
paused = true
|
|
|
|
|
|
|
|
for (task in tasks) {
|
|
|
|
|
|
|
|
pausedTasks.add(PausedTaskEntry(task.id, task.generationTask))
|
|
|
|
|
|
|
|
}
|
|
|
|
stopAll()
|
|
|
|
stopAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -162,6 +197,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun resumeAll() {
|
|
|
|
fun resumeAll() {
|
|
|
|
paused = false
|
|
|
|
paused = false
|
|
|
|
|
|
|
|
pausedTasks.clear()
|
|
|
|
startAll()
|
|
|
|
startAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -174,7 +210,8 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
val genTask = task.generationTask
|
|
|
|
val genTask = task.generationTask
|
|
|
|
server.consoleSender.sendMessage("""Task #${task.id} running for "${genTask.world.name}".
|
|
|
|
server.consoleSender.sendMessage("""Task #${task.id} running for "${genTask.world.name}".
|
|
|
|
|Progress ${task.generationTask.count} chunks
|
|
|
|
|Progress ${task.generationTask.count} chunks
|
|
|
|
|${if (task.generationTask.stopAfter > 0)"(${(task.generationTask.count.toDouble()/task.generationTask.stopAfter.toDouble())*100}%)" else ""}.
|
|
|
|
|${if (task.generationTask.stopAfter > 0)"(${(task.generationTask.count.toDouble()/
|
|
|
|
|
|
|
|
task.generationTask.stopAfter.toDouble())*100}%)" else ""}.
|
|
|
|
|Last Chunk: ${genTask.lastChunk.x}, ${genTask.lastChunk.z}""".trimMargin("|").replace('\n', ' '))
|
|
|
|
|Last Chunk: ${genTask.lastChunk.x}, ${genTask.lastChunk.z}""".trimMargin("|").replace('\n', ' '))
|
|
|
|
val updateStatement = chunkmaster.sqliteConnection.prepareStatement("""
|
|
|
|
val updateStatement = chunkmaster.sqliteConnection.prepareStatement("""
|
|
|
|
UPDATE generation_tasks SET last_x = ?, last_z = ?
|
|
|
|
UPDATE generation_tasks SET last_x = ?, last_z = ?
|
|
|
@ -185,11 +222,6 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
|
|
|
|
updateStatement.setInt(3, task.id)
|
|
|
|
updateStatement.setInt(3, task.id)
|
|
|
|
updateStatement.execute()
|
|
|
|
updateStatement.execute()
|
|
|
|
updateStatement.close()
|
|
|
|
updateStatement.close()
|
|
|
|
|
|
|
|
|
|
|
|
if (genTask.endReached) { // remove the task if it is finished after the progress has been saved
|
|
|
|
|
|
|
|
server.consoleSender.sendMessage("Task #${task.id} finished after ${genTask.count} chunks.")
|
|
|
|
|
|
|
|
removeTask(task.id)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error: Exception) {
|
|
|
|
} catch (error: Exception) {
|
|
|
|
server.consoleSender.sendMessage("Exception when saving task progress ${error.message}")
|
|
|
|
server.consoleSender.sendMessage("Exception when saving task progress ${error.message}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|