Skip to content

Commit 15d4ab4

Browse files
committed
feat: enhance PlayerLookupService with Mojang and Minecraft Services API integration
1 parent 9b28934 commit 15d4ab4

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

.idea/codeStyles/Project.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
77
javaVersion=21
88
mcVersion=1.21.4
99
group=dev.slne.surf
10-
version=1.21.4-2.15.0-SNAPSHOT
10+
version=1.21.4-2.15.1-SNAPSHOT
1111
relocationPrefix=dev.slne.surf.surfapi.libs

surf-api-core/surf-api-core-api/src/main/kotlin/dev/slne/surf/surfapi/core/api/service/PlayerLookupService.kt

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ object PlayerLookupService {
4545
*/
4646
private val nameToUuid = Caffeine.newBuilder()
4747
.expireAfterWrite(15.minutes)
48-
.asLoadingCache<String, UUID?> {
48+
.asLoadingCache<String, UUID?> { name ->
4949
try {
50-
MinecraftApi.getUuid(it)
50+
MojangApi.getUuid(name)
5151
} catch (_: Exception) {
5252
try {
53-
MinetoolsApi.getUuid(it)
53+
MinecraftServicesApi.getUuid(name)
5454
} catch (_: Exception) {
55-
null
55+
try {
56+
MinetoolsApi.getUuid(name)
57+
} catch (_: Exception) {
58+
null
59+
}
5660
}
5761
}
5862
}
@@ -63,15 +67,19 @@ object PlayerLookupService {
6367
*/
6468
private val uuidToName = Caffeine.newBuilder()
6569
.expireAfterWrite(15.minutes)
66-
.asLoadingCache<UUID, String?> {
70+
.asLoadingCache<UUID, String?> { uuid ->
6771
try {
68-
MinecraftApi.getUsername(it)
72+
MojangApi.getUsername(uuid)
6973
} catch (_: Exception) {
7074
try {
71-
MinetoolsApi.getUsername(it)
75+
MinecraftServicesApi.getUsername(uuid)
76+
} catch (_: Exception) {
77+
try {
78+
MinetoolsApi.getUsername(uuid)
7279
} catch (_: Exception) {
7380
null
7481
}
82+
}
7583
}
7684
}
7785

@@ -92,7 +100,7 @@ object PlayerLookupService {
92100
/**
93101
* API interaction with Mojang's official endpoints.
94102
*/
95-
private object MinecraftApi {
103+
private object MojangApi {
96104
private const val BASE_URL = "https://api.mojang.com"
97105

98106
/**
@@ -118,6 +126,32 @@ object PlayerLookupService {
118126
}
119127
}
120128

129+
private object MinecraftServicesApi {
130+
private const val BASE_URL = "https://api.minecraftservices.com"
131+
132+
/**
133+
* Fetches username from Minecraft Services using UUID.
134+
* @param uuid Player UUID.
135+
* @return Username retrieved from Minecraft Services.
136+
*/
137+
suspend fun getUsername(uuid: UUID): String {
138+
return client.get("$BASE_URL/minecraft/profile/lookup/${UUIDSerializer.fromUUID(uuid)}")
139+
.body<MojangResponse>()
140+
.name
141+
}
142+
143+
/**
144+
* Fetches UUID from Minecraft Services using username.
145+
* @param username Player username.
146+
* @return UUID retrieved from Minecraft Services.
147+
*/
148+
suspend fun getUuid(username: String): UUID {
149+
return client.get("$BASE_URL/minecraft/profile/lookup/name/$username")
150+
.body<MojangResponse>()
151+
.id
152+
}
153+
}
154+
121155
/**
122156
* API interaction with Minetools as a fallback.
123157
*/

0 commit comments

Comments
 (0)