Advertisement
XploreLP

Untitled

Mar 8th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package de.xplore.dungeonapi.utility;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.URL;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10.  
  11. import com.google.gson.JsonElement;
  12. import com.google.gson.JsonObject;
  13. import com.google.gson.JsonParser;
  14.  
  15. /**
  16.  * Fetche die UUID eines Spieler anhand seines Usernamen von den Mojang-Servern
  17.  * @author Yannik
  18.  *
  19.  */
  20. public class UUIDFetcher {
  21.    
  22.    private static Map<String, String> uuidCache;
  23.    
  24.    static {
  25.       uuidCache = new HashMap<String, String>();
  26.    }
  27.    /**
  28.     * Bekomme die UUID eines Spielers anhand seines Usernamen
  29.     * @param username Name des Spielers
  30.     * @return UUID des Spielers (ohne Bindestriche)
  31.     */
  32.    public static String getUUID(String username) {
  33.       if (uuidCache.containsKey(username)) return uuidCache.get(username);
  34.       try {
  35.          URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + username);
  36.          InputStream stream = url.openStream();
  37.          InputStreamReader inr = new InputStreamReader(stream);
  38.          BufferedReader reader = new BufferedReader(inr);
  39.          String s = null;
  40.          StringBuilder sb = new StringBuilder();
  41.          while ((s = reader.readLine()) != null) {
  42.             sb.append(s);
  43.          }
  44.          String result = sb.toString();
  45.          
  46.          JsonElement element = new JsonParser().parse(result);
  47.          
  48.          if(element == null){
  49.              return null;
  50.          }
  51.          
  52.          JsonObject obj = element.getAsJsonObject();
  53.          
  54.          String uuid = obj.get("id").toString();
  55.          
  56.          uuid = uuid.substring(1);
  57.          uuid = uuid.substring(0, uuid.length() - 1);
  58.          
  59.          uuidCache.put(username, uuid);
  60.          
  61.          return uuid;
  62.          
  63.       } catch (IOException e) {
  64.          e.printStackTrace();
  65.       }
  66.       return null;
  67.    }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement