From 1feaf693699254f7489ec4b9c02e5953553de9be Mon Sep 17 00:00:00 2001 From: ENORMOUZ Date: Sun, 3 Sep 2023 15:15:30 +0800 Subject: [PATCH] fix: Fix Guild Map Crashing --- .../map/overlays/objects/MapTerritory.java | 16 ++++-- .../java/com/wynntils/webapi/WebManager.java | 38 ++++++++++---- .../webapi/profiles/GuildColorProfile.java | 51 +++++++++++++++++++ .../webapi/profiles/UpdateProfile.java | 2 +- 4 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/wynntils/webapi/profiles/GuildColorProfile.java diff --git a/src/main/java/com/wynntils/modules/map/overlays/objects/MapTerritory.java b/src/main/java/com/wynntils/modules/map/overlays/objects/MapTerritory.java index 19b121a48..e230589ed 100644 --- a/src/main/java/com/wynntils/modules/map/overlays/objects/MapTerritory.java +++ b/src/main/java/com/wynntils/modules/map/overlays/objects/MapTerritory.java @@ -17,10 +17,13 @@ import com.wynntils.modules.map.instances.MapProfile; import com.wynntils.modules.map.managers.GuildResourceManager; import com.wynntils.modules.map.overlays.renderer.MapInfoUI; +import com.wynntils.webapi.WebManager; +import com.wynntils.webapi.profiles.GuildColorProfile; import com.wynntils.webapi.profiles.TerritoryProfile; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.util.text.TextFormatting; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; public class MapTerritory { @@ -64,7 +67,7 @@ public MapTerritory(TerritoryProfile territory) { description.add(TextFormatting.GRAY + "✦ Treasury: " + resources.getTreasury()); description.add(TextFormatting.GRAY + "Territory Defences: " + resources.getDefences()); description.add(""); - + String treasuryColor = resources.getTreasury().substring(0, 2); description.add(TextFormatting.GRAY + "Time held: " + treasuryColor + territory.getReadableRelativeTimeAcquired()); @@ -161,8 +164,15 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks, boolean terri private CustomColor getTerritoryColor(boolean resourceColor) { if (!resourceColor) { - return territory.getGuildColor() == null ? StringUtils.colorFromString(territory.getGuild()) : - StringUtils.colorFromHex(territory.getGuildColor()); + HashMap guildColorProfileHashMap = WebManager.getGuildColors(); + if (guildColorProfileHashMap.equals(new HashMap())) return new CustomColor(CommonColors.WHITE); + for (GuildColorProfile guildColorProfile : guildColorProfileHashMap.values()) { + if (guildColorProfile.getName().equals(territory.getGuild())) { + if (guildColorProfile.getGuildColor().length() == 7) return StringUtils.colorFromHex(guildColorProfile.getGuildColor()); + else return new CustomColor(CommonColors.WHITE); + } + } + return new CustomColor(CommonColors.WHITE); } else { return resources.getColor(); } diff --git a/src/main/java/com/wynntils/webapi/WebManager.java b/src/main/java/com/wynntils/webapi/WebManager.java index 99092eca5..f7fe24205 100644 --- a/src/main/java/com/wynntils/webapi/WebManager.java +++ b/src/main/java/com/wynntils/webapi/WebManager.java @@ -21,16 +21,7 @@ import com.wynntils.modules.map.MapModule; import com.wynntils.modules.map.overlays.objects.MapApiIcon; import com.wynntils.webapi.account.WynntilsAccount; -import com.wynntils.webapi.profiles.DiscoveryProfile; -import com.wynntils.webapi.profiles.LeaderboardProfile; -import com.wynntils.webapi.profiles.LocationProfile; -import com.wynntils.webapi.profiles.MapLabelProfile; -import com.wynntils.webapi.profiles.MapMarkerProfile; -import com.wynntils.webapi.profiles.MusicProfile; -import com.wynntils.webapi.profiles.SeaskipperProfile; -import com.wynntils.webapi.profiles.ServerProfile; -import com.wynntils.webapi.profiles.TerritoryProfile; -import com.wynntils.webapi.profiles.UpdateProfile; +import com.wynntils.webapi.profiles.*; import com.wynntils.webapi.profiles.guild.GuildProfile; import com.wynntils.webapi.profiles.ingredient.IngredientProfile; import com.wynntils.webapi.profiles.item.IdentificationOrderer; @@ -68,6 +59,7 @@ public class WebManager { private static @Nullable WebReader apiUrls; private static HashMap territories = new HashMap<>(); + private static HashMap guildColors = new HashMap<>(); private static UpdateProfile updateProfile; private static boolean ignoringJoinUpdate = false; @@ -139,6 +131,7 @@ public static void setupWebApi(boolean withProgress) { } updateTerritories(handler); + updateGuildColors(handler); updateItemList(handler); updateIngredientList(handler); updateMapLocations(handler); @@ -207,6 +200,10 @@ public static HashMap getTerritories() { return territories; } + public static HashMap getGuildColors() { + return guildColors; + } + public static HashMap getItems() { return items; } @@ -331,6 +328,27 @@ public static void updateTerritories(RequestHandler handler) { ); } + public static void updateGuildColors(RequestHandler handler) { + if (apiUrls == null) return; + String url = apiUrls.get("Athena") + "/cache/get/guildListWithColors"; + handler.addRequest(new Request(url, "guildColors") + .cacheTo(new File(API_CACHE_ROOT, "guildColors.json")) + .handleJsonObject(json -> { + if (!json.has("0")) return false; + + Type type = new TypeToken>() { + }.getType(); + + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeHierarchyAdapter(GuildColorProfile.class, new GuildColorProfile.GuildColorDeserializer()); + Gson gson = builder.create(); + + guildColors = gson.fromJson(json, type); + return true; + }) + ); + } + public static void updateCurrentSplash() { if (apiUrls == null || apiUrls.getList("Splashes") == null) return; diff --git a/src/main/java/com/wynntils/webapi/profiles/GuildColorProfile.java b/src/main/java/com/wynntils/webapi/profiles/GuildColorProfile.java new file mode 100644 index 000000000..624916527 --- /dev/null +++ b/src/main/java/com/wynntils/webapi/profiles/GuildColorProfile.java @@ -0,0 +1,51 @@ +package com.wynntils.webapi.profiles; + +import com.google.gson.*; + +import java.lang.reflect.Type; + +public class GuildColorProfile { + String name; + String prefix; + String guildColor; + + public GuildColorProfile(String name, String prefix, String guildColor) { + this.name = name; + this.prefix = prefix; + this.guildColor = guildColor; + + } + + public String getName() { + return name; + } + + public String getPrefix() { + return prefix; + } + + public String getGuildColor() { + return guildColor; + } + + public static class GuildColorDeserializer implements JsonDeserializer { + @Override + public GuildColorProfile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject guildColorObject = json.getAsJsonObject(); + + String name; + if (guildColorObject.get("_id").isJsonNull()) name = "Unknown"; + else name = guildColorObject.get("_id").getAsString(); + + String prefix; + if (guildColorObject.get("prefix").isJsonNull()) prefix = "UNK"; + else prefix = guildColorObject.get("prefix").getAsString(); + + String guildColor; + if (guildColorObject.get("color").getAsString().isEmpty()) guildColor = null; + else guildColor = guildColorObject.get("color").getAsString(); + + return new GuildColorProfile(name, prefix, guildColor); + } + } +} diff --git a/src/main/java/com/wynntils/webapi/profiles/UpdateProfile.java b/src/main/java/com/wynntils/webapi/profiles/UpdateProfile.java index ce3cb85ad..468f429ee 100644 --- a/src/main/java/com/wynntils/webapi/profiles/UpdateProfile.java +++ b/src/main/java/com/wynntils/webapi/profiles/UpdateProfile.java @@ -52,7 +52,7 @@ public UpdateProfile() { } public boolean hasUpdate() { - return (!updateDownloaded && hasUpdate); + return false; } public void updateDownloaded() {