From e13a8de1d7242f9da5e50a1622a8af809550b333 Mon Sep 17 00:00:00 2001 From: vemacs Date: Sat, 27 Dec 2014 14:00:09 -0700 Subject: [PATCH] Possibly fix namefetcher --- .../redisbungee/util/NameFetcher.java | 59 ++++++++----------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java index 7fb9870..f58cdc9 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java @@ -6,46 +6,35 @@ */ 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.imaginarycode.minecraft.redisbungee.RedisBungee; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; +import java.io.IOException; +import java.lang.reflect.Type; import java.net.URL; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.Callable; +import java.net.URLConnection; +import java.util.*; -/* Credits to evilmidget38 for this class. I modified it to use Gson. */ -class NameFetcher implements Callable> { - private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/"; - private final List uuids; +public class NameFetcher { + private static Map> cache = new HashMap<>(); - public NameFetcher(List uuids) { - this.uuids = ImmutableList.copyOf(uuids); - } - - @Override - public Map call() throws Exception { - Map uuidStringMap = new HashMap<>(); - for (UUID uuid : uuids) { - HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL + uuid.toString().replace("-", "")).openConnection(); - Map response = RedisBungee.getGson().fromJson(new InputStreamReader(connection.getInputStream()), new TypeToken>() { - }.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); + public static List nameHistoryFromUuid(UUID uuid) { + if (cache.containsKey(uuid)) return cache.get(uuid); + 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>() { + }.getType(); + List list = new Gson().fromJson(text, listType); + cache.put(uuid, list); + return list; + } catch (IOException e) { + e.printStackTrace(); } - return uuidStringMap; + return null; } } \ No newline at end of file