TabComplete for all subcommands

- added tab complete to all subcommands
- added list of paused task when no task is running
- added possibility to remove paused tasks
release/0.11-beta
Trivernis 5 years ago
parent d456f40124
commit 132294d683

@ -9,6 +9,7 @@ import org.bukkit.event.player.PlayerQuitEvent
class ChunkmasterEvents(private val chunkmaster: Chunkmaster, private val server: Server) : Listener {
private val pauseOnJoin = chunkmaster.config.getBoolean("generation.pause-on-join")
private var playerPaused = false
/**
* Autostart generation tasks
@ -19,9 +20,14 @@ class ChunkmasterEvents(private val chunkmaster: Chunkmaster, private val server
if (server.onlinePlayers.size == 1 && server.onlinePlayers.contains(event.player) ||
server.onlinePlayers.isEmpty()
) {
if (!chunkmaster.generationManager.paused) {
chunkmaster.generationManager.startAll()
chunkmaster.logger.info("Server is empty. Starting chunk generation tasks.")
if (!playerPaused) {
chunkmaster.generationManager.resumeAll()
chunkmaster.logger.info("Server is empty. Resuming chunk generation tasks.")
} else if (chunkmaster.generationManager.paused){
chunkmaster.logger.info("Generation was manually paused. Not resuming automatically.")
playerPaused = chunkmaster.generationManager.paused
} else {
chunkmaster.logger.info("Generation tasks are already running.")
}
}
}
@ -34,8 +40,9 @@ class ChunkmasterEvents(private val chunkmaster: Chunkmaster, private val server
fun onPlayerJoin(event: PlayerJoinEvent) {
if (pauseOnJoin) {
if (server.onlinePlayers.size == 1 || server.onlinePlayers.isEmpty()) {
chunkmaster.generationManager.stopAll()
chunkmaster.logger.info("Stopping generation tasks because of player join.")
playerPaused = chunkmaster.generationManager.paused
chunkmaster.generationManager.pauseAll()
chunkmaster.logger.info("Pausing generation tasks because of player join.")
}
}
}

@ -4,11 +4,33 @@ import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.chat.ComponentBuilder
import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.Subcommand
import net.trivernis.chunkmaster.lib.generation.TaskEntry
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
class CmdCancel(private val chunkmaster: Chunkmaster): Subcommand {
override val name = "cancel"
/**
* TabComplete for subcommand cancel.
*/
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: List<String>
): MutableList<String> {
val genManager = chunkmaster.generationManager
val allTasks = HashSet<TaskEntry>()
allTasks.addAll(genManager.pausedTasks)
allTasks.addAll(genManager.tasks)
return allTasks.filter {it.id.toString().indexOf(args[0]) == 0}
.map { it.id.toString() }.toMutableList()
}
/**
* Cancels the generation task if it exists.
*/
override fun execute(sender: CommandSender, args: List<String>): Boolean {
return if (args.isNotEmpty() && args[0].toIntOrNull() != null) {
if (chunkmaster.generationManager.removeTask(args[0].toInt())) {

@ -4,12 +4,32 @@ import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.chat.ComponentBuilder
import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.Subcommand
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class CmdGenerate(private val chunkmaster: Chunkmaster): Subcommand {
override val name = "generate"
/**
* TabComplete for generate command.
*/
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: List<String>
): MutableList<String> {
if (args.size == 1) {
return sender.server.worlds.filter { it.name.indexOf(args[0]) == 0 }
.map {it.name}.toMutableList()
}
return emptyList<String>().toMutableList()
}
/**
* Creates a new generation task for the world and chunk count.
*/
override fun execute(sender: CommandSender, args: List<String>): Boolean {
var worldName = ""
var stopAfter = -1

@ -4,21 +4,57 @@ import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.chat.ComponentBuilder
import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.Subcommand
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
class CmdList(private val chunkmaster: Chunkmaster): Subcommand {
override val name = "list"
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: List<String>
): MutableList<String> {
return emptyList<String>().toMutableList()
}
/**
* Lists all running or paused generation tasks.
*/
override fun execute(sender: CommandSender, args: List<String>): Boolean {
val runningTasks = chunkmaster.generationManager.tasks
if (runningTasks.isEmpty()) {
sender.spigot().sendMessage(*ComponentBuilder("There are no running generation tasks.")
val pausedTasks = chunkmaster.generationManager.pausedTasks
if (runningTasks.isEmpty() && pausedTasks.isEmpty()) {
sender.spigot().sendMessage(*ComponentBuilder("There are no generation tasks.")
.color(ChatColor.BLUE).create())
} else if (runningTasks.isEmpty()) {
val response = ComponentBuilder("Currently paused generation tasks:").color(ChatColor.WHITE).bold(true)
for (taskEntry in pausedTasks) {
val genTask = taskEntry.generationTask
response.append("\n - ").color(ChatColor.WHITE).bold(false)
response.append("#${taskEntry.id}").color(ChatColor.BLUE).append(" - ").color(ChatColor.WHITE)
response.append(genTask.world.name).color(ChatColor.GREEN).append(" - Progress: ").color(ChatColor.WHITE)
response.append("${genTask.count} chunks").color(ChatColor.BLUE)
if (genTask.stopAfter > 0) {
response.append(" (${(genTask.count.toDouble()/genTask.stopAfter.toDouble())*100}%).")
}
}
sender.spigot().sendMessage(*response.create())
} else {
val response = ComponentBuilder("Currently running generation tasks:").color(ChatColor.WHITE)
val response = ComponentBuilder("Currently running generation tasks:").color(ChatColor.WHITE).bold(true)
for (task in runningTasks) {
response.append("\n - #${task.id}: ${task.generationTask.world.name}, Progress ${task.generationTask.count}")
.color(ChatColor.BLUE)
val genTask = task.generationTask
response.append("\n - ").color(ChatColor.WHITE).bold(false)
.append("#${task.id}").color(ChatColor.BLUE).append(" - ").color(ChatColor.WHITE)
.append(genTask.world.name).color(ChatColor.GREEN).append(" - Progress: ").color(ChatColor.WHITE)
.append("${genTask.count} chunks").color(ChatColor.BLUE)
if (genTask.stopAfter > 0) {
response.append(" (${(genTask.count.toDouble()/genTask.stopAfter.toDouble())*100}%).")
}
}
sender.spigot().sendMessage(*response.create())
}

@ -4,11 +4,21 @@ import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.chat.ComponentBuilder
import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.Subcommand
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
class CmdPause(private val chunkmaster: Chunkmaster) : Subcommand {
override val name: String = "pause"
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: List<String>
): MutableList<String> {
return emptyList<String>().toMutableList()
}
override fun execute(sender: CommandSender, args: List<String>): Boolean {
return if (!chunkmaster.generationManager.paused) {
chunkmaster.generationManager.pauseAll()

@ -4,11 +4,21 @@ import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.chat.ComponentBuilder
import net.trivernis.chunkmaster.Chunkmaster
import net.trivernis.chunkmaster.lib.Subcommand
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
class CmdResume(private val chunkmaster: Chunkmaster): Subcommand {
override val name = "resume"
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: List<String>
): MutableList<String> {
return emptyList<String>().toMutableList()
}
override fun execute(sender: CommandSender, args: List<String>): Boolean {
return if (chunkmaster.generationManager.paused) {
chunkmaster.generationManager.resumeAll()

@ -23,7 +23,16 @@ class CommandChunkmaster(private val chunkmaster: Chunkmaster, private val serve
*/
override fun onTabComplete(sender: CommandSender, command: Command, alias: String, args: Array<out String>):
MutableList<String> {
return commands.keys.filter { it.indexOf(args[0]) == 0 }.toMutableList()
if (args.size == 1) {
return commands.keys.filter { it.indexOf(args[0]) == 0 }.toMutableList()
} else if (args.isNotEmpty()){
if (commands.containsKey(args[0])) {
val commandEntry = commands[args[0]]
return commandEntry!!.onTabComplete(sender, command, alias, args.slice(1 until args.size))
}
}
return emptyList<String>().toMutableList()
}
/**

@ -1,8 +1,10 @@
package net.trivernis.chunkmaster.lib
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
interface Subcommand {
val name: String
fun execute(sender: CommandSender, args: List<String>): Boolean
fun onTabComplete(sender: CommandSender, command: Command, alias: String, args: List<String>): MutableList<String>
}

@ -10,7 +10,8 @@ import java.lang.NullPointerException
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()
var paused = false
private set
@ -47,7 +48,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
if (!paused) {
val task = server.scheduler.runTaskTimer(chunkmaster, generationTask, 10,
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
@ -62,7 +65,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
val generationTask = createGenerationTask(world, center, last, stopAfter)
val task = server.scheduler.runTaskTimer(chunkmaster, generationTask, 10,
chunkmaster.config.getLong("generation.period"))
tasks.add(TaskEntry(id, task, generationTask))
tasks.add(RunningTaskEntry(id, task, generationTask))
}
}
@ -70,18 +73,26 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
* Stops a running generation task.
*/
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) {
taskEntry.generationTask.cancel()
taskEntry.task.cancel()
taskEntry.cancel()
val deleteTask = chunkmaster.sqliteConnection.prepareStatement("""
DELETE FROM generation_tasks WHERE id = ?;
""".trimIndent())
deleteTask.setInt(1, taskEntry.id)
deleteTask.execute()
deleteTask.close()
if (taskEntry.task.isCancelled) {
tasks.remove(taskEntry)
if (taskEntry is RunningTaskEntry) {
if (taskEntry.task.isCancelled) {
tasks.remove(taskEntry)
}
} else if (taskEntry is PausedTaskEntry) {
pausedTasks.remove(taskEntry)
}
return true
}
@ -109,7 +120,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
*/
fun stopAll() {
saveProgress()
val removalSet = HashSet<TaskEntry>()
val removalSet = HashSet<RunningTaskEntry>()
for (task in tasks) {
task.generationTask.cancel()
task.task.cancel()
@ -154,6 +165,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
*/
fun pauseAll() {
paused = true
for (task in tasks) {
pausedTasks.add(PausedTaskEntry(task.id, task.generationTask))
}
stopAll()
}
@ -162,6 +176,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
*/
fun resumeAll() {
paused = false
pausedTasks.clear()
startAll()
}
@ -174,7 +189,8 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
val genTask = task.generationTask
server.consoleSender.sendMessage("""Task #${task.id} running for "${genTask.world.name}".
|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', ' '))
val updateStatement = chunkmaster.sqliteConnection.prepareStatement("""
UPDATE generation_tasks SET last_x = ?, last_z = ?

@ -73,6 +73,9 @@ class GenerationTaskPaper(
}
}
pendingChunks.clear()
if (loadedChunks.isNotEmpty()) {
lastChunkCoords = ChunkCoordinates(loadedChunks.last().x, loadedChunks.last().z)
}
for (chunk in loadedChunks) {
if (chunk.isLoaded) {
chunk.unload(true)

@ -0,0 +1,10 @@
package net.trivernis.chunkmaster.lib.generation
class PausedTaskEntry(
override val id: Int,
override val generationTask: GenerationTask
) : TaskEntry {
override fun cancel() {
generationTask.cancel()
}
}

@ -0,0 +1,14 @@
package net.trivernis.chunkmaster.lib.generation
import org.bukkit.scheduler.BukkitTask
class RunningTaskEntry(
override val id: Int,
val task: BukkitTask,
override val generationTask: GenerationTask
) : TaskEntry {
override fun cancel() {
task.cancel()
generationTask.cancel()
}
}

@ -2,4 +2,12 @@ package net.trivernis.chunkmaster.lib.generation
import org.bukkit.scheduler.BukkitTask
data class TaskEntry(val id: Int, val task: BukkitTask, val generationTask: GenerationTask)
/**
* Generic task entry
*/
interface TaskEntry {
val id: Int
val generationTask: GenerationTask
fun cancel()
}
Loading…
Cancel
Save