From 810ce8adb9748de5d3c9542146363196f9c78b9f Mon Sep 17 00:00:00 2001 From: "Ansgar [Hiajen]" Date: Sun, 6 Jun 2021 17:37:36 +0200 Subject: [PATCH] get current player from UUID --- build.gradle | 26 +------------------ .../net/saltymc/eaa/commands/TagCommand.java | 2 +- .../eaa/util/mojangApi/PlayerInfo.java | 24 +++++++++++------ 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/build.gradle b/build.gradle index 9a377df..cf67376 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,5 @@ plugins { id 'fabric-loom' version '0.7-SNAPSHOT' - id 'maven-publish' } sourceCompatibility = JavaVersion.VERSION_1_8 @@ -66,27 +65,4 @@ jar { from("LICENSE") { rename { "${it}_${project.archivesBaseName}"} } -} - -// configure the maven publication -publishing { - publications { - mavenJava(MavenPublication) { - // add all the jars that should be included when publishing to maven - artifact(remapJar) { - builtBy remapJar - } - artifact(sourcesJar) { - builtBy remapSourcesJar - } - } - } - - // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. - repositories { - // Add repositories to publish to here. - // Notice: This block does NOT have the same function as the block in the top level. - // The repositories here will be used for publishing your artifact, not for - // retrieving dependencies. - } -} +} \ No newline at end of file diff --git a/src/main/java/net/saltymc/eaa/commands/TagCommand.java b/src/main/java/net/saltymc/eaa/commands/TagCommand.java index df28442..6cc7c01 100644 --- a/src/main/java/net/saltymc/eaa/commands/TagCommand.java +++ b/src/main/java/net/saltymc/eaa/commands/TagCommand.java @@ -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) { diff --git a/src/main/java/net/saltymc/eaa/util/mojangApi/PlayerInfo.java b/src/main/java/net/saltymc/eaa/util/mojangApi/PlayerInfo.java index 1f89a49..d8b8eb0 100644 --- a/src/main/java/net/saltymc/eaa/util/mojangApi/PlayerInfo.java +++ b/src/main/java/net/saltymc/eaa/util/mojangApi/PlayerInfo.java @@ -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 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 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 playernames = playerUUIDToNames(playerUUID); + + return playernames.get(playernames.size() - 1); + } }