Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/main/java/net/minestom/server/utils/mojang/MojangUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@

import java.io.IOException;
import java.util.UUID;
import java.util.regex.Pattern;

/**
* Utils class using mojang API.
*/
public final class MojangUtils {
private static final String FROM_UUID_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false";
private static final String FROM_USERNAME_URL = "https://api.mojang.com/users/profiles/minecraft/%s";
private static final Pattern UUID_PATTERN =
Pattern.compile("(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)");

private MojangUtils() {
// Private constructor to prevent instantiation of the utility class
}

/**
* Gets a player's UUID from their username
Expand All @@ -27,14 +34,8 @@ public final class MojangUtils {
@Blocking
public static @NotNull UUID getUUID(String username) throws IOException {
// Thanks stackoverflow: https://stackoverflow.com/a/19399768/13247146
return UUID.fromString(
retrieve(String.format(FROM_USERNAME_URL, username)).get("id")
.getAsString()
.replaceFirst(
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)",
"$1-$2-$3-$4-$5"
)
);
String uuidString = retrieve(String.format(FROM_USERNAME_URL, username)).get("id").getAsString();
return UUID.fromString(UUID_PATTERN.matcher(uuidString).replaceFirst("$1-$2-$3-$4-$5"));
}

/**
Expand Down