summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/PlayerProfile.kt
blob: 53e2d76dc9bd11b4895de29d9b7debd38d4d6857 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
@file:Suppress("unused", "UsePropertyAccessSyntax", "DEPRECATION")

package io.dico.parcels2

import io.dico.parcels2.storage.Storage
import io.dico.parcels2.util.PLAYER_NAME_PLACEHOLDER
import io.dico.parcels2.util.getPlayerName
import io.dico.parcels2.util.ext.isValid
import io.dico.parcels2.util.ext.uuid
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Unconfined
import kotlinx.coroutines.async
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import java.util.UUID

interface PlayerProfile {
    val uuid: UUID? get() = null
    val name: String?
    val nameOrBukkitName: String?
    val notNullName: String
    val isStar: Boolean get() = this is Star
    val exists: Boolean get() = this is RealImpl

    fun matches(player: OfflinePlayer, allowNameMatch: Boolean = false): Boolean

    fun equals(other: PlayerProfile): Boolean

    override fun equals(other: Any?): Boolean
    override fun hashCode(): Int

    val isFake: Boolean get() = this is Fake
    val isReal: Boolean get() = this is Real

    companion object {
        fun safe(uuid: UUID?, name: String?): PlayerProfile? {
            if (uuid != null) return Real(uuid, name)
            if (name != null) return invoke(name)
            return null
        }

        operator fun invoke(uuid: UUID?, name: String?): PlayerProfile {
            return safe(uuid, name) ?: throw IllegalArgumentException("One of uuid and name must not be null")
        }

        operator fun invoke(uuid: UUID): Real {
            if (uuid == Star.uuid) return Star
            return RealImpl(uuid, null)
        }

        operator fun invoke(name: String): PlayerProfile {
            if (name == Star.name) return Star
            return Fake(name)
        }

        operator fun invoke(player: OfflinePlayer): PlayerProfile {
            return if (player.isValid) Real(player.uuid, player.name) else Fake(player.name)
        }

        fun nameless(player: OfflinePlayer): Real {
            if (!player.isValid) throw IllegalArgumentException("The given OfflinePlayer is not valid")
            return RealImpl(player.uuid, null)
        }

        fun byName(input: String, allowReal: Boolean = true, allowFake: Boolean = false): PlayerProfile {
            if (!allowReal) {
                if (!allowFake) throw IllegalArgumentException("at least one of allowReal and allowFake must be true")
                return Fake(input)
            }

            if (input == Star.name) return Star

            return Bukkit.getOfflinePlayer(input).takeIf { it.isValid }?.let { PlayerProfile(it) }
                ?: Unresolved(input)
        }
    }

    interface Real : PlayerProfile {
        override val uuid: UUID
        override val nameOrBukkitName: String?
            // If a player is online, their name is prioritized to get name changes right immediately
            get() = Bukkit.getPlayer(uuid)?.name ?: name ?: getPlayerName(uuid)
        override val notNullName: String
            get() = nameOrBukkitName ?: PLAYER_NAME_PLACEHOLDER

        val player: OfflinePlayer? get() = Bukkit.getOfflinePlayer(uuid).takeIf { it.isValid }
        val playerUnchecked: OfflinePlayer get() = Bukkit.getOfflinePlayer(uuid)

        override fun matches(player: OfflinePlayer, allowNameMatch: Boolean): Boolean {
            return uuid == player.uuid || (allowNameMatch && name?.let { it == player.name } == true)
        }

        override fun equals(other: PlayerProfile): Boolean {
            return other is Real && uuid == other.uuid
        }

        companion object {
            fun byName(name: String): PlayerProfile {
                if (name == Star.name) return Star
                return Unresolved(name)
            }

            operator fun invoke(uuid: UUID, name: String?): Real {
                if (name == Star.name || uuid == Star.uuid) return Star
                return RealImpl(uuid, name)
            }

            fun safe(uuid: UUID?, name: String?): Real? {
                if (name == Star.name || uuid == Star.uuid) return Star
                if (uuid == null) return null
                return RealImpl(uuid, name)
            }

        }
    }

    object Star : BaseImpl(), Real {
        override val name: String = "*"
        // hopefully nobody will have this random UUID :)
        override val uuid: UUID = UUID.fromString("7d09c4c6-117d-4f36-9778-c4d24618cee1")

        override fun matches(player: OfflinePlayer, allowNameMatch: Boolean): Boolean {
            return true
        }
    }

    abstract class NameOnly(override val name: String) : BaseImpl() {
        override val notNullName get() = name
        override val nameOrBukkitName: String get() = name

        override fun matches(player: OfflinePlayer, allowNameMatch: Boolean): Boolean {
            return allowNameMatch && player.name == name
        }
    }

    class Fake(name: String) : NameOnly(name) {
        override fun equals(other: PlayerProfile): Boolean {
            return other is Fake && other.name == name
        }
    }

    class Unresolved(name: String) : NameOnly(name) {
        override fun equals(other: PlayerProfile): Boolean {
            return other is Unresolved && name == other.name
        }

        fun tryResolve(storage: Storage): Deferred<Real?> {
            return async(Unconfined) { tryResolveSuspendedly(storage) }
        }

        suspend fun tryResolveSuspendedly(storage: Storage): Real? {
            return storage.getPlayerUuidForName(name).await()?.let { resolve(it) }
        }

        fun resolve(uuid: UUID): Real {
            return RealImpl(uuid, name)
        }

        fun throwException(): Nothing {
            throw IllegalArgumentException("A UUID for the player $name can not be found")
        }
    }

    abstract class BaseImpl : PlayerProfile {
        override fun equals(other: Any?): Boolean {
            return this === other || (other is PlayerProfile && equals(other))
        }

        override fun hashCode(): Int {
            return uuid?.hashCode() ?: name!!.hashCode()
        }
    }

    private class RealImpl(override val uuid: UUID, override val name: String?) : BaseImpl(), Real

}


/*


/**
 * This class can represent:
 *
 * An existing player
 * A fake player (with only a name)
 * An existing player who must have its uuid resolved from the database (after checking against Bukkit OfflinePlayer)
 * STAR profile, which matches everyone. This profile is considered a REAL player, because it can have a privilege.
 */
class PlayerProfile2 private constructor(uuid: UUID?,
                                        val name: String?,
                                        val isReal: Boolean = uuid != null) {
    private var _uuid: UUID? = uuid
    val notNullName: String get() = name ?: getPlayerNameOrDefault(uuid!!)

    val uuid: UUID? get() = _uuid ?: if (isReal) throw IllegalStateException("This PlayerProfile must be resolved first") else null

    companion object {
        // below uuid is just a randomly generated one (version 4). Hopefully no minecraft player will ever have it :)
        val star = PlayerProfile(UUID.fromString("7d09c4c6-117d-4f36-9778-c4d24618cee1"), "*", true)

        fun nameless(player: OfflinePlayer): PlayerProfile {
            if (!player.isValid) throw IllegalArgumentException("The given OfflinePlayer is not valid")
            return PlayerProfile(player.uuid)
        }

        fun fromNameAndUuid(name: String?, uuid: UUID?): PlayerProfile? {
            if (name == null && uuid == null) return null
            if (star.name == name && star._uuid == uuid) return star
            return PlayerProfile(uuid, name)
        }

        fun realPlayerByName(name: String): PlayerProfile {
            return fromString(name, allowReal = true, allowFake = false)
        }

        fun fromString(input: String, allowReal: Boolean = true, allowFake: Boolean = false): PlayerProfile {
            if (!allowReal) {
                if (!allowFake) throw IllegalArgumentException("at least one of allowReal and allowFake must be true")
                return PlayerProfile(input)
            }

            if (input == star.name) return star

            return Bukkit.getOfflinePlayer(input).takeIf { it.isValid }?.let { PlayerProfile(it) }
                ?: PlayerProfile(null, input, !allowFake)
        }

        operator fun createWith(name: String): PlayerProfile {
            if (name == star.name) return star
            return PlayerProfile(null, name)
        }

        operator fun createWith(uuid: UUID): PlayerProfile {
            if (uuid == star.uuid) return star
            return PlayerProfile(uuid, null)
        }

        operator fun createWith(player: OfflinePlayer): PlayerProfile {
            // avoid UUID comparison against STAR
            return if (player.isValid) PlayerProfile(player.uuid, player.name) else createWith(player.name)
        }
    }

    val isStar: Boolean get() = this === star || (name == star.name && _uuid == star._uuid)
    val hasUUID: Boolean get() = _uuid != null
    val mustBeResolved: Boolean get() = isReal && _uuid == null

    val onlinePlayer: Player? get() = uuid?.let { Bukkit.getPlayer(uuid) }

    val onlinePlayerAllowingNameMatch: Player? get() = onlinePlayer ?: name?.let { Bukkit.getPlayerExact(name) }
    val offlinePlayer: OfflinePlayer? get() = uuid?.let { Bukkit.getOfflinePlayer(it).takeIf { it.isValid } }
    val offlinePlayerAllowingNameMatch: OfflinePlayer?
        get() = offlinePlayer ?: Bukkit.getOfflinePlayer(name).takeIf { it.isValid }

    fun matches(player: OfflinePlayer, allowNameMatch: Boolean = false): Boolean {
        if (isStar) return true
        return uuid?.let { it == player.uniqueId } ?: false
            || (allowNameMatch && name?.let { it == player.name } ?: false)
    }

    fun equals(other: PlayerProfile): Boolean {
        return if (_uuid != null) _uuid == other._uuid
        else other._uuid == null && isReal == other.isReal && name == other.name
    }

    override fun equals(other: Any?): Boolean {
        return other is PlayerProfile && equals(other)
    }

    override fun hashCode(): Int {
        return _uuid?.hashCode() ?: name!!.hashCode()
    }

    /**
     * resolve the uuid of this player profile if [mustBeResolved], using specified [storage].
     * returns true if the PlayerProfile has a uuid after this call.
     */
    suspend fun resolve(storage: Storage): Boolean {
        if (mustBeResolved) {
            val uuid = storage.getPlayerUuidForName(name!!).await()
            _uuid = uuid
            return uuid != null
        }
        return _uuid != null
    }

    fun resolve(uuid: UUID) {
        if (isReal && _uuid == null) {
            _uuid = uuid
        }
    }
}
*/