summaryrefslogtreewikicommitdiff
path: root/core/src/events/onUse.kt
blob: 9e758b8f1959c9dc37284fe871437c80d75ae3b7 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cat.freya.khs.event

import cat.freya.khs.Khs
import cat.freya.khs.game.Game
import cat.freya.khs.inv.createTeleportMenu
import cat.freya.khs.player.Player
import cat.freya.khs.world.Item

data class UseEvent(val plugin: Khs, val player: Player, val item: Item) : Event()

private fun onUseLobby(event: UseEvent) {
    val (plugin, player, item) = event

    // handle leave
    if (item.similar(plugin.config.lobby.leaveItem)) {
        event.cancel()
        plugin.commandGroup.handleCommand(player, listOf("leave"))
    }

    // handle start
    if (item.similar(plugin.config.lobby.startItem)) {
        event.cancel()
        plugin.commandGroup.handleCommand(player, listOf("start"))
    }
}

private fun onUseInGame(event: UseEvent) {
    val (plugin, player, item) = event

    if (item.similar(plugin.config.glow.item) && plugin.config.glow.enabled) {
        event.cancel()
        plugin.game.glow.start()
        player.inventory.remove(item)
    }
}

private fun onUseSpectator(event: UseEvent) {
    val (plugin, player, item) = event

    // toggle flight
    if (item.similar(plugin.config.spectatorItems.flight)) {
        event.cancel()

        // toggle flying
        player.allowFlight = !player.flying
        player.flying = player.allowFlight
        player.actionBar(
            if (player.flying) plugin.locale.spectator.flyingEnabled
            else plugin.locale.spectator.flyingDisabled
        )
    }

    // view teleport ui
    if (item.similar(plugin.config.spectatorItems.teleport)) {
        event.cancel()

        val inv = createTeleportMenu(plugin, 0u) ?: return
        player.showInventory(inv)
    }
}

// for a right click interaction
fun onUse(event: UseEvent) {
    val (plugin, player, _) = event
    val game = plugin.game

    if (!game.hasPlayer(player)) return

    when (game.status) {
        Game.Status.LOBBY -> onUseLobby(event)
        Game.Status.SEEKING -> onUseInGame(event)
        else -> {}
    }

    if (game.isSpectator(player)) onUseSpectator(event)
}