Package de.murmelmeister.library.utils
Class MojangUtils
java.lang.Object
de.murmelmeister.library.utils.MojangUtils
MojangUtils is a utility class for interacting with the Mojang API to retrieve Minecraft player profiles.
It provides methods to fetch player UUIDs based on usernames and vice versa, as well as to retrieve
player profiles in JSON format.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic com.google.gson.JsonObjectfromUsername(String username) Retrieves a JSON object representing a Minecraft user profile based on the provided username.static com.google.gson.JsonObjectRetrieves a JSON object representing a Minecraft user profile based on the provided UUID.static com.google.gson.JsonObjectConverts aUUIDto its correspondingJsonObjectrepresentation.static StringgetUsername(UUID playerUUID) Retrieves the username associated with the provided Minecraft player UUID.static UUIDRetrieves the UUID of a Minecraft player based on their username.private static com.google.gson.JsonObjectRetrieves a JSON object from a given URL by making an HTTP request and parsing the response.
-
Field Details
-
FROM_UUID_URL
- See Also:
-
FROM_USERNAME_URL
- See Also:
-
-
Constructor Details
-
MojangUtils
public MojangUtils()
-
-
Method Details
-
getUUID
Retrieves the UUID of a Minecraft player based on their username. This method makes a request to the Mojang API to fetch the corresponding UUID.- Parameters:
username- The Minecraft player's username whose UUID is to be retrieved.- Returns:
- The
UUIDof the specified Minecraft player. - Throws:
IOException- If an I/O error occurs during the API request.URISyntaxException- If the constructed URL for the API request is invalid.
-
getUsername
Retrieves the username associated with the provided Minecraft player UUID. This method makes a request to the Mojang API to fetch the corresponding username.- Parameters:
playerUUID- TheUUIDof the Minecraft player whose username is to be retrieved.- Returns:
- The username associated with the specified UUID.
- Throws:
IOException- If an I/O error occurs during the API request.URISyntaxException- If the constructed URL for the API request is invalid.
-
fromUuid
Converts aUUIDto its correspondingJsonObjectrepresentation. This method delegates the UUID processing to its String-based counterpart.- Parameters:
uuid- TheUUIDof the Minecraft player whose profile is to be retrieved.- Returns:
- A
JsonObjectcontaining the profile data associated with the specified UUID, ornullin case of an error during the retrieval process.
-
fromUuid
Retrieves a JSON object representing a Minecraft user profile based on the provided UUID. The method makes a request to the Mojang API to fetch the profile information associated with the UUID.- Parameters:
uuid- The UUID of the Minecraft player whose profile is to be retrieved.- Returns:
- A
JsonObjectcontaining the profile data of the specified UUID, ornullif an error occurs during the retrieval process.
-
fromUsername
Retrieves a JSON object representing a Minecraft user profile based on the provided username. Makes a request to the Mojang API to retrieve the profile information.- Parameters:
username- The username of the Minecraft player whose profile is to be retrieved.- Returns:
- A
JsonObjectcontaining the profile data of the specified username, ornullif an error occurs during the retrieval process.
-
retrieve
private static com.google.gson.JsonObject retrieve(String url) throws IOException, URISyntaxException Retrieves a JSON object from a given URL by making an HTTP request and parsing the response.- Parameters:
url- The URL to retrieve the JSON object from.- Returns:
- A
JsonObjectrepresenting the parsed JSON response from the given URL. - Throws:
IOException- If an I/O exception occurs during the HTTP request or if the response is empty.URISyntaxException- If the provided URL is not a valid URI.
-