diff options
author | bowser0000 <bowser0000@gmail.com> | 2020-07-19 17:58:35 -0400 |
---|---|---|
committer | bowser0000 <bowser0000@gmail.com> | 2020-07-19 17:58:35 -0400 |
commit | 1b6cdee6ed6503b9b3dad740f9efa1e60d87600b (patch) | |
tree | ec4b4c0b4d8f306022546eede2d319be62c76e70 | |
parent | d1f260be47ea166e77b2e25cd07d6b08ec3e767d (diff) | |
download | SkyblockMod-1b6cdee6ed6503b9b3dad740f9efa1e60d87600b.tar.gz SkyblockMod-1b6cdee6ed6503b9b3dad740f9efa1e60d87600b.tar.bz2 SkyblockMod-1b6cdee6ed6503b9b3dad740f9efa1e60d87600b.zip |
Move requests into API handler for future API commands
-rw-r--r-- | me/Danker/TheMod.java | 2 | ||||
-rw-r--r-- | me/Danker/commands/SlayerCommand.java | 44 | ||||
-rw-r--r-- | me/Danker/handlers/APIHandler.java | 54 |
3 files changed, 60 insertions, 40 deletions
diff --git a/me/Danker/TheMod.java b/me/Danker/TheMod.java index 5fdaeaa..76e367e 100644 --- a/me/Danker/TheMod.java +++ b/me/Danker/TheMod.java @@ -41,7 +41,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; public class TheMod { public static final String MODID = "Danker's Skyblock Mod"; - public static final String VERSION = "1.4.5"; + public static final String VERSION = "1.5.1"; static int checkItemsNow = 0; static int itemsChecked = 0; diff --git a/me/Danker/commands/SlayerCommand.java b/me/Danker/commands/SlayerCommand.java index ec23606..9fae3ef 100644 --- a/me/Danker/commands/SlayerCommand.java +++ b/me/Danker/commands/SlayerCommand.java @@ -55,51 +55,21 @@ public class SlayerCommand extends CommandBase { player.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "Checking slayer of " + username)); } else { username = arg1[0]; - String uuidURL = "https://api.mojang.com/users/profiles/minecraft/" + username; player.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "Checking slayer of " + username)); - - JsonObject uuidResponse = ah.getResponse(uuidURL, player); - uuid = uuidResponse.get("id").getAsString(); + uuid = ah.getUUID(username); } - // Get profiles - System.out.println("Fetching profiles..."); - String profilesURL = "https://api.hypixel.net/skyblock/profiles?uuid=" + uuid + "&key=" + key; - - JsonObject profilesResponse = ah.getResponse(profilesURL, player); - if (!profilesResponse.get("success").getAsBoolean()) { - String reason = profilesResponse.get("cause").getAsString(); - player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Failed with reason: " + reason)); - return; - } - if (profilesResponse.get("profiles").isJsonNull()) { - player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "This player doesn't appear to have played SkyBlock.")); + // Find stats of latest profile + String latestProfile = ah.getLatestProfileID(uuid, key); + if (latestProfile == null) { return; } - // 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; - } - } - - // Find stats of latest profile - System.out.println("Fetching profile..."); String profileURL = "https://api.hypixel.net/skyblock/profile?profile=" + latestProfile + "&key=" + key; - - JsonObject profileResponse = ah.getResponse(profileURL, player); + System.out.println("Fetching profile..."); + JsonObject profileResponse = ah.getResponse(profileURL); if (!profileResponse.get("success").getAsBoolean()) { - String reason = profilesResponse.get("cause").getAsString(); + String reason = profileResponse.get("cause").getAsString(); player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Failed with reason: " + reason)); return; } 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; + } } |