-
Notifications
You must be signed in to change notification settings - Fork 443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add MojangUtil.queueUsernameForUUID #444
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,15 @@ package gg.skytils.skytilsmod.utils | |
|
||
import gg.essential.lib.caffeine.cache.Cache | ||
import gg.essential.lib.caffeine.cache.Caffeine | ||
import gg.skytils.skytilsmod.Skytils | ||
import gg.skytils.skytilsmod.Skytils.Companion.client | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.launch | ||
import kotlinx.serialization.Serializable | ||
import net.minecraft.client.entity.EntityOtherPlayerMP | ||
import net.minecraftforge.common.MinecraftForge | ||
|
@@ -34,6 +38,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent | |
import java.util.* | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import kotlin.collections.ArrayDeque | ||
import kotlin.concurrent.fixedRateTimer | ||
|
||
|
||
|
@@ -42,6 +47,9 @@ object MojangUtil { | |
private val usernameToUuid: Cache<String, UUID> | ||
private val requestCount = AtomicInteger() | ||
|
||
private val usernameQueue = MutableSharedFlow<Pair<String, Long>>() | ||
private val uuidFlow = MutableSharedFlow<Pair<String, UUID?>>() | ||
|
||
init { | ||
Caffeine.newBuilder() | ||
.weakKeys() | ||
|
@@ -65,6 +73,13 @@ object MojangUtil { | |
} | ||
} | ||
|
||
suspend fun queueUsernameForUUID(name: String): UUID? { | ||
val username = name.lowercase() | ||
usernameQueue.emit(username to System.currentTimeMillis()) | ||
|
||
return uuidFlow.first { it.first == username }.second | ||
} | ||
Comment on lines
+76
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any guarantee that the username will be resolved? Afaict you need to pray someone else calls |
||
|
||
suspend fun getUUIDFromUsername(name: String): UUID? { | ||
val username = name.lowercase() | ||
return usernameToUuid.getIfPresent(username) ?: run { | ||
|
@@ -84,6 +99,46 @@ object MojangUtil { | |
} | ||
} | ||
|
||
suspend fun getUUIDsFromUsernames(names: Collection<String>): Map<String, UUID?> { | ||
if (requestCount.incrementAndGet() % 6 == 0) { | ||
client.get("https://api.minecraftservices.com/minecraft/profile/lookup/name/SlashSlayer?ts=${System.currentTimeMillis()}") | ||
requestCount.getAndIncrement() | ||
} | ||
val usernames = names.mapTo(hashSetOf()) { it.lowercase() } | ||
val map = HashMap<String, UUID?>() | ||
map.putAll(usernameToUuid.getAllPresent(usernames)) | ||
|
||
usernames.removeAll(map.keys) | ||
|
||
usernames.windowed(10).forEach { | ||
client.post("https://api.minecraftservices.com/minecraft/profile/lookup/bulk/byname") { | ||
contentType(ContentType.Application.Json) | ||
setBody(it) | ||
}.let { | ||
when (it.status) { | ||
HttpStatusCode.OK -> { | ||
val response = it.body<List<ProfileResponse>>() | ||
for (profile in response) { | ||
val username = profile.name.lowercase() | ||
val uuid = profile.id | ||
map[username] = uuid | ||
usernameToUuid[username] = uuid | ||
uuidToUsername[uuid] = username | ||
} | ||
} | ||
|
||
else -> throw it.body<MojangException>() | ||
} | ||
} | ||
} | ||
|
||
names.forEach { | ||
map.getOrPut(it) { null } | ||
} | ||
|
||
return map | ||
} | ||
|
||
suspend fun getUsernameFromUUID(uuid: UUID): String? { | ||
return uuidToUsername.getIfPresent(uuid) ?: run { | ||
makeMojangRequest("https://api.minecraftservices.com/minecraft/profile/lookup/${uuid}").let { | ||
|
@@ -107,6 +162,19 @@ object MojangUtil { | |
fixedRateTimer("Mojang-Fake-Requests-Insert", startAt = Date((System.currentTimeMillis() / 60000) * 60000 + 48000), period = 60_000L) { | ||
requestCount.set(0) | ||
} | ||
|
||
Skytils.IO.launch { | ||
val usernames = ArrayDeque<Pair<String, Long>>(10) | ||
usernameQueue.collect { name -> | ||
usernames.add(name) | ||
if (usernames.size >= 10 || usernames.isNotEmpty() && System.currentTimeMillis() - usernames.first().second >= 3000L) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this condition good? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're trying to create what is effectively a bucket ratelimiter, there are probably better ways to handle this (see Levelhead). Otherwise, you'd probably want your |
||
getUUIDsFromUsernames(usernames.map { it.first }).forEach { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be better to directly emit the API response? |
||
uuidFlow.emit(it.toPair()) | ||
} | ||
usernames.clear() | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if 2 queue the same username what will happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should still emit. Only
StateFlow
will disregard repeated consecutive elements.