2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-23 04:28:01 +00:00

Make NameFetcher less hazardous to ones health.

This commit is contained in:
Tux 2015-01-25 00:02:30 -05:00
parent 20ef1ee284
commit f201cdc4e8

View File

@ -6,37 +6,43 @@
*/ */
package com.imaginarycode.minecraft.redisbungee.util; package com.imaginarycode.minecraft.redisbungee.util;
import com.google.gson.JsonArray; import com.google.common.io.ByteStreams;
import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken;
import com.google.gson.JsonParser; import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner;
import java.util.UUID; import java.util.UUID;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NameFetcher { public class NameFetcher {
private static JsonParser parser = new JsonParser(); 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();
public static List<String> nameHistoryFromUuid(UUID uuid) { String text;
URLConnection connection;
try { try (InputStream is = connection.getInputStream()) {
connection = new URL("https://api.mojang.com/user/profiles/" text = new String(ByteStreams.toByteArray(is));
+ 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) { Type listType = new TypeToken<List<Name>>() {}.getType();
e.printStackTrace(); List<Name> names = RedisBungee.getGson().fromJson(text, listType);
List<String> humanNames = new ArrayList<>();
for (Name name : names) {
humanNames.add(name.name);
} }
return null; return humanNames;
}
public static class Name {
private String name;
} }
} }