From 705150f7ade74e87c71cd8dbb48aac89979c4471 Mon Sep 17 00:00:00 2001 From: "Ansgar [Hiajen]" Date: Sat, 29 May 2021 16:52:18 +0200 Subject: [PATCH] implement username to UUID tp username --- gradle.properties | 2 +- .../net/saltymc/eaa/commands/TagCommand.java | 21 +++--- .../util/mojangApi/MojangUtilException.java | 12 ++++ .../eaa/util/mojangApi/PlayerInfo.java | 65 +++++++++++++++++++ 4 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/saltymc/eaa/util/mojangApi/MojangUtilException.java create mode 100644 src/main/java/net/saltymc/eaa/util/mojangApi/PlayerInfo.java diff --git a/gradle.properties b/gradle.properties index b33028b..988331a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,4 +14,4 @@ org.gradle.jvmargs=-Xmx1G # Dependencies # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api (or https://fabricmc.net/versions.html) - fabric_version=0.34.2+1.16 + fabric_version=0.34.2+1.16 \ 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 6904330..df28442 100644 --- a/src/main/java/net/saltymc/eaa/commands/TagCommand.java +++ b/src/main/java/net/saltymc/eaa/commands/TagCommand.java @@ -1,23 +1,15 @@ package net.saltymc.eaa.commands; -import com.google.common.collect.Iterables; -import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.suggestion.Suggestions; import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; import net.minecraft.client.toast.SystemToast; -import net.minecraft.command.CommandSource; -import net.minecraft.command.EntitySelectorReader; import net.minecraft.command.argument.EntityArgumentType; -import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.LiteralText; import net.minecraft.text.Text; -import net.minecraft.util.Formatting; -import java.util.Collection; +import net.saltymc.eaa.util.mojangApi.PlayerInfo; import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.argument; import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.literal; @@ -32,8 +24,17 @@ public class TagCommand extends EaaModCommand{ SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.TUTORIAL_HINT, Text.of(tag), Text.of(player)); + Text text = null; + try { + String playerUUID = PlayerInfo.playerNameToUUID(player); + String playerName = PlayerInfo.playerUUIDToName(playerUUID); - + text = Text.of("You tagged player " + playerName + " : " + playerUUID); + } catch (Exception e) { + text = Text.of(e.toString()); + } + source.sendFeedback(text); + return 1; } diff --git a/src/main/java/net/saltymc/eaa/util/mojangApi/MojangUtilException.java b/src/main/java/net/saltymc/eaa/util/mojangApi/MojangUtilException.java new file mode 100644 index 0000000..7e55c61 --- /dev/null +++ b/src/main/java/net/saltymc/eaa/util/mojangApi/MojangUtilException.java @@ -0,0 +1,12 @@ +package net.saltymc.eaa.util.mojangApi; + +public class MojangUtilException extends Exception { + + MojangUtilException(String message){ + super(message); + } + + MojangUtilException(String message, Throwable ex){ + super(message, ex); + } +} diff --git a/src/main/java/net/saltymc/eaa/util/mojangApi/PlayerInfo.java b/src/main/java/net/saltymc/eaa/util/mojangApi/PlayerInfo.java new file mode 100644 index 0000000..1f89a49 --- /dev/null +++ b/src/main/java/net/saltymc/eaa/util/mojangApi/PlayerInfo.java @@ -0,0 +1,65 @@ +package net.saltymc.eaa.util.mojangApi; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +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 java.util.UUID; + + +public class PlayerInfo { + + private static final Logger LOGGER = LogManager.getLogger(); + + + public static String playerNameToUUID(String playerName) throws MojangUtilException { + + HttpClient httpClient = HttpClientBuilder.create().build(); + try { + HttpPost request = new HttpPost("https://api.mojang.com/profiles/minecraft"); + StringEntity params = new StringEntity("[\"" + playerName + "\"]"); + request.addHeader("Content-Type", "application/json"); + request.setEntity(params); + HttpResponse response = httpClient.execute(request); + + // take the response body as a json formatted string + JsonObject responseJSON = new JsonParser().parse(EntityUtils.toString(response.getEntity())).getAsJsonArray().get(0).getAsJsonObject(); + + //doublecheck correct name + if (!responseJSON.get("name").getAsString().equalsIgnoreCase(playerName)) + throw new Exception("Api response provided wrong player!"); + + return responseJSON.get("id").getAsString(); + + } 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 String playerUUIDToName(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); + + // take the response body as a json formatted string + JsonObject responseJSON = new JsonParser().parse(EntityUtils.toString(response.getEntity())).getAsJsonArray().get(0).getAsJsonObject(); + + + return responseJSON.get("name").getAsString(); + } catch (Exception e){ + LOGGER.error("Could not parse UUID to username!", e); + throw new MojangUtilException("Could not parse UUID to username!", e); + } + } +}