Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.xplore.dungeonapi.utility;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- /**
- * Fetche die UUID eines Spieler anhand seines Usernamen von den Mojang-Servern
- * @author Yannik
- *
- */
- public class UUIDFetcher {
- private static Map<String, String> uuidCache;
- static {
- uuidCache = new HashMap<String, String>();
- }
- /**
- * Bekomme die UUID eines Spielers anhand seines Usernamen
- * @param username Name des Spielers
- * @return UUID des Spielers (ohne Bindestriche)
- */
- public static String getUUID(String username) {
- if (uuidCache.containsKey(username)) return uuidCache.get(username);
- try {
- URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + username);
- InputStream stream = url.openStream();
- InputStreamReader inr = new InputStreamReader(stream);
- BufferedReader reader = new BufferedReader(inr);
- String s = null;
- StringBuilder sb = new StringBuilder();
- while ((s = reader.readLine()) != null) {
- sb.append(s);
- }
- String result = sb.toString();
- JsonElement element = new JsonParser().parse(result);
- if(element == null){
- return null;
- }
- JsonObject obj = element.getAsJsonObject();
- String uuid = obj.get("id").toString();
- uuid = uuid.substring(1);
- uuid = uuid.substring(0, uuid.length() - 1);
- uuidCache.put(username, uuid);
- return uuid;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement