get current player from UUID

This commit is contained in:
Hiajen Hiajen 2021-06-06 17:37:36 +02:00
parent 705150f7ad
commit 810ce8adb9
3 changed files with 18 additions and 34 deletions

View file

@ -27,7 +27,7 @@ public class TagCommand extends EaaModCommand{
Text text = null;
try {
String playerUUID = PlayerInfo.playerNameToUUID(player);
String playerName = PlayerInfo.playerUUIDToName(playerUUID);
String playerName = PlayerInfo.playerUUIDToCurrentName(playerUUID);
text = Text.of("You tagged player " + playerName + " : " + playerUUID);
} catch (Exception e) {

View file

@ -1,6 +1,6 @@
package net.saltymc.eaa.util.mojangApi;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpResponse;
@ -13,14 +13,13 @@ import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
public class PlayerInfo {
private static final Logger LOGGER = LogManager.getLogger();
public static String playerNameToUUID(String playerName) throws MojangUtilException {
HttpClient httpClient = HttpClientBuilder.create().build();
@ -46,20 +45,29 @@ public class PlayerInfo {
}
}
public static String playerUUIDToName(String playerUUID) throws MojangUtilException{
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.toString().replaceAll("-", "") + "/names");
HttpResponse response = httpClient.execute(request);
List<String> ret = new ArrayList<>();
// take the response body as a json formatted string
JsonObject responseJSON = new JsonParser().parse(EntityUtils.toString(response.getEntity())).getAsJsonArray().get(0).getAsJsonObject();
for (JsonElement responseJSON : new JsonParser().parse(EntityUtils.toString(response.getEntity())).getAsJsonArray())
ret.add(responseJSON.getAsJsonObject().get("name").getAsString());
return ret;
return responseJSON.get("name").getAsString();
} catch (Exception e){
} 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);
}
}