package eu.univento.commons.player.uuid; import eu.univento.commons.Commons; import eu.univento.commons.server.ServerType; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.UUID; /** * @author codebucketdev */ public class NameFetcher { private static final JSONParser jsonParser = new JSONParser(); private static final String REQUEST_URL = "https://mcapi.de/api/user/"; private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/"; public static String getRequest(UUID uuid) { try { HttpURLConnection connection = (HttpURLConnection) new URL(REQUEST_URL + fromUniqueId(uuid)).openConnection(); JSONObject response = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream())); String uniqueId = (String) response.get("uuid"); if (uniqueId.length() == 0) Commons.getCommons().getLoggingHandler().log(ServerType.getServerType() ,"A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?"); return (String) response.get("username"); } catch (IOException |ParseException e) { return callMojang(uuid); } } private static String callMojang(UUID uuid) { try { HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL + fromUniqueId(uuid)).openConnection(); JSONObject response = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream())); String name = (String) response.get("name"); if (name == null) Commons.getCommons().getLoggingHandler().log(ServerType.getServerType(),"A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?"); String cause = (String) response.get("cause"); String errorMessage = (String) response.get("errorMessage"); if (cause != null && cause.length() > 0) { Commons.getCommons().getLoggingHandler().log(ServerType.getServerType(), errorMessage); } return name; } catch (IOException | ParseException e) { e.printStackTrace(); Commons.getCommons().getLoggingHandler().log(ServerType.getServerType(), "A Username for UUID '" + uuid.toString() + "' was not found in the database! Is the account not premium?"); } return null; } private static String fromUniqueId(UUID uuid) { return uuid.toString().replaceAll("-", ""); } }