blob: eb00c529bd97d0e7bc3e14cae11698e1eae209c6 (
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
|
package io.dico.parcels2
import io.dico.parcels2.util.uuid
import kotlinx.coroutines.experimental.CompletableDeferred
import kotlinx.coroutines.experimental.Deferred
import org.bukkit.OfflinePlayer
import java.util.*
interface GlobalAddedData : AddedData {
val uuid: UUID
}
class GlobalAddedDataManager(val plugin: ParcelsPlugin) {
private val map = mutableMapOf<UUID, GlobalAddedData?>()
operator fun get(player: OfflinePlayer) = get(player.uuid)
operator fun get(uuid: UUID): GlobalAddedData? {
}
fun getDeferred(uuid: UUID): Deferred<AddedData> {
get(uuid)?.let { return CompletableDeferred(it) }
}
private suspend fun getAsync(uuid: UUID): GlobalAddedData {
val data = plugin.storage.readGlobalAddedData(ParcelOwner(uuid = uuid)).await()
?: return GlobalAddedDataImpl(uuid)
val result = GlobalAddedDataImpl(uuid, data)
map[uuid] = result
return result
}
private inner class GlobalAddedDataImpl(override val uuid: UUID,
data: MutableMap<UUID, AddedStatus> = emptyData)
: AddedDataHolder(data), GlobalAddedData {
private inline var data get() = added; set(value) = run { added = value }
private inline val isEmpty get() = data === emptyData
override fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean {
if (isEmpty) {
if (status == AddedStatus.DEFAULT) return false
data = mutableMapOf()
}
return super.setAddedStatus(uuid, status).also {
if (it) plugin.storage.setGlobalAddedStatus(ParcelOwner(uuid = this.uuid), uuid, status)
}
}
}
private companion object {
val emptyData = mapOf<UUID, AddedStatus>() as MutableMap<UUID, AddedStatus>
}
}
|