From 928b4a4bf101a5cb1647325ef333b85113839c0a Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Sun, 25 Aug 2024 13:50:35 -0400 Subject: [PATCH 1/3] Fix PV Collections Crash --- .../profileviewer/ProfileViewerScreen.java | 56 ++++++++++--------- .../collections/GenericCategory.java | 7 +-- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java index 80af29358a..d9a99b5470 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java @@ -39,7 +39,6 @@ import org.slf4j.LoggerFactory; import java.awt.*; -import java.io.IOException; import java.util.List; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -214,34 +213,37 @@ public static void initClass() { } @NotNull - public static Map> fetchCollectionsData() { - if (!COLLECTIONS_CACHE.isEmpty()) return COLLECTIONS_CACHE; - try { - JsonObject jsonObject = JsonParser.parseString(Http.sendGetRequest(HYPIXEL_COLLECTIONS)).getAsJsonObject(); - if (jsonObject.get("success").getAsBoolean()) { - Map collectionsMap = new HashMap<>(); - Map tierRequirementsMap = new HashMap<>(); - JsonObject collections = jsonObject.getAsJsonObject("collections"); - collections.entrySet().forEach(entry -> { - String category = entry.getKey(); - JsonObject itemsObject = entry.getValue().getAsJsonObject().getAsJsonObject("items"); - String[] items = itemsObject.keySet().toArray(new String[0]); - collectionsMap.put(category, items); - itemsObject.entrySet().forEach(itemEntry -> { - IntList tierReqs = new IntArrayList(); - itemEntry.getValue().getAsJsonObject().getAsJsonArray("tiers").forEach(req -> - tierReqs.add(req.getAsJsonObject().get("amountRequired").getAsInt())); - tierRequirementsMap.put(itemEntry.getKey(), tierReqs); + private static void fetchCollectionsData() { + CompletableFuture.runAsync(() -> { + try { + JsonObject jsonObject = JsonParser.parseString(Http.sendGetRequest(HYPIXEL_COLLECTIONS)).getAsJsonObject(); + if (jsonObject.get("success").getAsBoolean()) { + Map collectionsMap = new HashMap<>(); + Map tierRequirementsMap = new HashMap<>(); + JsonObject collections = jsonObject.getAsJsonObject("collections"); + collections.entrySet().forEach(entry -> { + String category = entry.getKey(); + JsonObject itemsObject = entry.getValue().getAsJsonObject().getAsJsonObject("items"); + String[] items = itemsObject.keySet().toArray(new String[0]); + collectionsMap.put(category, items); + itemsObject.entrySet().forEach(itemEntry -> { + IntList tierReqs = new IntArrayList(); + itemEntry.getValue().getAsJsonObject().getAsJsonArray("tiers").forEach(req -> + tierReqs.add(req.getAsJsonObject().get("amountRequired").getAsInt())); + tierRequirementsMap.put(itemEntry.getKey(), tierReqs); + }); }); - }); - COLLECTIONS_CACHE.put("COLLECTIONS", collectionsMap); - COLLECTIONS_CACHE.put("TIER_REQS", tierRequirementsMap); - return COLLECTIONS_CACHE; + COLLECTIONS_CACHE.put("COLLECTIONS", collectionsMap); + COLLECTIONS_CACHE.put("TIER_REQS", tierRequirementsMap); + } + } catch (Exception e) { + LOGGER.error("[Skyblocker Profile Viewer] Failed to fetch collections data", e); } - } catch (IOException | InterruptedException e) { - LOGGER.error("[Skyblocker Profile Viewer] Failed to fetch collections data", e); - } - return Collections.emptyMap(); + }); + } + + public static Map> getCollectionsData() { + return COLLECTIONS_CACHE; } /** diff --git a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/collections/GenericCategory.java b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/collections/GenericCategory.java index 10778adc0b..ec8fcd6d06 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/collections/GenericCategory.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/collections/GenericCategory.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.Map; -import static de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen.fetchCollectionsData; +import static de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen.getCollectionsData; import static de.hysky.skyblocker.skyblock.profileviewer.utils.ProfileViewerUtils.COMMA_FORMATTER; public class GenericCategory implements ProfileViewerPage { @@ -44,11 +44,10 @@ public class GenericCategory implements ProfileViewerPage { Map.entry("MUSHROOM_COLLECTION", "RED_MUSHROOM")); private final String[] ROMAN_NUMERALS = {"-", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"}; + @SuppressWarnings("unchecked") public GenericCategory(JsonObject hProfile, JsonObject pProfile, String collection) { - Map> fetchedData = fetchCollectionsData(); - //noinspection unchecked + Map> fetchedData = getCollectionsData(); collectionsMap = (Map) fetchedData.get("COLLECTIONS"); - //noinspection unchecked tierRequirementsMap = (Map) fetchedData.get("TIER_REQS"); this.category = collection; setupItemStacks(hProfile, pProfile); From 4ba73931e36c67fbdde51fa52df5a3d67d216444 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:17:53 +0800 Subject: [PATCH 2/3] Clean up ProfileViewerScreen --- .../skyblock/profileviewer/ProfileViewerScreen.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java index d9a99b5470..ad6987c7cf 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java @@ -16,7 +16,7 @@ import de.hysky.skyblocker.utils.Http; import de.hysky.skyblocker.utils.ProfileUtils; import de.hysky.skyblocker.utils.scheduler.Scheduler; -import it.unimi.dsi.fastutil.ints.IntArrayList; +import it.unimi.dsi.fastutil.ints.IntImmutableList; import it.unimi.dsi.fastutil.ints.IntList; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; @@ -34,7 +34,6 @@ import net.minecraft.entity.player.PlayerModelPart; import net.minecraft.text.Text; import net.minecraft.util.Identifier; -import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -201,7 +200,7 @@ public static void initClass() { fetchCollectionsData(); // caching on launch ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { - LiteralArgumentBuilder literalArgumentBuilder = ClientCommandManager.literal("pv") + LiteralArgumentBuilder literalArgumentBuilder = ClientCommandManager.literal("pv") .then(ClientCommandManager.argument("username", StringArgumentType.string()) .suggests((source, builder) -> CommandSource.suggestMatching(getPlayerSuggestions(source.getSource()), builder)) .executes(Scheduler.queueOpenScreenFactoryCommand(context -> new ProfileViewerScreen(StringArgumentType.getString(context, "username")))) @@ -212,7 +211,6 @@ public static void initClass() { }); } - @NotNull private static void fetchCollectionsData() { CompletableFuture.runAsync(() -> { try { @@ -227,9 +225,9 @@ private static void fetchCollectionsData() { String[] items = itemsObject.keySet().toArray(new String[0]); collectionsMap.put(category, items); itemsObject.entrySet().forEach(itemEntry -> { - IntList tierReqs = new IntArrayList(); - itemEntry.getValue().getAsJsonObject().getAsJsonArray("tiers").forEach(req -> - tierReqs.add(req.getAsJsonObject().get("amountRequired").getAsInt())); + IntImmutableList tierReqs = IntImmutableList.toList(itemEntry.getValue().getAsJsonObject().getAsJsonArray("tiers").asList().stream() + .mapToInt(tier -> tier.getAsJsonObject().get("amountRequired").getAsInt()) + ); tierRequirementsMap.put(itemEntry.getKey(), tierReqs); }); }); From 23fd52d0a8ce68bd4b908353e8b2c792dc02d5ef Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:43:36 +0800 Subject: [PATCH 3/3] Clean up collections cache --- .../profileviewer/ProfileViewerScreen.java | 16 ++++++++++------ .../collections/GenericCategory.java | 8 +++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java index ad6987c7cf..1939e0f6f2 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/ProfileViewerScreen.java @@ -18,7 +18,6 @@ import de.hysky.skyblocker.utils.scheduler.Scheduler; import it.unimi.dsi.fastutil.ints.IntImmutableList; import it.unimi.dsi.fastutil.ints.IntList; -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; @@ -48,10 +47,11 @@ public class ProfileViewerScreen extends Screen { public static final Logger LOGGER = LoggerFactory.getLogger(ProfileViewerScreen.class); private static final Text TITLE = Text.of("Skyblocker Profile Viewer"); private static final String HYPIXEL_COLLECTIONS = "https://api.hypixel.net/v2/resources/skyblock/collections"; - private static final Object2ObjectOpenHashMap> COLLECTIONS_CACHE = new Object2ObjectOpenHashMap<>(); private static final Identifier TEXTURE = Identifier.of(SkyblockerMod.NAMESPACE, "textures/gui/profile_viewer/base_plate.png"); private static final int GUI_WIDTH = 322; private static final int GUI_HEIGHT = 180; + private static Map COLLECTIONS; + private static Map TIER_REQUIREMENTS; private String playerName; private JsonObject hypixelProfile; @@ -231,8 +231,8 @@ private static void fetchCollectionsData() { tierRequirementsMap.put(itemEntry.getKey(), tierReqs); }); }); - COLLECTIONS_CACHE.put("COLLECTIONS", collectionsMap); - COLLECTIONS_CACHE.put("TIER_REQS", tierRequirementsMap); + COLLECTIONS = collectionsMap; + TIER_REQUIREMENTS = tierRequirementsMap; } } catch (Exception e) { LOGGER.error("[Skyblocker Profile Viewer] Failed to fetch collections data", e); @@ -240,8 +240,12 @@ private static void fetchCollectionsData() { }); } - public static Map> getCollectionsData() { - return COLLECTIONS_CACHE; + public static Map getCollections() { + return COLLECTIONS; + } + + public static Map getTierRequirements() { + return TIER_REQUIREMENTS; } /** diff --git a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/collections/GenericCategory.java b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/collections/GenericCategory.java index ec8fcd6d06..b2e0e7029f 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/collections/GenericCategory.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/collections/GenericCategory.java @@ -4,6 +4,7 @@ import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.skyblock.itemlist.ItemRepository; import de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerPage; +import de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen; import de.hysky.skyblocker.skyblock.tabhud.util.Ico; import it.unimi.dsi.fastutil.ints.IntList; import net.minecraft.client.MinecraftClient; @@ -26,7 +27,6 @@ import java.util.List; import java.util.Map; -import static de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen.getCollectionsData; import static de.hysky.skyblocker.skyblock.profileviewer.utils.ProfileViewerUtils.COMMA_FORMATTER; public class GenericCategory implements ProfileViewerPage { @@ -44,11 +44,9 @@ public class GenericCategory implements ProfileViewerPage { Map.entry("MUSHROOM_COLLECTION", "RED_MUSHROOM")); private final String[] ROMAN_NUMERALS = {"-", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"}; - @SuppressWarnings("unchecked") public GenericCategory(JsonObject hProfile, JsonObject pProfile, String collection) { - Map> fetchedData = getCollectionsData(); - collectionsMap = (Map) fetchedData.get("COLLECTIONS"); - tierRequirementsMap = (Map) fetchedData.get("TIER_REQS"); + collectionsMap = ProfileViewerScreen.getCollections(); + tierRequirementsMap = ProfileViewerScreen.getTierRequirements(); this.category = collection; setupItemStacks(hProfile, pProfile); }