99import org .bukkit .entity .Player ;
1010import xyz .theprogramsrc .supercoreapi .global .utils .Utils ;
1111
12+ import java .io .IOException ;
1213import java .net .URL ;
1314import java .nio .charset .Charset ;
1415import java .util .UUID ;
1516
1617@ SuppressWarnings ("ALL" )
1718public class SkinTexture {
1819 private String url ;
19- private long createdAt ;
2020
2121 /**
2222 * Creates a new SkinTexture
2323 * @param url the url of the texture
24- * @param createdAt Time created {@link System#currentTimeMillis()}
2524 */
26- public SkinTexture (String url , long createdAt ) {
25+ public SkinTexture (String url ) {
2726 this .url = url ;
28- this .createdAt = createdAt ;
29- }
30-
31- /**
32- * Parse a SkinTexture from serialized format
33- * (URL:split:TIME_CREATED)
34- * @param serialized
35- */
36- public SkinTexture (String serialized ) {
37- this .url = serialized .split (":split:" )[0 ];
38- this .createdAt = Long .valueOf (serialized .split (":split:" )[1 ]);
3927 }
4028
4129 /**
@@ -46,21 +34,13 @@ public String getUrl() {
4634 return this .url ;
4735 }
4836
49- /**
50- * Gets when was created the SkinTexture
51- * @return creation time in millis
52- */
53- public long getCreatedAt () {
54- return this .createdAt ;
55- }
56-
5737 /**
5838 * Serialize the SkinTexture
5939 * (URL:split:TIME_CREATED)
6040 * @return the serialized SkinTexture
6141 */
6242 public String toString () {
63- return this .url + ":split:" + this . createdAt ;
43+ return this .url ;
6444 }
6545
6646 /**
@@ -70,15 +50,10 @@ public String toString() {
7050 */
7151 public static SkinTexture fromPlayer (Player player ) {
7252 GameProfile gameProfile = PlayerGameProfile .get (player );
73-
74- try {
75- Property texturesProperty = (Property )gameProfile .getProperties ().get ("textures" ).iterator ().next ();
76- String texturesPropertyValue = texturesProperty .getValue ();
77- String base64ToUrl = base64ToUrl (texturesPropertyValue );
78- return base64ToUrl != null && !base64ToUrl .equals ("null" ) ? new SkinTexture (base64ToUrl , System .currentTimeMillis ()) : null ;
79- } catch (Exception ex ) {
80- return null ;
81- }
53+ Property texturesProperty = (Property )gameProfile .getProperties ().get ("textures" ).iterator ().next ();
54+ String texturesPropertyValue = texturesProperty .getValue ();
55+ String base64ToUrl = base64ToUrl (texturesPropertyValue );
56+ return base64ToUrl != null && !base64ToUrl .equals ("null" ) ? new SkinTexture (base64ToUrl ) : null ;
8257 }
8358
8459 /**
@@ -87,16 +62,9 @@ public static SkinTexture fromPlayer(Player player) {
8762 * @return the skin
8863 */
8964 public static SkinTexture fromMojang (String playerName ) {
90- String url = "https://api.mojang.com/users/profiles/minecraft/" + playerName ;
91- String response = getResponse (url );
92-
93- try {
94- String uuid = (new JsonParser ()).parse (response ).getAsJsonObject ().get ("id" ).getAsString ();
95- String fullUUID = Utils .uuidToFullUUID (uuid );
96- return fromMojang (UUID .fromString (fullUUID ));
97- } catch (Exception ex ) {
98- return null ;
99- }
65+ String uuid = (new JsonParser ()).parse (getResponse ("https://api.mojang.com/users/profiles/minecraft/" + playerName )).getAsJsonObject ().get ("id" ).getAsString ();
66+ String fullUUID = Utils .uuidToFullUUID (uuid );
67+ return fromMojang (UUID .fromString (fullUUID ));
10068 }
10169
10270 /**
@@ -105,15 +73,9 @@ public static SkinTexture fromMojang(String playerName) {
10573 * @return the skin
10674 */
10775 public static SkinTexture fromMojang (UUID uuid ) {
108- String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid .toString ().replace ("-" , "" ) + "?unsigned=false" ;
109- String response = getResponse (url );
110- try {
111- JsonObject properties = (new JsonParser ()).parse (response ).getAsJsonObject ().get ("properties" ).getAsJsonArray ().get (0 ).getAsJsonObject ();
112- String value = properties .get ("value" ).getAsString ();
113- return new SkinTexture (base64ToUrl (value ), System .currentTimeMillis ());
114- } catch (Exception ex ) {
115- return null ;
116- }
76+ JsonObject properties = (new JsonParser ()).parse (getResponse ("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid .toString ().replace ("-" , "" ) + "?unsigned=false" )).getAsJsonObject ().get ("properties" ).getAsJsonArray ().get (0 ).getAsJsonObject ();
77+ String value = properties .get ("value" ).getAsString ();
78+ return new SkinTexture (base64ToUrl (value ));
11779 }
11880
11981 /**
@@ -122,31 +84,22 @@ public static SkinTexture fromMojang(UUID uuid) {
12284 * @return the skin
12385 */
12486 public static SkinTexture fromURL (String url ){
125- return new SkinTexture (url , System . currentTimeMillis () );
87+ return new SkinTexture (url );
12688 }
12789
12890 private static String getResponse (String stringUrl ) {
12991 try {
13092 URL url = new URL (stringUrl );
13193 return IOUtils .toString (url .openStream (), Charset .forName ("UTF-8" ));
132- } catch (Exception ex ) {
94+ } catch (IOException e ) {
95+ e .printStackTrace ();
13396 return null ;
13497 }
13598 }
13699
137100 private static String base64ToUrl (String base64 ) {
138- try {
139- String var1 = new String (Base64 .decodeBase64 (base64 .getBytes ()));
140- JsonObject var2 = (JsonObject )(new JsonParser ()).parse (var1 );
141- JsonObject var3 = var2 .getAsJsonObject ("textures" ).getAsJsonObject ("SKIN" );
142- if (var3 == null ) {
143- return null ;
144- } else {
145- String var4 = var3 .get ("url" ).getAsString ();
146- return var4 ;
147- }
148- } catch (Exception ex ) {
149- return null ;
150- }
101+ String content = new String (Base64 .decodeBase64 (base64 .getBytes ()));
102+ JsonObject json = new JsonParser ().parse (content ).getAsJsonObject ().get ("textures" ).getAsJsonObject ().get ("SKIN" ).getAsJsonObject ();
103+ return json != null ? json .get ("url" ).getAsString () : null ;
151104 }
152105}
0 commit comments