2014-04-20 05:12:28 +00:00
|
|
|
package com.imaginarycode.minecraft.redisbungee.util;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
|
2015-01-25 05:17:52 +00:00
|
|
|
import com.squareup.okhttp.MediaType;
|
2015-02-10 15:04:03 +00:00
|
|
|
import com.squareup.okhttp.OkHttpClient;
|
2015-01-25 05:17:52 +00:00
|
|
|
import com.squareup.okhttp.Request;
|
|
|
|
import com.squareup.okhttp.RequestBody;
|
2015-02-10 15:04:03 +00:00
|
|
|
import lombok.Setter;
|
2014-04-20 05:12:28 +00:00
|
|
|
|
2015-06-24 09:51:40 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
2014-04-20 05:12:28 +00:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
/* Credits to evilmidget38 for this class. I modified it to use Gson. */
|
2015-02-04 15:02:30 +00:00
|
|
|
public class UUIDFetcher implements Callable<Map<String, UUID>> {
|
2014-04-20 05:12:28 +00:00
|
|
|
private static final double PROFILES_PER_REQUEST = 100;
|
|
|
|
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
|
2015-01-25 05:17:52 +00:00
|
|
|
private static final MediaType JSON = MediaType.parse("application/json");
|
2014-04-20 05:12:28 +00:00
|
|
|
private final List<String> names;
|
|
|
|
private final boolean rateLimiting;
|
|
|
|
|
2015-02-10 15:04:03 +00:00
|
|
|
@Setter
|
|
|
|
private static OkHttpClient httpClient;
|
|
|
|
|
2015-06-24 10:06:34 +00:00
|
|
|
private UUIDFetcher(List<String> names, boolean rateLimiting) {
|
2014-04-20 05:12:28 +00:00
|
|
|
this.names = ImmutableList.copyOf(names);
|
|
|
|
this.rateLimiting = rateLimiting;
|
|
|
|
}
|
|
|
|
|
|
|
|
public UUIDFetcher(List<String> names) {
|
|
|
|
this(names, true);
|
|
|
|
}
|
|
|
|
|
2014-05-23 14:53:38 +00:00
|
|
|
public static UUID getUUID(String id) {
|
2014-04-20 05:12:28 +00:00
|
|
|
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:56:46 +00:00
|
|
|
public Map<String, UUID> call() throws Exception {
|
|
|
|
Map<String, UUID> uuidMap = new HashMap<>();
|
|
|
|
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
|
|
|
|
for (int i = 0; i < requests; i++) {
|
|
|
|
String body = RedisBungee.getGson().toJson(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
|
2015-01-25 05:17:52 +00:00
|
|
|
Request request = new Request.Builder().url(PROFILE_URL).post(RequestBody.create(JSON, body)).build();
|
2015-02-10 15:04:03 +00:00
|
|
|
String response = httpClient.newCall(request).execute().body().string();
|
2015-01-25 05:17:52 +00:00
|
|
|
Profile[] array = RedisBungee.getGson().fromJson(response, Profile[].class);
|
2014-09-21 17:56:46 +00:00
|
|
|
for (Profile profile : array) {
|
|
|
|
UUID uuid = UUIDFetcher.getUUID(profile.id);
|
|
|
|
uuidMap.put(profile.name, uuid);
|
|
|
|
}
|
|
|
|
if (rateLimiting && i != requests - 1) {
|
|
|
|
Thread.sleep(100L);
|
|
|
|
}
|
2014-04-20 05:12:28 +00:00
|
|
|
}
|
2014-09-21 17:56:46 +00:00
|
|
|
return uuidMap;
|
2014-04-20 05:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 05:04:34 +00:00
|
|
|
private static class Profile {
|
2014-04-20 05:12:28 +00:00
|
|
|
String id;
|
|
|
|
String name;
|
|
|
|
}
|
|
|
|
}
|