Class MojangUtils

java.lang.Object
de.murmelmeister.library.utils.MojangUtils

public final class MojangUtils extends Object
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
    Modifier and Type
    Field
    Description
    private static final String
     
    private static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.google.gson.JsonObject
    fromUsername(String username)
    Retrieves a JSON object representing a Minecraft user profile based on the provided username.
    static com.google.gson.JsonObject
    Retrieves a JSON object representing a Minecraft user profile based on the provided UUID.
    static com.google.gson.JsonObject
    fromUuid(UUID uuid)
    Converts a UUID to its corresponding JsonObject representation.
    static String
    getUsername(UUID playerUUID)
    Retrieves the username associated with the provided Minecraft player UUID.
    static UUID
    getUUID(String username)
    Retrieves the UUID of a Minecraft player based on their username.
    private static com.google.gson.JsonObject
    Retrieves a JSON object from a given URL by making an HTTP request and parsing the response.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • MojangUtils

      public MojangUtils()
  • Method Details

    • getUUID

      public static UUID getUUID(String username) throws IOException, URISyntaxException
      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 UUID of 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

      public static String getUsername(UUID playerUUID) throws IOException, URISyntaxException
      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 - The UUID of 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

      public static com.google.gson.JsonObject fromUuid(UUID uuid)
      Converts a UUID to its corresponding JsonObject representation. This method delegates the UUID processing to its String-based counterpart.
      Parameters:
      uuid - The UUID of the Minecraft player whose profile is to be retrieved.
      Returns:
      A JsonObject containing the profile data associated with the specified UUID, or null in case of an error during the retrieval process.
    • fromUuid

      public static com.google.gson.JsonObject fromUuid(String uuid)
      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 JsonObject containing the profile data of the specified UUID, or null if an error occurs during the retrieval process.
    • fromUsername

      public static com.google.gson.JsonObject fromUsername(String username)
      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 JsonObject containing the profile data of the specified username, or null if 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 JsonObject representing 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.