-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff3a18c
commit bdcd7b3
Showing
3 changed files
with
48 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |