Skip to content

Commit

Permalink
fix: mojang api ratelimits
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidJoker committed Jan 28, 2025
1 parent ff3a18c commit bdcd7b3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {

testImplementation("io.kotest:kotest-runner-junit5:$koTestVersion")
testImplementation("io.mockk:mockk:${mockkVersion}")
testImplementation("com.google.code.gson:gson:2.11.0")
}

paperweight {
Expand Down
36 changes: 20 additions & 16 deletions src/main/kotlin/cc/modlabs/kpaper/utils/MojangAPI.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package cc.modlabs.kpaper.utils

import com.google.gson.Gson
import java.net.URL
import java.net.URI

class MojangAPI {
private val gson = Gson()

fun getUUID(name: String): String {
val baseUrl = "https://api.mojang.com/users/profiles/minecraft/$name"
val response = URL(baseUrl).readText()
val uuidResponse = gson.fromJson(response, MojangUUIDResponse::class.java)
return uuidResponse.id.replace("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})".toRegex(), "$1-$2-$3-$4-$5")
}
fun getUser(user: String): MclSuccessResponse? {
val url = "https://mcl.flawcra.cc/$user"
val response = URI.create(url).toURL().readText()

val errorResponse = gson.fromJson(response, MclErrorResponse::class.java)
if (errorResponse.error != null) {
return null
}

fun getUsername(uuid: String): String {
// http request to mojang api to get username
val responseUrl = "https://sessionserver.mojang.com/session/minecraft/profile/$uuid"
val response = URL(responseUrl).readText()
val skinResponse = gson.fromJson(response, SkinResponse::class.java)
return skinResponse.name
return gson.fromJson(response, MclSuccessResponse::class.java)
}
}

data class SkinResponse(val id: String, val name: String, val properties: List<MojangProperty>)
data class MojangProperty(val name: String, val value: String)
data class MclErrorResponse(
val code: String? = null,
val error: String? = null
)

data class MojangUUIDResponse(val name: String, val id: String)
data class MclSuccessResponse(
val username: String,
val id: String, // already hyphenated
val avatar: String,
val skin_texture: String,
)
27 changes: 27 additions & 0 deletions src/test/kotlin/cc/modlabs/kpaper/MojangTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cc.modlabs.kpaper

import cc.modlabs.kpaper.utils.MojangAPI
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class MojangTest: FunSpec() {

init {
coroutineTestScope = true
test("testMojangAPI") {
val api = MojangAPI()
val uuid = api.getUser("Notch")
println("UUID: $uuid")

val username = api.getUser(uuid!!.id)

username?.username shouldBe "Notch"
println("Username: $username")

val fail = api.getUser("NotchNotch2348723467823467887462387")
fail shouldBe null

println("Fail: $fail")
}
}
}

0 comments on commit bdcd7b3

Please sign in to comment.