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

@ -1,6 +1,5 @@
plugins { plugins {
id 'fabric-loom' version '0.7-SNAPSHOT' id 'fabric-loom' version '0.7-SNAPSHOT'
id 'maven-publish'
} }
sourceCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = JavaVersion.VERSION_1_8
@ -66,27 +65,4 @@ jar {
from("LICENSE") { from("LICENSE") {
rename { "${it}_${project.archivesBaseName}"} 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.
}
}

View file

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

View file

@ -1,6 +1,6 @@
package net.saltymc.eaa.util.mojangApi; 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.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import org.apache.http.HttpResponse; 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.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.util.UUID; import java.util.ArrayList;
import java.util.List;
public class PlayerInfo { public class PlayerInfo {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
public static String playerNameToUUID(String playerName) throws MojangUtilException { public static String playerNameToUUID(String playerName) throws MojangUtilException {
HttpClient httpClient = HttpClientBuilder.create().build(); 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 { try {
HttpClient httpClient = HttpClientBuilder.create().build(); HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://api.mojang.com/user/profiles/" + playerUUID.toString().replaceAll("-", "") + "/names"); HttpGet request = new HttpGet("https://api.mojang.com/user/profiles/" + playerUUID.toString().replaceAll("-", "") + "/names");
HttpResponse response = httpClient.execute(request); HttpResponse response = httpClient.execute(request);
List<String> ret = new ArrayList<>();
// take the response body as a json formatted string // 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); LOGGER.error("Could not parse UUID to username!", e);
throw new MojangUtilException("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);
}
} }