From 1b6cdee6ed6503b9b3dad740f9efa1e60d87600b Mon Sep 17 00:00:00 2001 From: bowser0000 Date: Sun, 19 Jul 2020 17:58:35 -0400 Subject: Move requests into API handler for future API commands --- me/Danker/handlers/APIHandler.java | 54 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'me/Danker/handlers/APIHandler.java') diff --git a/me/Danker/handlers/APIHandler.java b/me/Danker/handlers/APIHandler.java index 5997713..94868a2 100644 --- a/me/Danker/handlers/APIHandler.java +++ b/me/Danker/handlers/APIHandler.java @@ -8,14 +8,19 @@ import java.net.MalformedURLException; import java.net.URL; import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; public class APIHandler { - public static JsonObject getResponse(String urlString, EntityPlayer player) { + public static JsonObject getResponse(String urlString) { + EntityPlayer player = Minecraft.getMinecraft().thePlayer; + try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); @@ -36,7 +41,7 @@ public class APIHandler { return object; } else { - player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Request failed. Incorrect arguments?")); + player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Request failed. HTTP Error Code: " + conn.getResponseCode())); } } catch (MalformedURLException ex) { player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "An error has occured. See logs for more details.")); @@ -48,4 +53,49 @@ public class APIHandler { return new JsonObject(); } + + public static String getUUID(String username) { + Gson gson = new Gson(); + + JsonObject uuidResponse = getResponse("https://api.mojang.com/users/profiles/minecraft/" + username); + String UUID = uuidResponse.get("id").getAsString(); + return UUID; + } + + public static String getLatestProfileID(String UUID, String key) { + Gson gson = new Gson(); + EntityPlayer player = Minecraft.getMinecraft().thePlayer; + + // Get profiles + System.out.println("Fetching profiles..."); + + JsonObject profilesResponse = getResponse("https://api.hypixel.net/skyblock/profiles?uuid=" + UUID + "&key=" + key); + if (!profilesResponse.get("success").getAsBoolean()) { + String reason = profilesResponse.get("cause").getAsString(); + player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Failed with reason: " + reason)); + return null; + } + if (profilesResponse.get("profiles").isJsonNull()) { + player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "This player doesn't appear to have played SkyBlock.")); + return null; + } + + // Loop through profiles to find latest + System.out.println("Looping through profiles..."); + String latestProfile = ""; + int latestSave = 0; + JsonArray profilesArray = profilesResponse.get("profiles").getAsJsonArray(); + + for (JsonElement profile : profilesArray) { + JsonObject profileJSON = profile.getAsJsonObject(); + int profileLastSave = profileJSON.get("members").getAsJsonObject().get(UUID).getAsJsonObject().get("last_save").getAsInt(); + + if (profileLastSave > latestSave) { + latestProfile = profileJSON.get("profile_id").getAsString(); + latestSave = profileLastSave; + } + } + + return latestProfile; + } } -- cgit