summaryrefslogtreewikicommitdiff
path: root/core/src/Checks.kt
blob: cc3c4c7299b646d8acca2113385a8391f032be09 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package cat.freya.khs

import cat.freya.khs.game.Game
import cat.freya.khs.game.KhsMap
import cat.freya.khs.player.Player
import cat.freya.khs.world.Position

class Checks(val plugin: Khs, val player: Player) {
    /// checks if there exists a map that is setup
    fun gameMapExists() {
        if (plugin.game.selectMap() == null) {
            val msg =
                if (plugin.maps.isEmpty()) plugin.locale.map.none else plugin.locale.map.noneSetup
            error(msg)
        }
    }

    /// checks that the game is in progress
    fun gameInProgress() {
        if (!plugin.game.status.inProgress()) {
            error(plugin.locale.game.notInProgress)
        }
    }

    /// checks that the game is not in progress
    fun gameNotInProgress() {
        if (plugin.game.status != Game.Status.LOBBY) {
            error(plugin.locale.game.inProgress)
        }
    }

    /// checks that the caller is in the game
    fun playerNotInGame() {
        if (plugin.game.hasPlayer(player)) {
            error(plugin.locale.game.inGame)
        }
    }

    /// checks that the caller is in the game
    fun playerInGame() {
        if (!plugin.game.hasPlayer(player)) {
            error(plugin.locale.game.notInGame)
        }
    }

    /// check if the lobby has enough players to start
    fun lobbyHasEnoughPlayers() {
        if (plugin.game.size < plugin.config.minPlayers) {
            error(plugin.locale.lobby.notEnoughPlayers.with(plugin.config.minPlayers))
        }
    }

    /// check if the lobby is empty
    fun lobbyEmpty() {
        if (plugin.game.size > 0u) {
            error(plugin.locale.lobby.inUse)
        }
    }

    /// cheks that the player is in the game world
    fun inMapWorld(mapName: String) {
        inMapWorld(plugin.maps.get(mapName))
    }

    /// cheks that the player is in the game world
    fun inMapWorld(map: KhsMap?) {
        if (map?.worldName != player.location.worldName) error(plugin.locale.map.wrongWorld)
    }

    /// Checks that the map exists and is setup
    fun mapSetup(map: KhsMap?) {
        if (map == null) error(plugin.locale.map.unknown)
        if (!map.setup) error(plugin.locale.map.setup.not.with(map.name))
    }

    /// Checks if the map has bounds
    fun mapHasBounds(map: KhsMap?) {
        if (map == null) error(plugin.locale.map.unknown)
        if (map.bounds() == null) error(plugin.locale.map.error.bounds)
    }

    /// Checks if the map has bounds
    fun mapHasBounds(name: String) {
        mapHasBounds(plugin.maps.get(name))
    }

    /// Checks if a map exists
    fun mapExists(name: String) {
        mapExists(plugin.maps.get(name))
    }

    /// Checks if a map exists
    fun mapExists(map: KhsMap?) {
        if (map == null) error(plugin.locale.map.unknown)
    }

    /// Checks if a map doesnt exists
    fun mapDoesNotExist(name: String) {
        if (plugin.maps.containsKey(name)) error(plugin.locale.map.exists)
    }

    /// Checks if a map name is valid
    fun mapNameValid(name: String) {
        if (!name.matches(Regex("[a-zA-Z0-9]*")) || name.isEmpty())
            error(plugin.locale.map.invalidName)
    }

    /// Checks if a world exists
    fun worldExists(worldName: String) {
        if (!plugin.shim.worlds.contains(worldName))
            error(plugin.locale.world.doesntExist.with(worldName))
    }

    /// Checks if a world doesnt exists
    fun worldDoesNotExist(worldName: String) {
        if (plugin.shim.worlds.contains(worldName))
            error(plugin.locale.world.exists.with(worldName))
    }

    /// Checks if a world is valid for a map
    fun worldValid(worldName: String) {
        worldExists(worldName)
        if (worldName.startsWith("hs_")) error(plugin.locale.world.doesntExist.with(worldName))
    }

    /// Checks that a world is not in use
    fun worldNotInUse(worldName: String) {
        val map =
            plugin.maps.values.find { it.worldName == worldName || it.gameWorldName == worldName }
        if (map != null) error(plugin.locale.world.inUseBy.with(worldName, map.name))
        if (plugin.config.exit?.worldName == worldName)
            error(plugin.locale.world.inUse.with(worldName))
    }

    /// Checks if blockhunt is supported
    fun blockHuntSupported() {
        if (!plugin.shim.supports(9)) error(plugin.locale.blockHunt.notSupported)
    }

    /// Checks if a map has block hunt enabled
    fun blockHuntEnabled(name: String) {
        mapExists(name)
        val map = plugin.maps.get(name) ?: return
        if (!map.config.blockHunt.enabled) error(plugin.locale.blockHunt.notEnabled)
    }

    private fun isSpawnInRange(map: KhsMap, position: Position?): Boolean {
        if (position == null) return true // return true to not reset a null value

        // check world border (with in 100 blocks)
        val border = map.config.worldBorder
        if (border.enabled && border.pos?.distance(position) ?: 0.0 > 100.0) return false

        // check in bounds
        if (map.bounds()?.inBounds(position.x, position.z) == false) return false

        return true
    }

    /// Makes sure a spawn is in gane
    fun spawnInRange(map: KhsMap, pos: Position) {
        if (!isSpawnInRange(map, pos)) error(plugin.locale.map.error.notInRange)
    }

    /// Makes sure spawns are in range
    fun spawnsInRange(map: KhsMap) {
        // check game spawn
        if (!isSpawnInRange(map, map.gameSpawn?.position)) {
            player.message(plugin.locale.prefix.warning + plugin.locale.map.warn.gameSpawnReset)
            map.gameSpawn = null
        }
        // check seeker spawn
        if (!isSpawnInRange(map, map.seekerLobbySpawn?.position)) {
            player.message(plugin.locale.prefix.warning + plugin.locale.map.warn.seekerSpawnReset)
            map.seekerLobbySpawn = null
        }
        // check lobby spawn
        if (!isSpawnInRange(map, map.lobbySpawn?.position)) {
            player.message(plugin.locale.prefix.warning + plugin.locale.map.warn.lobbySpawnReset)
            map.lobbySpawn = null
        }
    }
}

fun runChecks(plugin: Khs, player: Player, fn: Checks.() -> Unit) {
    fn(Checks(plugin, player))
}