implement username to UUID tp username

This commit is contained in:
Hiajen Hiajen 2021-05-29 16:52:18 +02:00
parent b3e5a3fe4d
commit 705150f7ad
4 changed files with 89 additions and 11 deletions

View file

@ -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,7 +24,16 @@ 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;
}

View file

@ -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);
}
}

View file

@ -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);
}
}
}