blob: 839de223614c17e8818f486d60c17b3d01b4eb46 (
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
|
package moe.nea.firmament.apis
import java.util.UUID
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import moe.nea.firmament.Firmament
import moe.nea.firmament.util.ErrorUtil
import moe.nea.firmament.util.MinecraftDispatcher
import moe.nea.firmament.util.net.HttpUtil
object Routes {
private val nameToUUID: MutableMap<String, Deferred<UUID?>> = mutableMapOf()
private val UUIDToName: MutableMap<UUID, Deferred<String?>> = mutableMapOf()
suspend fun getPlayerNameForUUID(uuid: UUID): String? {
return withContext(MinecraftDispatcher) {
UUIDToName.computeIfAbsent(uuid) {
async(Firmament.coroutineScope.coroutineContext) {
val data = ErrorUtil.catch("could not get name for uuid $uuid") {
HttpUtil.request("https://mowojang.matdoes.dev/$uuid")
.forJson<MowojangNameLookup>()
.await()
}.orNull() ?: return@async null
launch(MinecraftDispatcher) {
nameToUUID[data.name] = async { data.id }
}
data.name
}
}
}.await()
}
suspend fun getUUIDForPlayerName(name: String): UUID? {
return withContext(MinecraftDispatcher) {
nameToUUID.computeIfAbsent(name) {
async(Firmament.coroutineScope.coroutineContext) {
val data =
ErrorUtil.catch("could not get uuid for name $name") {
HttpUtil.request("https://mowojang.matdoes.dev/$name")
.forJson<MowojangNameLookup>()
.await()
}.orNull() ?: return@async null
launch(MinecraftDispatcher) {
UUIDToName[data.id] = async { data.name }
}
data.id
}
}
}.await()
}
}
|