Merge remote-tracking branch 'origin/master'

Conflicts:
	src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java
This commit is contained in:
Tux 2014-12-27 18:28:09 -05:00
commit 3c7c9c616a
2 changed files with 22 additions and 37 deletions

View File

@ -6,46 +6,31 @@
*/ */
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.InputStreamReader; import java.io.IOException;
import java.net.HttpURLConnection; import java.lang.reflect.Type;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.net.URLConnection;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
/* Credits to evilmidget38 for this class. I modified it to use Gson. */ public class NameFetcher {
class NameFetcher implements Callable<Map<UUID, String>> { public static List<String> nameHistoryFromUuid(UUID uuid) {
private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/"; URLConnection connection;
private final List<UUID> uuids; try {
connection = new URL("https://api.mojang.com/user/profiles/"
public NameFetcher(List<UUID> uuids) { + uuid.toString().replace("-", "").toLowerCase() + "/names"
this.uuids = ImmutableList.copyOf(uuids); ).openConnection();
} String text = new Scanner(connection.getInputStream()).useDelimiter("\\Z").next();
Type listType = new TypeToken<List<String>>() {
@Override }.getType();
public Map<UUID, String> call() throws Exception { List<String> list = new Gson().fromJson(text, listType);
Map<UUID, String> uuidStringMap = new HashMap<>(); return list;
for (UUID uuid : uuids) { } catch (IOException e) {
HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL + uuid.toString().replace("-", "")).openConnection(); e.printStackTrace();
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; return null;
} }
} }

View File

@ -162,7 +162,7 @@ public final class UUIDTranslator {
// That didn't work. Let's ask Mojang. This call may fail, because Mojang is insane. // That didn't work. Let's ask Mojang. This call may fail, because Mojang is insane.
String name; String name;
try { try {
name = new NameFetcher(Collections.singletonList(player)).call().get(player); name = NameFetcher.nameHistoryFromUuid(player).get(0);
} catch (Exception e) { } catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, "Unable to fetch name from Mojang for " + player, e); plugin.getLogger().log(Level.SEVERE, "Unable to fetch name from Mojang for " + player, e);
return null; return null;