diff options
3 files changed, 29 insertions, 15 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/loaders/MinionHelperApiLoader.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/loaders/MinionHelperApiLoader.java index f364c390..aaa398f4 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/loaders/MinionHelperApiLoader.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/loaders/MinionHelperApiLoader.java @@ -92,7 +92,7 @@ public class MinionHelperApiLoader { String uuid = getUuid(); if (uuid == null) return; - NotEnoughUpdates.INSTANCE.manager.hypixelApi.updateProfileData(uuid); + NotEnoughUpdates.INSTANCE.manager.apiUtils.updateProfileData(uuid); } private String getUuid() { diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java index 8c594911..32601e3e 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java @@ -22,6 +22,8 @@ package io.github.moulberry.notenoughupdates.util; import com.google.gson.Gson; import com.google.gson.JsonObject; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; +import io.github.moulberry.notenoughupdates.events.ProfileDataLoadedEvent; +import net.minecraft.client.Minecraft; import org.apache.commons.io.IOUtils; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URIBuilder; @@ -47,7 +49,9 @@ import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -58,6 +62,7 @@ public class ApiUtil { private static final ExecutorService executorService = Executors.newFixedThreadPool(3); private static final String USER_AGENT = "NotEnoughUpdates/" + NotEnoughUpdates.VERSION; private static SSLContext ctx; + private final Map<String, CompletableFuture<Void>> updateTasks = new HashMap<>(); static { try { @@ -76,6 +81,23 @@ public class ApiUtil { } } + public void updateProfileData() { + updateProfileData(Minecraft.getMinecraft().thePlayer.getUniqueID().toString().replace("-", "")); + } + + public void updateProfileData(String playerUuid) { + if (!updateTasks.getOrDefault(playerUuid, CompletableFuture.completedFuture(null)).isDone()) return; + + updateTasks.put(playerUuid, newHypixelApiRequest("skyblock/profiles") + .queryArgument("uuid", Minecraft.getMinecraft().thePlayer.getUniqueID().toString().replace("-", "")) + .requestJson() + .handle((jsonObject, throwable) -> { + new ProfileDataLoadedEvent(jsonObject).post(); + return null; + })); + + } + public static class Request { private final List<NameValuePair> queryArguments = new ArrayList<>(); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/HotmInformation.java b/src/main/java/io/github/moulberry/notenoughupdates/util/HotmInformation.java index 1968b51e..3b84c4f9 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/HotmInformation.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HotmInformation.java @@ -23,6 +23,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; +import io.github.moulberry.notenoughupdates.events.ProfileDataLoadedEvent; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiChest; import net.minecraft.inventory.ContainerChest; @@ -135,7 +136,7 @@ public class HotmInformation { public synchronized void onLobbyJoin(WorldEvent.Load event) { if (shouldReloadSoon) { shouldReloadSoon = false; - requestUpdate(false); + neu.manager.apiUtils.updateProfileData(); } } @@ -154,17 +155,7 @@ public class HotmInformation { @SubscribeEvent public synchronized void onChat(ClientChatReceivedEvent event) { if (event.message.getUnformattedText().equals("Welcome to Hypixel SkyBlock!")) - requestUpdate(false); - } - - public synchronized void requestUpdate(boolean force) { - if (updateTask.isDone() || force) { - updateTask = neu.manager.apiUtils - .newHypixelApiRequest("skyblock/profiles") - .queryArgument("uuid", Minecraft.getMinecraft().thePlayer.getUniqueID().toString().replace("-", "")) - .requestJson() - .thenAccept(this::updateInformation); - } + neu.manager.apiUtils.updateProfileData(); } /* @@ -176,8 +167,9 @@ public class HotmInformation { if (level > 20) return -1; return QUICK_FORGE_MULTIPLIERS[level - 1]; } - - public void updateInformation(JsonObject entireApiResponse) { + @SubscribeEvent + public void onApiDataLoaded(ProfileDataLoadedEvent event) { + JsonObject entireApiResponse = event.getData(); if (!entireApiResponse.has("success") || !entireApiResponse.get("success").getAsBoolean()) return; JsonArray profiles = entireApiResponse.getAsJsonArray("profiles"); for (JsonElement element : profiles) { |