Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. package net.mcplaza.auth;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.math.BigInteger;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.UUID;
  9.  
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14. import org.json.simple.JSONObject;
  15. import org.json.simple.parser.JSONParser;
  16.  
  17. import net.mcplaza.auth.UUIDFetcher.Response;
  18.  
  19. public class UUIDFetcher {
  20.  
  21. public static Map<String, UUID> uuidStore = new HashMap<String, UUID>();
  22. public static Map<UUID, String> nameStore = new HashMap<UUID, String>();
  23.  
  24. private static final String agent = "Mozilla/5.0";
  25.  
  26. public static Response getResponse(String name) {
  27.  
  28. if(uuidStore.containsKey(name)) {
  29. Response r = new Response();
  30. r.name = name;
  31. r.uuid = uuidStore.get(name);
  32. return r;
  33. }
  34.  
  35. HttpClient client = new DefaultHttpClient();
  36. HttpGet get = new HttpGet("https://use.gameapis.net/mc/player/profile/" + name);
  37.  
  38. get.addHeader("User-Agent", "Mozilla/5.0");
  39. try {
  40.  
  41. HttpResponse response = client.execute(get);
  42.  
  43. BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  44. JSONParser parser = new JSONParser();
  45. JSONObject object = (JSONObject)parser.parse(reader);
  46. String uuid = (String) object.get("id");
  47. String targetName = (String) object.get("name");
  48.  
  49. UUID id = new UUID(new BigInteger(uuid.substring(0, 16), 16).longValue(), new BigInteger(uuid.substring(16), 16).longValue());
  50. Response r = new Response();
  51. r.name = targetName;
  52. r.uuid = id;
  53. uuidStore.put(name, id);
  54. nameStore.put(id, name);
  55. return r;
  56.  
  57. } catch(Exception ex) {
  58. ex.printStackTrace();
  59. }
  60. return null;
  61.  
  62. }
  63.  
  64. public static Response getResponse(UUID uuid) {
  65.  
  66. if(nameStore.containsKey(uuid)) {
  67. Response r = new Response();
  68. r.name = nameStore.get(uuid);
  69. r.uuid = uuid;
  70. return r;
  71. }
  72.  
  73. HttpClient client = new DefaultHttpClient();
  74.  
  75. HttpGet get = new HttpGet("https://use.gameapis.net/mc/player/profile/" + uuid.toString().replaceAll("-", ""));
  76. get.addHeader("User-Agent", "Mozilla/5.0");
  77. try {
  78.  
  79. HttpResponse response = client.execute(get);
  80.  
  81. BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  82. JSONParser parser = new JSONParser();
  83. JSONObject object = (JSONObject)parser.parse(reader);
  84. String name = (String) object.get("name");
  85. Response r = new Response();
  86. r.name = name;
  87. r.uuid = uuid;
  88. uuidStore.put(name, uuid);
  89. nameStore.put(uuid, name);
  90. return r;
  91.  
  92. } catch(Exception ex) {
  93. ex.printStackTrace();
  94. }
  95. return null;
  96. }
  97.  
  98. public static class Response {
  99. public String name;
  100. public UUID uuid;
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement