diff options
Diffstat (limited to 'src/main')
4 files changed, 84 insertions, 34 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/events/ProfileDataLoadedEvent.java b/src/main/java/io/github/moulberry/notenoughupdates/events/ProfileDataLoadedEvent.java new file mode 100644 index 00000000..956acfe0 --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/events/ProfileDataLoadedEvent.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2022 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>. + */ + +package io.github.moulberry.notenoughupdates.events; + +import com.google.gson.JsonObject; + +import javax.annotation.Nullable; + +//TODO extend the usage of this event (accessory bag and storage data) +public class ProfileDataLoadedEvent extends NEUEvent { + + @Nullable + private final JsonObject data; + + public ProfileDataLoadedEvent(@Nullable JsonObject entireApiResponse) { + this.data = entireApiResponse; + } + + @Nullable + public JsonObject getData() { + return data; + } +} 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 7bbd5682..f364c390 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 @@ -24,6 +24,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; import io.github.moulberry.notenoughupdates.core.util.StringUtils; +import io.github.moulberry.notenoughupdates.events.ProfileDataLoadedEvent; import io.github.moulberry.notenoughupdates.miscgui.minionhelper.ApiData; import io.github.moulberry.notenoughupdates.miscgui.minionhelper.MinionHelperManager; import io.github.moulberry.notenoughupdates.profileviewer.ProfileViewer; @@ -90,16 +91,8 @@ public class MinionHelperApiLoader { dirty = false; String uuid = getUuid(); if (uuid == null) return; - HashMap<String, String> map = new HashMap<String, String>() {{ - put("uuid", uuid); - }}; - - NotEnoughUpdates neu = NotEnoughUpdates.INSTANCE; - neu.manager.hypixelApi.getHypixelApiAsync( - neu.config.apiData.apiKey, - "skyblock/profiles", - map - ).thenAccept(this::updateInformation); + + NotEnoughUpdates.INSTANCE.manager.hypixelApi.updateProfileData(uuid); } private String getUuid() { @@ -112,15 +105,17 @@ public class MinionHelperApiLoader { return thePlayer.getUniqueID().toString().replace("-", ""); } - private void updateInformation(JsonObject entireApiResponse) { - if (entireApiResponse == null) { + @SubscribeEvent + public void onApiDataLoaded(ProfileDataLoadedEvent event) { + JsonObject data = event.getData(); + if (data == null) { invalidApiKey = true; return; } invalidApiKey = false; - if (!entireApiResponse.has("success") || !entireApiResponse.get("success").getAsBoolean()) return; - JsonArray profiles = entireApiResponse.getAsJsonArray("profiles"); + if (!data.has("success") || !data.get("success").getAsBoolean()) return; + JsonArray profiles = data.getAsJsonArray("profiles"); for (JsonElement element : profiles) { JsonObject profile = element.getAsJsonObject(); String profileName = profile.get("cute_name").getAsString(); 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 5d7bfa6d..a80fcbc8 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; @@ -36,7 +37,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; public class HotmInformation { @@ -111,8 +111,6 @@ public class HotmInformation { } - private CompletableFuture<Void> updateTask = CompletableFuture.completedFuture(null); - private boolean shouldReloadSoon = false; public HotmInformation(NotEnoughUpdates neu) { @@ -135,7 +133,7 @@ public class HotmInformation { public synchronized void onLobbyJoin(WorldEvent.Load event) { if (shouldReloadSoon) { shouldReloadSoon = false; - requestUpdate(false); + neu.manager.hypixelApi.updateProfileData(); } } @@ -154,19 +152,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.hypixelApi.getHypixelApiAsync( - neu.config.apiData.apiKey, - "skyblock/profiles", - new HashMap<String, String>() {{ - put("uuid", Minecraft.getMinecraft().thePlayer.getUniqueID().toString().replace("-", "")); - }} - ).thenAccept(this::updateInformation); - } + neu.manager.hypixelApi.updateProfileData(); } /* @@ -179,9 +165,13 @@ public class HotmInformation { return QUICK_FORGE_MULTIPLIERS[level - 1]; } - public void updateInformation(JsonObject entireApiResponse) { - if (!entireApiResponse.has("success") || !entireApiResponse.get("success").getAsBoolean()) return; - JsonArray profiles = entireApiResponse.getAsJsonArray("profiles"); + @SubscribeEvent + public void onApiDataLoaded(ProfileDataLoadedEvent event) { + JsonObject data = event.getData(); + if (data == null) return; + + if (!data.has("success") || !data.get("success").getAsBoolean()) return; + JsonArray profiles = data.getAsJsonArray("profiles"); for (JsonElement element : profiles) { JsonObject profile = element.getAsJsonObject(); String profileName = profile.get("cute_name").getAsString(); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java index eb4ca90b..12a5ac50 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.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 java.io.IOException; @@ -43,10 +45,33 @@ public class HypixelApi { private final Gson gson = new Gson(); private final ExecutorService es = Executors.newFixedThreadPool(3); + private final Map<String, CompletableFuture<Void>> updateTasks = new HashMap<>(); + public CompletableFuture<JsonObject> getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args) { return getApiAsync(generateApiUrl(apiKey, method, args)); } + //TODO create class that calls this method aver EVERY world switch + public void updateProfileData() { + updateProfileData(Minecraft.getMinecraft().thePlayer.getUniqueID().toString().replace("-", "")); + } + + public void updateProfileData(String playerUuid) { + //TODO remove + Utils.addChatMessage("updateProfileData"); + if (!updateTasks.getOrDefault(playerUuid, CompletableFuture.completedFuture(null)).isDone()) return; + + HashMap<String, String> map = new HashMap<String, String>() {{ + put("uuid", playerUuid); + }}; + String apiKey = NotEnoughUpdates.INSTANCE.config.apiData.apiKey; + updateTasks.put(playerUuid, getHypixelApiAsync( + apiKey, + "skyblock/profiles", + map + ).thenAccept(jsonObject -> new ProfileDataLoadedEvent(jsonObject).post())); + } + public void getHypixelApiAsync( String apiKey, String method, |