summaryrefslogtreewikicommitdiff
path: root/core/src/command/Help.kt
blob: 168b69cda66043b060a3f974bf7a866625be398c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cat.freya.khs.command

import cat.freya.khs.Khs
import cat.freya.khs.command.util.Command
import cat.freya.khs.player.Player
import kotlin.text.toUInt

class KhsHelp : Command {
    override val label = "help"
    override val usage = listOf("*page")
    override val description = "Lists the commands you can use"

    override fun execute(plugin: Khs, player: Player, args: List<String>) {
        val commands = plugin.commandGroup.commandsFor(player)
        val pageSize = 4u
        val pages = (commands.size.toUInt() + pageSize - 1u) / pageSize
        val page = maxOf(minOf(args.firstOrNull().let { it?.toUIntOrNull() } ?: 0u, pages), 1u)

        player.message(
            buildString {
                appendLine(
                    "&b=================== &fHelp: Page ($page/$pages) &b==================="
                )
                for ((label, command) in commands.chunked(pageSize.toInt()).get(page.toInt() - 1)) {
                    val cmd = label.substring(3)
                    val usage = command.usage.joinToString(" ")
                    val description = command.description
                    appendLine("&7?&f &b/hs &f$cmd &9$usage")
                    appendLine("&7?&f  &7&o$description")
                }
                appendLine("&b=====================================================")
            }
        )
    }

    override fun autoComplete(plugin: Khs, parameter: String, typed: String): List<String> {
        return listOf(parameter)
    }
}