From e5cc9718dad93581000783c1a298862e6da91c99 Mon Sep 17 00:00:00 2001 From: ENORMOUZ <68216143+ENORMOUZ@users.noreply.github.com> Date: Wed, 13 Sep 2023 18:57:54 +0800 Subject: [PATCH] fix: Fix Guild Map Crashing (#689) * fix: Fix Guild Map Crashing * fix: Fix Guild Map Crashing * fix: Make color loading faster * fix: Return random color if no valid color for guild * fix: Make random color better * fix: Generate random color only once, also optimize imports * refactor: use a much simpler data structure * Update src/main/java/com/wynntils/modules/map/overlays/objects/MapTerritory.java * 0 to 1 * CommonColors doesn't need any changes --------- Co-authored-by: DonkeyBlaster <57310593+DonkeyBlaster@users.noreply.github.com> --- .../map/overlays/objects/MapTerritory.java | 32 +++++++++++++---- .../java/com/wynntils/webapi/WebManager.java | 35 ++++++++++++++++++- 2 files changed, 59 insertions(+), 8 deletions(-) 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..408473283 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 @@ -10,22 +10,26 @@ import com.wynntils.core.framework.rendering.colors.CommonColors; import com.wynntils.core.framework.rendering.colors.CustomColor; import com.wynntils.core.framework.rendering.textures.Textures; -import com.wynntils.core.utils.StringUtils; import com.wynntils.core.utils.objects.Storage; import com.wynntils.modules.map.configs.MapConfig; import com.wynntils.modules.map.instances.GuildResourceContainer; 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.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; +import java.util.Map; +import java.util.Random; public class MapTerritory { private static final CustomColor territoryNameColour = new CustomColor(CommonColors.WHITE); + private static final HashMap backupGuildColors = new HashMap<>(); ScreenRenderer renderer = null; @@ -64,7 +68,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()); @@ -160,12 +164,26 @@ 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()); - } else { - return resources.getColor(); + if (resourceColor) return resources.getColor(); + + if (WebManager.getGuildColors().containsKey(territory.getGuild())) { + return WebManager.getGuildColors().get(territory.getGuild()); + } + + // Guild not found in the list, check backup list and add a random color if not found + if (!backupGuildColors.containsKey(territory.getGuild())) { + Random random = new Random(); + backupGuildColors.put( + territory.getGuild(), + CustomColor.fromHSV( + random.nextFloat(), + random.nextFloat(), + 0.5f + random.nextFloat() * 0.5f, + 1f + ) + ); } + return backupGuildColors.get(territory.getGuild()); } public void postDraw(int mouseX, int mouseY, float partialTicks, int width, int height) { diff --git a/src/main/java/com/wynntils/webapi/WebManager.java b/src/main/java/com/wynntils/webapi/WebManager.java index 99092eca5..60bac9f02 100644 --- a/src/main/java/com/wynntils/webapi/WebManager.java +++ b/src/main/java/com/wynntils/webapi/WebManager.java @@ -15,6 +15,7 @@ import com.wynntils.Reference; import com.wynntils.core.events.custom.WynnGuildWarEvent; import com.wynntils.core.framework.FrameworkManager; +import com.wynntils.core.framework.rendering.colors.CustomColor; import com.wynntils.core.utils.Utils; import com.wynntils.modules.core.enums.UpdateStream; import com.wynntils.modules.core.overlays.UpdateOverlay; @@ -37,7 +38,6 @@ import com.wynntils.webapi.profiles.item.ItemGuessProfile; import com.wynntils.webapi.profiles.item.ItemProfile; import com.wynntils.webapi.profiles.item.enums.ItemType; -import com.wynntils.webapi.profiles.item.objects.IdentificationContainer; import com.wynntils.webapi.profiles.item.objects.MajorIdentification; import com.wynntils.webapi.profiles.music.MusicLocationsProfile; import com.wynntils.webapi.profiles.player.PlayerStatsProfile; @@ -68,6 +68,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 +140,7 @@ public static void setupWebApi(boolean withProgress) { } updateTerritories(handler); + updateGuildColors(handler); updateItemList(handler); updateIngredientList(handler); updateMapLocations(handler); @@ -207,6 +209,10 @@ public static HashMap getTerritories() { return territories; } + public static HashMap getGuildColors() { + return guildColors; + } + public static HashMap getItems() { return items; } @@ -331,6 +337,33 @@ 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; + // json is {"0": {data}}, {"1": {data}}, etc. + // we need to convert it to {data}, {data}, etc. + guildColors = new HashMap<>(); + for (Map.Entry entry : json.entrySet()) { + JsonObject data = entry.getValue().getAsJsonObject(); + // data is now {"_id":"Kingdom Foxes","prefix":"Fox","color":"#FF8200"} + + String colorString = entry.getValue().getAsJsonObject().get("color").getAsString(); + if (colorString.length() != 7 && colorString.length() != 4 && colorString.length() != 3) continue; + + guildColors.put( // name, CustomColor + entry.getValue().getAsJsonObject().get("_id").getAsString(), + CustomColor.fromString(colorString, 1f) + ); + } + return true; + }) + ); + } + public static void updateCurrentSplash() { if (apiUrls == null || apiUrls.getList("Splashes") == null) return;