Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PV Collections Crash #959

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
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;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
Expand All @@ -34,12 +33,10 @@
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;

import java.awt.*;
import java.io.IOException;
import java.util.List;
import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand All @@ -50,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<String, Map<String, ?>> 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<String, String[]> COLLECTIONS;
private static Map<String, IntList> TIER_REQUIREMENTS;

private String playerName;
private JsonObject hypixelProfile;
Expand Down Expand Up @@ -202,7 +200,7 @@ public static void initClass() {
fetchCollectionsData(); // caching on launch

ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
LiteralArgumentBuilder<FabricClientCommandSource> literalArgumentBuilder = ClientCommandManager.literal("pv")
LiteralArgumentBuilder<FabricClientCommandSource> 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"))))
Expand All @@ -213,35 +211,41 @@ public static void initClass() {
});
}

@NotNull
public static Map<String, Map<String, ?>> fetchCollectionsData() {
if (!COLLECTIONS_CACHE.isEmpty()) return COLLECTIONS_CACHE;
try {
JsonObject jsonObject = JsonParser.parseString(Http.sendGetRequest(HYPIXEL_COLLECTIONS)).getAsJsonObject();
if (jsonObject.get("success").getAsBoolean()) {
Map<String, String[]> collectionsMap = new HashMap<>();
Map<String, IntList> 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<String, String[]> collectionsMap = new HashMap<>();
Map<String, IntList> 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 -> {
IntImmutableList tierReqs = IntImmutableList.toList(itemEntry.getValue().getAsJsonObject().getAsJsonArray("tiers").asList().stream()
.mapToInt(tier -> tier.getAsJsonObject().get("amountRequired").getAsInt())
);
tierRequirementsMap.put(itemEntry.getKey(), tierReqs);
});
});
});
COLLECTIONS_CACHE.put("COLLECTIONS", collectionsMap);
COLLECTIONS_CACHE.put("TIER_REQS", tierRequirementsMap);
return COLLECTIONS_CACHE;
COLLECTIONS = collectionsMap;
TIER_REQUIREMENTS = 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<String, String[]> getCollections() {
return COLLECTIONS;
}

public static Map<String, IntList> getTierRequirements() {
return TIER_REQUIREMENTS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +27,6 @@
import java.util.List;
import java.util.Map;

import static de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen.fetchCollectionsData;
import static de.hysky.skyblocker.skyblock.profileviewer.utils.ProfileViewerUtils.COMMA_FORMATTER;

public class GenericCategory implements ProfileViewerPage {
Expand All @@ -45,11 +45,8 @@ public class GenericCategory implements ProfileViewerPage {
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"};

public GenericCategory(JsonObject hProfile, JsonObject pProfile, String collection) {
Map<String, Map<String, ?>> fetchedData = fetchCollectionsData();
//noinspection unchecked
collectionsMap = (Map<String, String[]>) fetchedData.get("COLLECTIONS");
//noinspection unchecked
tierRequirementsMap = (Map<String, IntList>) fetchedData.get("TIER_REQS");
collectionsMap = ProfileViewerScreen.getCollections();
tierRequirementsMap = ProfileViewerScreen.getTierRequirements();
this.category = collection;
setupItemStacks(hProfile, pProfile);
}
Expand Down