Advertisement
Guest User

Untitled

a guest
May 20th, 2015
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1.  
  2. import com.google.common.collect.ImmutableList;
  3. import org.json.simple.JSONArray;
  4. import org.json.simple.JSONObject;
  5. import org.json.simple.parser.JSONParser;
  6.  
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.nio.ByteBuffer;
  12. import java.util.*;
  13. import java.util.concurrent.Callable;
  14.  
  15. public class UUIDFetcher implements Callable<Map<String, UUID>> {
  16. private static final double PROFILES_PER_REQUEST = 100;
  17. private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
  18. private final JSONParser jsonParser = new JSONParser();
  19. private final List<String> names;
  20. private final boolean rateLimiting;
  21.  
  22. public UUIDFetcher(List<String> names, boolean rateLimiting) {
  23. this.names = ImmutableList.copyOf(names);
  24. this.rateLimiting = rateLimiting;
  25. }
  26.  
  27. public UUIDFetcher(List<String> names) {
  28. this(names, true);
  29. }
  30.  
  31. public Map<String, UUID> call() throws Exception {
  32. Map<String, UUID> uuidMap = new HashMap<String, UUID>();
  33. int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
  34. for (int i = 0; i < requests; i++) {
  35. HttpURLConnection connection = createConnection();
  36. String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
  37. writeBody(connection, body);
  38. JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
  39. for (Object profile : array) {
  40. JSONObject jsonProfile = (JSONObject) profile;
  41. String id = (String) jsonProfile.get("id");
  42. String name = (String) jsonProfile.get("name");
  43. UUID uuid = UUIDFetcher.getUUID(id);
  44. uuidMap.put(name, uuid);
  45. }
  46. if (rateLimiting && i != requests - 1) {
  47. Thread.sleep(100L);
  48. }
  49. }
  50. return uuidMap;
  51. }
  52.  
  53. private static void writeBody(HttpURLConnection connection, String body) throws Exception {
  54. OutputStream stream = connection.getOutputStream();
  55. stream.write(body.getBytes());
  56. stream.flush();
  57. stream.close();
  58. }
  59.  
  60. private static HttpURLConnection createConnection() throws Exception {
  61. URL url = new URL(PROFILE_URL);
  62. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  63. connection.setRequestMethod("POST");
  64. connection.setRequestProperty("Content-Type", "application/json");
  65. connection.setUseCaches(false);
  66. connection.setDoInput(true);
  67. connection.setDoOutput(true);
  68. return connection;
  69. }
  70.  
  71. private static UUID getUUID(String id) {
  72. return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" +id.substring(20, 32));
  73. }
  74.  
  75. public static byte[] toBytes(UUID uuid) {
  76. ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
  77. byteBuffer.putLong(uuid.getMostSignificantBits());
  78. byteBuffer.putLong(uuid.getLeastSignificantBits());
  79. return byteBuffer.array();
  80. }
  81.  
  82. public static UUID fromBytes(byte[] array) {
  83. if (array.length != 16) {
  84. throw new IllegalArgumentException("Illegal byte array length: " + array.length);
  85. }
  86. ByteBuffer byteBuffer = ByteBuffer.wrap(array);
  87. long mostSignificant = byteBuffer.getLong();
  88. long leastSignificant = byteBuffer.getLong();
  89. return new UUID(mostSignificant, leastSignificant);
  90. }
  91.  
  92. public static UUID getUUIDOf(String name) throws Exception {
  93. return new UUIDFetcher(Arrays.asList(name)).call().get(name);
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement