1010import java .io .OutputStream ;
1111import java .lang .reflect .Type ;
1212import java .net .HttpURLConnection ;
13+ import java .net .Proxy ;
14+ import java .net .URL ;
1315import java .nio .charset .StandardCharsets ;
1416import java .util .List ;
1517import java .util .UUID ;
2022 * Connections are established using the RequestWithProxyParser class to support proxy configurations.
2123 */
2224public class MojangAPIParser {
23-
2425 private static final String BASE_URL = "https://api.minecraftservices.com/minecraft/profile/lookup" ;
26+ private static final String NAME_ENDPOINT = "/name/" ;
27+ private static final String BULK_ENDPOINT = "/bulk/byname" ;
28+ private static final String CONTENT_TYPE = "Content-Type" ;
29+ private static final String APPLICATION_JSON = "application/json" ;
30+ private static final String POST_METHOD = "POST" ;
2531
2632 /**
27- * Looks up a Minecraft profile by its username using the Mojang API .
33+ * Retrieves the profile information for a user based on their username .
2834 *
29- * @param name the username of the Minecraft profile to be looked up
30- * @return a {@code ProfileLookup} object containing the profile's ID and username
31- * @throws IOException if an I/O error occurs while connecting to the Mojang API or parsing the response
35+ * @param name the username for which profile information is to be looked up
36+ * @return a {@link ProfileLookup} object containing the profile information
37+ * @throws IOException if an I/O error occurs during the profile lookup
3238 */
3339 public static ProfileLookup lookupProfile (String name ) throws IOException {
34- String url = BASE_URL + "/name/" + name ;
35- HttpURLConnection connection = RequestWithProxyParser .openConnection (url );
40+ return lookupProfile (true , name );
41+ }
42+
43+ /**
44+ * Looks up profile information for a given username from a remote server.
45+ *
46+ * @param useProxy whether to use a proxy connection for the request
47+ * @param name the username to look up
48+ * @return the {@link ProfileLookup} containing the profile ID and name
49+ * @throws IOException if an I/O error occurs while performing the request or reading the response
50+ */
51+ public static ProfileLookup lookupProfile (boolean useProxy , String name ) throws IOException {
52+ String url = BASE_URL + NAME_ENDPOINT + name ;
53+ HttpURLConnection connection = createConnection (url , useProxy );
3654 return JsonParser .requestAndParse (connection , ProfileLookup .class );
3755 }
3856
3957 /**
40- * Retrieves the Minecraft profile associated with the given UUID by performing a lookup request to the Mojang API .
58+ * Retrieves the profile information associated with the specified UUID.
4159 *
42- * @param uuid the unique identifier of the Minecraft account to look up, must not be null
43- * @return a {@link ProfileLookup} record containing the profile information, including the UUID and username
44- * @throws IOException if an I/O error occurs while making the request or parsing the response
60+ * @param uuid the {@link UUID} of the profile to lookup; must not be null
61+ * @return a {@link ProfileLookup} containing the profile information corresponding to the given UUID
62+ * @throws IOException if an I/O error occurs while making the request or processing the response
4563 */
4664 public static ProfileLookup lookupProfile (UUID uuid ) throws IOException {
65+ return lookupProfile (true , uuid );
66+ }
67+
68+ /**
69+ * Looks up a profile from the Mojang API using the specified UUID.
70+ *
71+ * @param useProxy a boolean indicating whether a proxy should be used for the connection
72+ * @param uuid the UUID of the profile to lookup, must not be null
73+ * @return a {@link ProfileLookup} object containing the profile information
74+ * @throws IOException if an error occurs during the network request or while reading the response
75+ */
76+ public static ProfileLookup lookupProfile (boolean useProxy , UUID uuid ) throws IOException {
4777 String url = BASE_URL + "/" + UUIDParser .removeDashes (uuid );
48- HttpURLConnection connection = RequestWithProxyParser . openConnection (url );
78+ HttpURLConnection connection = createConnection (url , useProxy );
4979 return JsonParser .requestAndParse (connection , ProfileLookup .class );
5080 }
5181
5282 /**
53- * Retrieves a list of Minecraft profiles by performing a bulk lookup using an array of usernames.
83+ * Retrieves a list of profile lookups for the given player names.
84+ *
85+ * @param names the array of player names to look up profiles for; each name must be a valid string
86+ * @return a list of {@code ProfileLookup} records containing the ID and name of each requested profile
87+ * @throws IOException if an I/O error occurs during the HTTP request or response parsing
88+ */
89+ public static List <ProfileLookup > lookupProfiles (String ... names ) throws IOException {
90+ return lookupProfiles (true , names );
91+ }
92+
93+ /**
94+ * Looks up profiles for the given list of names by sending an HTTP request to the Mojang API.
5495 *
55- * @param names an array of usernames to be looked up; each name represents a Minecraft user's profile
56- * @return a list of {@code ProfileLookup} records containing the id and name of the queried profiles
57- * @throws IOException if an I/O error occurs during the HTTP request or response handling
96+ * @param useProxy a boolean indicating whether to use a proxy for the HTTP connection
97+ * @param names a variable-length list of usernames to look up profiles for
98+ * @return a list of {@link ProfileLookup} representing the fetched profile information for each provided name
99+ * @throws IOException if an I/O error occurs while making the request or processing the response
58100 */
59- public static List <ProfileLookup > lookupProfile (String ... names ) throws IOException {
60- String url = BASE_URL + "/bulk/byname" ;
61- HttpURLConnection connection = RequestWithProxyParser .openConnection (url );
62- connection .setRequestMethod ("POST" );
63- connection .setRequestProperty ("Content-Type" , "application/json" );
101+ public static List <ProfileLookup > lookupProfiles (boolean useProxy , String ... names ) throws IOException {
102+ String url = BASE_URL + BULK_ENDPOINT ;
103+ HttpURLConnection connection = createConnection (url , useProxy );
104+
105+ connection .setRequestMethod (POST_METHOD );
106+ connection .setRequestProperty (CONTENT_TYPE , APPLICATION_JSON );
64107 connection .setDoOutput (true );
108+
65109 String jsonBody = JsonParser .toJson (names );
66110 try (OutputStream outputStream = connection .getOutputStream ()) {
67111 outputStream .write (jsonBody .getBytes (StandardCharsets .UTF_8 ));
68112 outputStream .flush ();
69113 }
114+
70115 Type listType = new TypeToken <List <ProfileLookup >>() {
71116 }.getType ();
72117 return JsonParser .requestAndParse (connection , listType );
73118 }
119+
120+ /**
121+ * Creates and returns an {@link HttpURLConnection} instance for the specified URL.
122+ * If the {@code useProxy} flag is set to {@code true}, a connection using a proxy is created.
123+ * Otherwise, a direct connection is instantiated.
124+ *
125+ * @param url the URL to which the connection is to be established
126+ * @param useProxy a boolean flag indicating whether a proxy should be used for the connection
127+ * @return an instance of {@link HttpURLConnection} configured based on the provided parameters
128+ * @throws IOException if an I/O error occurs while creating the connection
129+ */
130+ private static HttpURLConnection createConnection (String url , boolean useProxy ) throws IOException {
131+ return useProxy
132+ ? RequestWithProxyParser .openConnection (url )
133+ : (HttpURLConnection ) new URL (url ).openConnection (Proxy .NO_PROXY );
134+ }
74135}
0 commit comments