Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.URL;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement;
  9. import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject;
  10. import org.bukkit.craftbukkit.libs.com.google.gson.JsonParser;
  11. public class UUIDFetcher {
  12.  
  13. private static Map<String, String> uuidCache;
  14.  
  15. static {
  16. uuidCache = new HashMap<String, String>();
  17. }
  18. public static String getUUID(String username) {
  19. if (uuidCache.containsKey(username)) return uuidCache.get(username);
  20. try {
  21. URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + username);
  22. InputStream stream = url.openStream();
  23. InputStreamReader inr = new InputStreamReader(stream);
  24. BufferedReader reader = new BufferedReader(inr);
  25. String s = null;
  26. StringBuilder sb = new StringBuilder();
  27. while ((s = reader.readLine()) != null) {
  28. sb.append(s);
  29. }
  30. String result = sb.toString();
  31.  
  32. JsonElement element = new JsonParser().parse(result);
  33. JsonObject obj = element.getAsJsonObject();
  34.  
  35. String uuid = obj.get("id").toString();
  36.  
  37. uuid = uuid.substring(1);
  38. uuid = uuid.substring(0, uuid.length() - 1);
  39.  
  40. uuidCache.put(username, uuid);
  41.  
  42. return uuid;
  43.  
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. return null;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement