Guest User

UUIDFetcher

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