mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-11-23 04:28:01 +00:00
vemacs broke the build
This commit is contained in:
parent
e13a8de1d7
commit
250a6e0887
@ -6,35 +6,46 @@
|
|||||||
*/
|
*/
|
||||||
package com.imaginarycode.minecraft.redisbungee.util;
|
package com.imaginarycode.minecraft.redisbungee.util;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Type;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.util.HashMap;
|
||||||
import java.util.*;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
public class NameFetcher {
|
/* Credits to evilmidget38 for this class. I modified it to use Gson. */
|
||||||
private static Map<UUID, List<String>> cache = new HashMap<>();
|
class NameFetcher implements Callable<Map<UUID, String>> {
|
||||||
|
private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/";
|
||||||
|
private final List<UUID> uuids;
|
||||||
|
|
||||||
public static List<String> nameHistoryFromUuid(UUID uuid) {
|
public NameFetcher(List<UUID> uuids) {
|
||||||
if (cache.containsKey(uuid)) return cache.get(uuid);
|
this.uuids = ImmutableList.copyOf(uuids);
|
||||||
URLConnection connection;
|
|
||||||
try {
|
|
||||||
connection = new URL("https://api.mojang.com/user/profiles/"
|
|
||||||
+ uuid.toString().replace("-", "").toLowerCase() + "/names"
|
|
||||||
).openConnection();
|
|
||||||
String text = new Scanner(connection.getInputStream()).useDelimiter("\\Z").next();
|
|
||||||
Type listType = new TypeToken<List<String>>() {
|
|
||||||
}.getType();
|
|
||||||
List<String> list = new Gson().fromJson(text, listType);
|
|
||||||
cache.put(uuid, list);
|
|
||||||
return list;
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
@Override
|
||||||
|
public Map<UUID, String> call() throws Exception {
|
||||||
|
Map<UUID, String> uuidStringMap = new HashMap<>();
|
||||||
|
for (UUID uuid : uuids) {
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL + uuid.toString().replace("-", "")).openConnection();
|
||||||
|
Map<String, String> response = RedisBungee.getGson().fromJson(new InputStreamReader(connection.getInputStream()), new TypeToken<Map<String, String>>() {
|
||||||
|
}.getType());
|
||||||
|
String name = response.get("name");
|
||||||
|
if (name == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String cause = response.get("cause");
|
||||||
|
String errorMessage = response.get("errorMessage");
|
||||||
|
if (cause != null && cause.length() > 0) {
|
||||||
|
throw new IllegalStateException(errorMessage);
|
||||||
|
}
|
||||||
|
uuidStringMap.put(uuid, name);
|
||||||
|
}
|
||||||
|
return uuidStringMap;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user