Skip to content

Commit b03c217

Browse files
committed
automatically distinguish between UUID and name
1 parent a49ff52 commit b03c217

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies {
1818

1919
### Server Status
2020

21-
#### [Demo API Endpoint](https://api.aliorpse.tech/minecraft/server/status/java/hypixel.net)
21+
#### [Demo API Endpoint](https://api.aliorpse.tech/minecraft/server/status/hypixel.net:25565?type=java)
2222

2323
```kotlin
2424
runBlocking {
@@ -41,13 +41,14 @@ runBlocking {
4141

4242
### Player Profile (Java Edition Only)
4343

44-
#### [Demo API Endpoint](https://api.aliorpse.tech/minecraft/player/profile/name/Aliorpse)
44+
#### [Demo API Endpoint](https://api.aliorpse.tech/minecraft/player/profile/Aliorpse)
4545

4646
```kotlin
4747
runBlocking {
4848
var pl
49-
pl = Player.getProfile("Aliorpse", Player.IDType.NAME)
50-
pl = Player.getProfile("ec042e1200ac4a249cc83eb1fab0bd88", Player.IDType.UUID)
49+
pl = Player.getProfile("Aliorpse")
50+
pl = Player.getProfile("ec042e1200ac4a249cc83eb1fab0bd88")
51+
pl = Player.getProfile("ec042e12-00ac-4a24-9cc8-3eb1fab0bd88")
5152

5253
println(pl)
5354
}

src/main/kotlin/tech/aliorpse/mcutils/model/server/MOTDTextComponentAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.squareup.moshi.ToJson
77
import tech.aliorpse.mcutils.modules.server.JavaServer.moshi
88

99
/**
10-
* 有些傻逼服务器喜欢往 [MOTDTextComponent.text] 里面加§. mcutils不支持这种写法. §不会做处理.
10+
* 有些傻逼服务器喜欢往 [MOTDTextComponent.text] 里面加§. mcutils不支持这种写法. 这里的§不会做处理.
1111
*/
1212
internal class MOTDTextComponentAdapter(
1313
private val colorAdapter: ColorAdapter

src/main/kotlin/tech/aliorpse/mcutils/modules/player/Player.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,42 @@ import kotlinx.coroutines.runBlocking
44
import tech.aliorpse.mcutils.model.player.PlayerProfile
55

66
object Player {
7-
/**
8-
* UUID or NAME.
9-
*/
10-
enum class IDType {
11-
UUID, NAME
12-
}
7+
const val UUID_LENGTH = 32
8+
val nameRegex = Regex("^[A-Za-z0-9_]{3,16}$")
139

1410
/**
15-
* Retrieves the player's profile from the Mojang session server.
11+
* Fetches a player's profile from Mojang's session server.
12+
*
13+
* The input can be either:
14+
* - A UUID (with or without dashes, 32–36 characters), or
15+
* - A valid Minecraft username (3–16 characters, letters, digits, and underscores).
1616
*
17-
* @param id Either UUID or name.
18-
* @return `PlayerProfile` containing detailed player information including id, name, skin, cape, and model type.
17+
* @param player The player's UUID or username.
18+
* @return A [PlayerProfile] containing the player's UUID, username, skin, cape, and model type.
19+
* @throws IllegalArgumentException if the input is neither a valid UUID nor a valid username.
1920
*/
20-
suspend fun getProfile(id: String, type: IDType): PlayerProfile {
21-
return when (type) {
22-
IDType.UUID -> PlayerClient.sessionService.getProfile(id)
21+
suspend fun getProfile(player: String): PlayerProfile {
22+
val pl = player.replace("-", "")
2323

24-
IDType.NAME -> {
25-
val id = PlayerClient.profileService.getUUID(id).id
26-
PlayerClient.sessionService.getProfile(id)
24+
return when {
25+
pl.length == UUID_LENGTH -> {
26+
PlayerClient.profileService.getProfile(pl)
2727
}
28+
29+
nameRegex.matches(pl) -> {
30+
val uuid = PlayerClient.sessionService.getUUID(pl).id
31+
PlayerClient.sessionService.getProfile(uuid)
32+
}
33+
34+
else -> throw IllegalArgumentException("Invalid identifier: $pl")
2835
}
2936
}
3037

3138
/**
3239
* Blocking method of [getProfile].
3340
*/
3441
@JvmStatic
35-
fun getProfileBlocking(id: String, type: IDType): PlayerProfile = runBlocking {
36-
getProfile(id, type)
42+
fun getProfileBlocking(player: String): PlayerProfile = runBlocking {
43+
getProfile(player)
3744
}
3845
}

0 commit comments

Comments
 (0)