Skip to content

Commit da4683d

Browse files
fix: send fake requests to the Mojang API to prevent 429 errors
1 parent b59ec1a commit da4683d

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/main/kotlin/gg/skytils/skytilsmod/utils/MojangUtil.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import gg.essential.lib.caffeine.cache.Caffeine
2323
import gg.skytils.skytilsmod.Skytils.Companion.client
2424
import io.ktor.client.call.*
2525
import io.ktor.client.request.*
26+
import io.ktor.client.statement.*
2627
import io.ktor.http.*
2728
import kotlinx.serialization.Serializable
2829
import net.minecraft.client.entity.EntityOtherPlayerMP
@@ -32,11 +33,14 @@ import net.minecraftforge.fml.common.eventhandler.EventPriority
3233
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
3334
import java.util.*
3435
import java.util.concurrent.TimeUnit
36+
import java.util.concurrent.atomic.AtomicInteger
37+
import kotlin.concurrent.fixedRateTimer
3538

3639

3740
object MojangUtil {
3841
private val uuidToUsername: Cache<UUID, String>
3942
private val usernameToUuid: Cache<String, UUID>
43+
private val requestCount = AtomicInteger()
4044

4145
init {
4246
Caffeine.newBuilder()
@@ -64,7 +68,7 @@ object MojangUtil {
6468
suspend fun getUUIDFromUsername(name: String): UUID? {
6569
val username = name.lowercase()
6670
return usernameToUuid.getIfPresent(username) ?: run {
67-
client.get("https://api.mojang.com/users/profiles/minecraft/$username").let {
71+
makeMojangRequest("https://api.mojang.com/users/profiles/minecraft/$username").let {
6872
when (it.status) {
6973
HttpStatusCode.OK -> {
7074
val (id, _) = it.body<ProfileResponse>()
@@ -82,7 +86,7 @@ object MojangUtil {
8286

8387
suspend fun getUsernameFromUUID(uuid: UUID): String? {
8488
return uuidToUsername.getIfPresent(uuid) ?: run {
85-
client.get("https://api.mojang.com/user/profile/${uuid}").let {
89+
makeMojangRequest("https://api.mojang.com/user/profile/${uuid}").let {
8690
when (it.status) {
8791
HttpStatusCode.OK -> {
8892
val (_, name) = it.body<ProfileResponse>()
@@ -99,6 +103,22 @@ object MojangUtil {
99103
}
100104
}
101105

106+
init {
107+
fixedRateTimer("Mojang-Fake-Requests-Insert", startAt = Date((System.currentTimeMillis() / 60000) * 60000 + 48000), period = 60_000L) {
108+
requestCount.set(0)
109+
}
110+
}
111+
112+
/**
113+
* @see <a href="https://bugs.mojang.com/browse/WEB-6830">WEB-6830</a>
114+
*/
115+
private suspend fun makeMojangRequest(url: String): HttpResponse {
116+
if (requestCount.incrementAndGet() % 6 == 0) {
117+
client.get("https://api.mojang.com/users/profiles/minecraft/SlashSlayer?ts=${System.currentTimeMillis()}")
118+
}
119+
return client.get(url)
120+
}
121+
102122
@Serializable
103123
private data class ProfileResponse(
104124
@Serializable(with = UUIDAsString::class) val id: UUID,

0 commit comments

Comments
 (0)