/** * Copyright © 2013 tuxed * 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; import com.google.common.collect.ImmutableList; import com.google.gson.reflect.TypeToken; import com.imaginarycode.minecraft.redisbungee.RedisBungee; import java.io.InputStreamReader; import java.net.HttpURLConnection; 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; /* Credits to evilmidget38 for this class. I modified it to use Gson. */ public class NameFetcher implements Callable> { private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/"; private final List uuids; 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); } return uuidStringMap; } }