95 lines
3.4 KiB
Java
95 lines
3.4 KiB
Java
package net.saltymc.eaa.util.mojangApi;
|
|
|
|
import net.minecraft.client.MinecraftClient;
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.client.HttpClient;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONObject;
|
|
import org.json.simple.parser.JSONParser;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class PlayerInfo {
|
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
|
|
@Nullable
|
|
public static String playerNameToUUID(String playerName) {
|
|
String playerUUID = null;
|
|
|
|
try {
|
|
playerUUID = MinecraftClient.getInstance().getNetworkHandler().getPlayerListEntry(playerName).getProfile().getId().toString();
|
|
} catch (NullPointerException e) {
|
|
try {
|
|
playerUUID = playerNameToUUIDbyAPI(playerName);
|
|
} catch (MojangUtilException ex){
|
|
//TODO:
|
|
}
|
|
|
|
}
|
|
|
|
return playerUUID;
|
|
}
|
|
|
|
@Nullable
|
|
private static String playerNameToUUIDbyAPI(String playerName) throws MojangUtilException {
|
|
|
|
try {
|
|
HttpClient httpClient = HttpClientBuilder.create().build();
|
|
HttpGet request = new HttpGet("https://api.mojang.com/users/profiles/minecraft/" + playerName);
|
|
|
|
HttpResponse response = httpClient.execute(request);
|
|
|
|
if (response.getStatusLine().getStatusCode() != 200)
|
|
return null;
|
|
|
|
// take the response body as a json formatted string
|
|
JSONObject responseJSON = (JSONObject) new JSONParser().parse(EntityUtils.toString(response.getEntity()));
|
|
//doublecheck correct name
|
|
if (responseJSON.containsKey("name") && ! ((String)responseJSON.get("name")).equalsIgnoreCase(playerName))
|
|
return null;
|
|
|
|
return (String) responseJSON.get("id");
|
|
|
|
} catch (Exception ex) {
|
|
LOGGER.error("Error parsing playername to UUID while contacting API: ", ex);
|
|
throw new MojangUtilException("Could not parse Username to UUID!", ex);
|
|
}
|
|
}
|
|
|
|
public static List<String> playerUUIDToNames(String playerUUID) throws MojangUtilException {
|
|
try {
|
|
HttpClient httpClient = HttpClientBuilder.create().build();
|
|
HttpGet request = new HttpGet("https://api.mojang.com/user/profiles/" + playerUUID.replaceAll("-", "") + "/names");
|
|
HttpResponse response = httpClient.execute(request);
|
|
|
|
List<String> ret = new ArrayList<>();
|
|
|
|
// take the response body as a json formatted string
|
|
for (Object responseJSON : (JSONArray) new JSONParser().parse(EntityUtils.toString(response.getEntity()))) {
|
|
ret.add((String) ((JSONObject)responseJSON).get("name"));
|
|
}
|
|
|
|
return ret;
|
|
|
|
} catch (Exception e) {
|
|
LOGGER.error("Could not parse UUID to username!", e);
|
|
throw new MojangUtilException("Could not parse UUID to username!", e);
|
|
}
|
|
}
|
|
|
|
public static String playerUUIDToCurrentName(String playerUUID) throws MojangUtilException {
|
|
List<String> playernames = playerUUIDToNames(playerUUID);
|
|
|
|
return playernames.get(playernames.size() - 1);
|
|
}
|
|
}
|