aboutsummaryrefslogtreecommitdiff
path: root/me/Danker/handlers/APIHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'me/Danker/handlers/APIHandler.java')
-rw-r--r--me/Danker/handlers/APIHandler.java54
1 files changed, 52 insertions, 2 deletions
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;
+ }
}