forked from jeremylvln/Shulker
-
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.
feat(shulker-proxy-agent): query mojang api to get unknown player nam…
…es or ids
- Loading branch information
1 parent
b1351ec
commit e7eb734
Showing
5 changed files
with
100 additions
and
3 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
49 changes: 49 additions & 0 deletions
49
...ent/src/common/kotlin/io/shulkermc/proxyagent/adapters/mojang/HttpMojangGatewayAdapter.kt
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,49 @@ | ||
package io.shulkermc.proxyagent.adapters.mojang | ||
|
||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParser | ||
import java.net.HttpURLConnection | ||
import java.net.URI | ||
import java.util.Optional | ||
import java.util.UUID | ||
|
||
class HttpMojangGatewayAdapter : MojangGatewayAdapter { | ||
override fun getProfileFromName(playerName: String): Optional<MojangGatewayAdapter.MojangProfile> { | ||
val url = URI("https://api.mojang.com/users/profiles/minecraft/$playerName").toURL() | ||
val connection = url.openConnection() as HttpURLConnection | ||
connection.requestMethod = "GET" | ||
|
||
val status = connection.responseCode | ||
if (status != 200) return Optional.empty() | ||
|
||
val response = connection.inputStream.bufferedReader().use { it.readText() } | ||
val responseJson = JsonParser.parseString(response).asJsonObject | ||
return Optional.of(this.getProfileFromJson(responseJson)) | ||
} | ||
|
||
override fun getProfileFromId(playerId: UUID): Optional<MojangGatewayAdapter.MojangProfile> { | ||
val undashedPlayerId = playerId.toString().replace("-", "") | ||
val url = URI("https://sessionserver.mojang.com/session/minecraft/profile/$undashedPlayerId").toURL() | ||
val connection = url.openConnection() as HttpURLConnection | ||
connection.requestMethod = "GET" | ||
|
||
val status = connection.responseCode | ||
if (status != 200) return Optional.empty() | ||
|
||
val response = connection.inputStream.bufferedReader().use { it.readText() } | ||
val responseJson = JsonParser.parseString(response).asJsonObject | ||
return Optional.of(this.getProfileFromJson(responseJson)) | ||
} | ||
|
||
private fun getProfileFromJson(json: JsonObject): MojangGatewayAdapter.MojangProfile { | ||
val uuid = UUID.fromString( | ||
json.get("id").asString.replaceFirst( | ||
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", | ||
"$1-$2-$3-$4-$5" | ||
) | ||
) | ||
val name = json.get("name").asString | ||
|
||
return MojangGatewayAdapter.MojangProfile(uuid, name) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...y-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/mojang/MojangGatewayAdapter.kt
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,11 @@ | ||
package io.shulkermc.proxyagent.adapters.mojang | ||
|
||
import java.util.Optional | ||
import java.util.UUID | ||
|
||
interface MojangGatewayAdapter { | ||
fun getProfileFromName(playerName: String): Optional<MojangProfile> | ||
fun getProfileFromId(playerId: UUID): Optional<MojangProfile> | ||
|
||
data class MojangProfile(val playerId: UUID, val playerName: 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
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