summaryrefslogtreewikicommitdiff
path: root/core/src/events/onJoin.kt
blob: 1089cabd6d19be15b3ee69cdb5f2b26e7a596ce9 (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
package cat.freya.khs.event

import cat.freya.khs.Khs
import cat.freya.khs.player.Player

data class JoinEvent(val plugin: Khs, val player: Player) : Event()

fun onJoin(event: JoinEvent) {
    val (plugin, player) = event
    val game = plugin.game

    // save name data for user
    plugin.database?.upsertName(player.uuid, player.name)

    // uhhhh
    if (game.hasPlayer(player)) game.leave(player.uuid)

    if (plugin.config.autoJoin) {
        game.join(player.uuid)
        return
    }

    val worldName = player.world?.name ?: return
    if (
        (plugin.config.teleportStraysToExit && worldName == game.map?.worldName) ||
            ((plugin.config.teleportStraysToExit || plugin.config.mapSaveEnabled) &&
                worldName == game.map?.gameWorldName)
    ) {
        // teleport to exit if inside game world(s)
        plugin.config.exit?.let {
            player.teleport(it)
            player.setGameMode(Player.GameMode.ADVENTURE)
        }
    }
}