summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/ParcelOwner.kt
blob: 5a36cacfee038e0450bcd8e03dbbaf37d177bcda (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
@file:Suppress("unused", "UsePropertyAccessSyntax", "DEPRECATION")

package io.dico.parcels2

import io.dico.parcels2.util.getPlayerNameOrDefault
import io.dico.parcels2.util.isValid
import io.dico.parcels2.util.uuid
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.entity.Player
import java.util.UUID

class ParcelOwner(val uuid: UUID?,
                  val name: String?) {
    val notNullName: String by lazy { name ?: getPlayerNameOrDefault(uuid!!) }

    constructor(name: String) : this(null, name)
    constructor(uuid: UUID) : this(uuid, null)
    constructor(player: OfflinePlayer) : this(player.uuid, player.name)

    companion object {
        fun nameless(player: OfflinePlayer) = ParcelOwner(player.uuid, null)
    }

    inline val hasUUID: Boolean get() = 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 {
        return uuid?.let { it == player.uniqueId } ?: false
            || (allowNameMatch && name?.let { it == player.name } ?: false)
    }

    fun equals(other: ParcelOwner): Boolean {
        return if (hasUUID) other.hasUUID && uuid == other.uuid
        else !other.hasUUID && name == other.name
    }

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

    override fun hashCode(): Int {
        return if (hasUUID) uuid!!.hashCode() else name!!.hashCode()
    }

}