1+ package cn .xiaopangxie732 .mojang_api ;
2+
3+ import java .util .Properties ;
4+
5+ import com .google .gson .Gson ;
6+ import com .google .gson .JsonSyntaxException ;
7+
8+ import cn .xiaopangxie732 .mojang_api .exceptions .UsernameOrTimestampInvalidException ;
9+ import cn .xiaopangxie732 .mojang_api .util .Net ;
10+
11+ /**
12+ * To get UUID of username(playername).
13+ * @author XiaoPangxie732
14+ */
15+ public class UserName {
16+
17+ private static String url = "https://api.mojang.com/users/profiles/minecraft/" ;
18+ private static Gson json = new Gson ();
19+
20+ /**
21+ * Get the UUID of this playername at a time.
22+ * @param username The playername needs to get UUID.
23+ * @param timestamp A UNIX timestamp (without milliseconds).
24+ * @return The UUID of the name at the timestamp provided.
25+ * @throws IllegalArgumentException When the timestamp is invalid.
26+ * @throws UsernameOrTimestampInvalidException When the username is invalid or the timestamp has some error(or playername wasn't change at least once).
27+ * @since 0.0.3
28+ * @author XiaoPangxie732
29+ */
30+ public static String UUIDAtTime (String username , long timestamp ) throws UsernameOrTimestampInvalidException , IllegalArgumentException {
31+ String furl = url + username + "?at=" + timestamp ;
32+ String response = null ;
33+ try {
34+ response = Net .getConnection (furl );
35+ } catch (IllegalArgumentException ex ) {
36+ throw new UsernameOrTimestampInvalidException ("Username \" " + username + "\" is invalid or on timestamp \" " + Long .toString (timestamp ).replace ("0" , "Original" ) + "\" the username wasn't change." );
37+ }
38+ Properties result = json .fromJson (response , Properties .class );
39+ if (!result .getProperty ("error" , "noError" ).equals ("noError" ))
40+ throw new IllegalArgumentException (result .getProperty ("errorMessage" ));
41+ return result .getProperty ("id" );
42+ }
43+
44+ /**
45+ * Get the UUID of this playername at now.
46+ * @param username The playername needs to get UUID.
47+ * @return The UUID of the name at now.
48+ * @throws UsernameOrTimestampInvalidException When the username is invalid.
49+ * @since 0.0.3
50+ * @author XiaoPangxie732
51+ */
52+ public static String UUIDAtNow (String username ) throws UsernameOrTimestampInvalidException {
53+ String furl = url + username ;
54+ String response ;
55+ try {
56+ response = Net .getConnection (furl );
57+ } catch (IllegalArgumentException e ) {
58+ throw new UsernameOrTimestampInvalidException ("Username \" " + username + "\" is invalid" );
59+ }
60+ Properties result = json .fromJson (response , Properties .class );
61+ return result .getProperty ("id" );
62+ }
63+
64+ /**
65+ * Get the UUID of this playername at original time (timestamp=0).
66+ * @param username The playername needs to get UUID.
67+ * @return The UUID of the name at the original.
68+ * @throws UsernameOrTimestampInvalidException When the username is invalid or playername wasn't change at least once.
69+ * @since 0.0.3
70+ * @author XiaoPangxie732
71+ */
72+ public static String UUIDAtOriginal (String username ) throws UsernameOrTimestampInvalidException {
73+ return UUIDAtTime (username , 0 );
74+ }
75+
76+ /**
77+ * Get the UUID of the playername(s).
78+ * @param usernames The playername(s) needs to get UUID.
79+ * @return The UUID(s) of the playername(s).
80+ * @throws IllegalArgumentException When the request names reached more than 100.
81+ * @throws IllegalArgumentException when any of the usernames is <code>null</code> or <code>""</code>.
82+ */
83+ public static String UUIDOfNames (String ... usernames ) throws IllegalArgumentException {
84+ if (usernames .length > 100 ) throw new IllegalArgumentException ("Too more names! (" + usernames .length + "/100)" );
85+ String response = Net .postConnection ("https://api.mojang.com/profiles/minecraft" , "application/json" , json .toJson (usernames ));
86+ Properties [] names = null ;
87+ try {
88+ names = json .fromJson (response , Properties [].class );
89+ } catch (JsonSyntaxException e ) {
90+ throw new IllegalArgumentException (json .fromJson (response , Properties .class ).getProperty ("errorMessage" ));
91+ }
92+ String result = "" ;
93+ for (Properties output : names ) {
94+ result += output .getProperty ("name" ) + "=" + output .getProperty ("id" ) + "\n " ;
95+ }
96+ return result ;
97+ }
98+ }
0 commit comments