From d69740851e2ce6443f00cc07120e305abbfc4307 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 16 Sep 2019 17:02:31 +0200 Subject: [PATCH] Added generation list task --- .../net/trivernis/chunkmaster/Chunkmaster.kt | 2 ++ .../chunkmaster/ChunkmasterEvents.kt | 3 ++- .../commands/CommandListGenTasks.kt | 24 +++++++++++++++++++ .../chunkmaster/lib/GenerationManager.kt | 9 ++++--- src/main/resources/plugin.yml | 10 +++++++- 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/net/trivernis/chunkmaster/commands/CommandListGenTasks.kt diff --git a/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt b/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt index 1679bcf..5b74044 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt @@ -1,6 +1,7 @@ package net.trivernis.chunkmaster import net.trivernis.chunkmaster.commands.CommandGenerate +import net.trivernis.chunkmaster.commands.CommandListGenTasks import net.trivernis.chunkmaster.lib.GenerationManager import net.trivernis.chunkmaster.lib.Spiral import org.bukkit.plugin.java.JavaPlugin @@ -20,6 +21,7 @@ class Chunkmaster: JavaPlugin() { generationManager = GenerationManager(this, server) generationManager.init() getCommand("generate")?.setExecutor(CommandGenerate(this)) + getCommand("listgentasks")?.setExecutor(CommandListGenTasks(this)) server.pluginManager.registerEvents(ChunkmasterEvents(this, server), this) } diff --git a/src/main/kotlin/net/trivernis/chunkmaster/ChunkmasterEvents.kt b/src/main/kotlin/net/trivernis/chunkmaster/ChunkmasterEvents.kt index bdae7a0..f000d88 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/ChunkmasterEvents.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/ChunkmasterEvents.kt @@ -11,7 +11,8 @@ class ChunkmasterEvents(private val chunkmaster: Chunkmaster, private val server * Autostart generation tasks */ fun onPlayerQuit(event: PlayerQuitEvent) { - if (server.onlinePlayers.size == 1 && server.onlinePlayers.contains(event.player)) { + if (server.onlinePlayers.size == 1 && server.onlinePlayers.contains(event.player) || + server.onlinePlayers.isEmpty()) { chunkmaster.generationManager.startAll() chunkmaster.logger.info("Server is empty. Starting chunk generation tasks.") } diff --git a/src/main/kotlin/net/trivernis/chunkmaster/commands/CommandListGenTasks.kt b/src/main/kotlin/net/trivernis/chunkmaster/commands/CommandListGenTasks.kt new file mode 100644 index 0000000..aa18df5 --- /dev/null +++ b/src/main/kotlin/net/trivernis/chunkmaster/commands/CommandListGenTasks.kt @@ -0,0 +1,24 @@ +package net.trivernis.chunkmaster.commands + +import net.md_5.bungee.api.ChatColor +import net.md_5.bungee.api.chat.ComponentBuilder +import net.trivernis.chunkmaster.Chunkmaster +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender + +class CommandListGenTasks(private val chunkmaster: Chunkmaster): CommandExecutor { + /** + * Responds with a list of running generation tasks. + */ + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + val runningTasks = chunkmaster.generationManager.tasks + val response = ComponentBuilder("Currently running generation tasks").color(ChatColor.BLUE) + for (task in runningTasks) { + response.append("\n - #${task.id}: ${task.generationTask.world}, Progress ${task.generationTask.count}") + } + response.color(ChatColor.GREEN) + sender.spigot().sendMessage(*response.create()) + return true + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/GenerationManager.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/GenerationManager.kt index cb39191..3e753aa 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/GenerationManager.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/GenerationManager.kt @@ -11,7 +11,8 @@ import java.lang.NullPointerException class GenerationManager(private val chunkmaster: Chunkmaster, private val server: Server) { - private val tasks: HashSet = HashSet() + val tasks: HashSet = HashSet() + get() = field /** * Adds a generation task @@ -60,7 +61,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server fun init() { chunkmaster.logger.info("Creating task to load chunk generation Tasks later...") server.scheduler.runTaskLater(chunkmaster, Runnable { - startAll() // run startAll after 10 seconds + if (server.onlinePlayers.isEmpty()) { + startAll() // run startAll after 10 seconds if empty + } }, 200) } @@ -108,7 +111,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server for (task in tasks) { try { 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. Last Chunk: ${genTask.lastChunk.x}, ${genTask.lastChunk.z}") val updateStatement = chunkmaster.sqliteConnection.prepareStatement(""" UPDATE generation_tasks SET last_x = ?, last_z = ? diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e6779cd..0b88a55 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -10,12 +10,20 @@ commands: description: Generates chunks starting from the world spawnpoint permission: chumkmaster.generate usage: /generate [world] + listgentasks: + description: Lists all generation tasks + permission: chunkmaster.listgentasks + usage: /listgentasks permissions: cunkmaster.generate: description: Allows generate command default: op + chunkmaster.listgentasks: + description: Lists all generation tasks + default: op chunkmaster.*: description: Wildcard permission default: op children: - - chunkmaster.generate \ No newline at end of file + - chunkmaster.generate + - chunkmaster.listgentasks \ No newline at end of file