Skip to content

Instantly share code, notes, and snippets.

@Copystrike
Created July 6, 2018 14:14
Show Gist options
  • Save Copystrike/f7ba786cd4ed0ce5becef7ad0e8edf67 to your computer and use it in GitHub Desktop.
Save Copystrike/f7ba786cd4ed0ce5becef7ad0e8edf67 to your computer and use it in GitHub Desktop.
package me.ikeetjeop.staffonlineultimate.utils;
import com.avaje.ebean.text.json.JsonElementArray;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.bukkit.Bukkit;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
/**
* Created by @Ikeetjeop aka Nick on 10-May-18.
*/
public class NameFetcher {
private static Map<UUID, String> cache = new HashMap<>();
public static String getNameOf(UUID uuid) {
if (cache.containsKey(uuid)) {
return cache.get(uuid);
}
URL url;
HttpURLConnection connection;
try {
url = new URL("https://mc-heads.net/minecraft/profile/" + uuid.toString().replace("-", "").toLowerCase());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if(connection.getResponseCode() == 429){
return null;
}
String text = new Scanner(connection.getInputStream()).useDelimiter("\\Z").next();
JsonObject jobj = new Gson().fromJson(text, JsonObject.class);
cache.put(uuid, jobj.get("name").getAsString());
return jobj.get("name").getAsString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void invalidateCache() {
cache.clear();
}
public static Map<UUID, String> getCache() {
return cache;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment