RedisBungee/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java

87 lines
3.6 KiB
Java
Raw Normal View History

/**
2015-06-09 23:37:01 +00:00
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
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;
import java.util.*;
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>> {
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");
private final List<String> names;
private final boolean rateLimiting;
2015-02-10 15:04:03 +00:00
@Setter
private static OkHttpClient httpClient;
public UUIDFetcher(List<String> names, boolean rateLimiting) {
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) {
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-09-21 17:56:46 +00:00
return uuidMap;
}
2015-01-25 05:04:34 +00:00
private static class Profile {
String id;
String name;
}
}