Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
fix: Fix Guild Map Crashing (#689)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ENORMOUZ and ryanzhoudev authored Sep 13, 2023
1 parent 71bf7fe commit e5cc971
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, CustomColor> backupGuildColors = new HashMap<>();

ScreenRenderer renderer = null;

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/com/wynntils/webapi/WebManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -68,6 +68,7 @@ public class WebManager {
private static @Nullable WebReader apiUrls;

private static HashMap<String, TerritoryProfile> territories = new HashMap<>();
private static HashMap<String, CustomColor> guildColors = new HashMap<>();
private static UpdateProfile updateProfile;
private static boolean ignoringJoinUpdate = false;

Expand Down Expand Up @@ -139,6 +140,7 @@ public static void setupWebApi(boolean withProgress) {
}

updateTerritories(handler);
updateGuildColors(handler);
updateItemList(handler);
updateIngredientList(handler);
updateMapLocations(handler);
Expand Down Expand Up @@ -207,6 +209,10 @@ public static HashMap<String, TerritoryProfile> getTerritories() {
return territories;
}

public static HashMap<String, CustomColor> getGuildColors() {
return guildColors;
}

public static HashMap<String, ItemProfile> getItems() {
return items;
}
Expand Down Expand Up @@ -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<String, JsonElement> 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;

Expand Down

0 comments on commit e5cc971

Please sign in to comment.