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
Browse files Browse the repository at this point in the history
  • Loading branch information
ENORMOUZ committed Sep 3, 2023
1 parent 71bf7fe commit 1feaf69
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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<String, GuildColorProfile> guildColorProfileHashMap = WebManager.getGuildColors();
if (guildColorProfileHashMap.equals(new HashMap<String, GuildColorProfile>())) 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();
}
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/wynntils/webapi/WebManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +59,7 @@ public class WebManager {
private static @Nullable WebReader apiUrls;

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

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

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

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

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

Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/wynntils/webapi/profiles/GuildColorProfile.java
Original file line number Diff line number Diff line change
@@ -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<GuildColorProfile> {
@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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public UpdateProfile() {
}

public boolean hasUpdate() {
return (!updateDownloaded && hasUpdate);
return false;
}

public void updateDownloaded() {
Expand Down

0 comments on commit 1feaf69

Please sign in to comment.