2014-04-20 05:12:28 +00:00
|
|
|
/**
|
|
|
|
* Copyright © 2013 tuxed <write@imaginarycode.com>
|
|
|
|
* This work is free. You can redistribute it and/or modify it under the
|
|
|
|
* terms of the Do What The Fuck You Want To Public License, Version 2,
|
|
|
|
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
|
|
|
|
*/
|
|
|
|
package com.imaginarycode.minecraft.redisbungee.util;
|
|
|
|
|
2015-01-08 01:28:28 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2014-04-20 05:12:28 +00:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
2014-12-27 23:30:26 +00:00
|
|
|
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
|
2014-04-20 05:12:28 +00:00
|
|
|
|
2015-01-08 01:28:28 +00:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
2014-04-20 05:12:28 +00:00
|
|
|
import java.net.URL;
|
2015-01-08 01:28:28 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.Callable;
|
2014-04-20 05:12:28 +00:00
|
|
|
|
2015-01-08 01:28:28 +00:00
|
|
|
/* Credits to evilmidget38 for this class. I modified it to use Gson. */
|
|
|
|
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 NameFetcher(List<UUID> uuids) {
|
|
|
|
this.uuids = ImmutableList.copyOf(uuids);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
2014-04-20 05:12:28 +00:00
|
|
|
}
|
|
|
|
}
|