summaryrefslogtreewikicommitdiff
path: root/core/src/command/Join.kt
blob: 88bb94c603022bb85b93b84d4f1373ed69604ca2 (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
package cat.freya.khs.command

import cat.freya.khs.Khs
import cat.freya.khs.command.util.Command
import cat.freya.khs.player.Player
import cat.freya.khs.runChecks

class KhsJoin : Command {
    override val label = "join"
    override val usage = listOf<String>("*map")
    override val description = "Joins the game, and can set a map if the lobby is empty"

    override fun execute(plugin: Khs, player: Player, args: List<String>) {
        val mapName = args.firstOrNull()
        val map = mapName?.let { plugin.maps.get(it) }

        runChecks(plugin, player) {
            gameMapExists()
            playerNotInGame()
            if (mapName != null) mapSetup(map)
        }

        if (plugin.game.size == 0u) plugin.game.setMap(map)

        plugin.game.join(player.uuid)
    }

    override fun autoComplete(plugin: Khs, parameter: String, typed: String): List<String> =
        when (parameter) {
            "*map" -> plugin.maps.keys.filter { it.startsWith(typed) }
            else -> listOf()
        }
}