Use new-new method with a better ratelimit

This commit is contained in:
vemacs 2015-01-09 07:45:45 -07:00
parent 6511e2154f
commit 20ef1ee284
1 changed files with 26 additions and 10 deletions

View File

@ -6,21 +6,37 @@
*/
package com.imaginarycode.minecraft.redisbungee.util;
import com.google.common.io.ByteStreams;
import com.google.gson.reflect.TypeToken;
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;
public class NameFetcher {
public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException {
URLConnection connection = new URL("https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "").toLowerCase() + "/names").openConnection();
String text = new String(ByteStreams.toByteArray(connection.getInputStream()));
Type listType = new TypeToken<List<String>>() {}.getType();
return RedisBungee.getGson().fromJson(text, listType);
private static JsonParser parser = new JsonParser();
public static List<String> nameHistoryFromUuid(UUID 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();
JsonArray list = (JsonArray) parser.parse(text);
List<String> names = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
names.add(((JsonObject) list.get(i)).get("name").getAsString());
}
return names;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}