@@ -4,35 +4,42 @@ import kotlinx.coroutines.runBlocking
44import tech.aliorpse.mcutils.model.player.PlayerProfile
55
66object 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