77import com .mojang .util .UUIDTypeAdapter ;
88import org .yaml .snakeyaml .external .biz .base64Coder .Base64Coder ;
99
10+ /* DEPRECATED
1011import java.io.BufferedReader;
11- import java .io .IOException ;
1212import java.io.InputStreamReader;
13- import java .lang .reflect .Type ;
14- import java .net .HttpURLConnection ;
1513import java.net.URL;
14+ import java.net.HttpURLConnection; */
15+ import java .io .IOException ;
16+ import java .lang .reflect .Type ;
17+ import java .net .URI ;
18+ import java .net .http .HttpClient ;
19+ import java .net .http .HttpRequest ;
20+ import java .net .http .HttpResponse ;
1621import java .util .*;
1722
1823/**
@@ -29,6 +34,8 @@ public class GameProfileBuilder {
2934 private static final Object sync = new Object ();
3035 private static long cacheTime = -1L ;
3136
37+ private static final HttpClient client = HttpClient .newHttpClient ();
38+
3239 public static GameProfile fetch (UUID uuid ) throws IOException {
3340 return fetch (uuid , false );
3441 }
@@ -38,11 +45,33 @@ public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException
3845 return cache .get (uuid ).profile ;
3946 }
4047
41- HttpURLConnection connection ;
48+ String url = String .format ("https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false" , UUIDTypeAdapter .fromUUID (uuid ));
49+ HttpRequest request = HttpRequest .newBuilder ()
50+ .uri (URI .create (url ))
51+ .GET ()
52+ .timeout (java .time .Duration .ofSeconds (5 )) // Set timeout for request
53+ .build ();
54+
55+ synchronized (sync ) {
56+ try {
57+ HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
58+ if (response .statusCode () == 200 ) {
59+ String json = response .body ();
60+ GameProfile result = gson .fromJson (json , GameProfile .class );
61+ cache .put (uuid , new CachedProfile (result ));
62+ return result ;
63+ }
64+ } catch (InterruptedException e ) {
65+ Thread .currentThread ().interrupt ();
66+ throw new IOException ("Request interrupted" , e );
67+ }
68+ }
69+
70+ //DEPRECATED
71+ /*HttpURLConnection connection;
4272 synchronized (sync) {
4373 connection = (HttpURLConnection) new URL(
44- String .format ("https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false" ,
45- UUIDTypeAdapter .fromUUID (uuid ))).openConnection ();
74+ String.format("https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false", UUIDTypeAdapter.fromUUID(uuid))).openConnection();
4675 connection.setReadTimeout(5000);
4776 }
4877 if (connection.getResponseCode() == 200) {
@@ -52,11 +81,12 @@ public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException
5281 GameProfile result = gson.fromJson(json, GameProfile.class);
5382 cache.put(uuid, new CachedProfile(result));
5483 return result;
55- }
84+ }*/
85+
5686 if ((!forceNew ) && (cache .containsKey (uuid ))) {
5787 return cache .get (uuid ).profile ;
5888 }
59- throw new IOException ("Could not connect to mojang servers for unknown player: " + uuid .toString ());
89+ throw new IOException ("Could not connect to mojang servers for unknown player: " + uuid ); // + uuid.toString());
6090 }
6191
6292 public static GameProfile getProfile (UUID uuid , String name , String skin ) {
0 commit comments