Commons/src/main/java/eu/univento/commons/player/uuid/NameFetcher.java

65 lines
2.7 KiB
Java
Raw Normal View History

2016-08-02 23:53:40 +02:00
package eu.univento.commons.player.uuid;
2016-12-06 19:17:13 +01:00
import eu.univento.commons.Commons;
import eu.univento.commons.server.ServerType;
2016-08-02 23:53:40 +02:00
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
2016-12-06 19:17:13 +01:00
import org.json.simple.parser.ParseException;
2016-08-02 23:53:40 +02:00
2016-12-06 19:17:13 +01:00
import java.io.IOException;
2016-08-02 23:53:40 +02:00
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");
2016-12-06 19:17:13 +01:00
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?");
2016-08-02 23:53:40 +02:00
return (String) response.get("username");
2016-12-06 19:17:13 +01:00
} catch (IOException |ParseException e) {
2016-08-02 23:53:40 +02:00
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");
2016-12-06 19:17:13 +01:00
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?");
2016-08-02 23:53:40 +02:00
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);
2016-08-02 23:53:40 +02:00
}
return name;
2016-12-06 19:17:13 +01:00
} 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?");
2016-08-02 23:53:40 +02:00
}
2016-12-06 19:17:13 +01:00
return null;
2016-08-02 23:53:40 +02:00
}
private static String fromUniqueId(UUID uuid) {
return uuid.toString().replaceAll("-", "");
}
}