aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils
diff options
context:
space:
mode:
authorKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2024-04-17 15:55:18 -0400
committerVeritasDL <121319869+VeritasDL@users.noreply.github.com>2024-04-19 18:01:00 -0400
commite049ed1d7ebfc265283d8ef0205d0826a1bec5de (patch)
treeab47462ddcafc77b1a0ccfa3ff44c849c10af26c /src/main/java/de/hysky/skyblocker/utils
parente6c0ca1ec23dfcd6e0137628a1df9d15cb65ab0d (diff)
downloadSkyblocker-e049ed1d7ebfc265283d8ef0205d0826a1bec5de.tar.gz
Skyblocker-e049ed1d7ebfc265283d8ef0205d0826a1bec5de.tar.bz2
Skyblocker-e049ed1d7ebfc265283d8ef0205d0826a1bec5de.zip
Add ProfileUtils and clean up code
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java b/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java
new file mode 100644
index 00000000..0a1f238a
--- /dev/null
+++ b/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java
@@ -0,0 +1,58 @@
+package de.hysky.skyblocker.utils;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import de.hysky.skyblocker.SkyblockerMod;
+import it.unimi.dsi.fastutil.objects.ObjectLongPair;
+import net.minecraft.client.MinecraftClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+
+public class ProfileUtils {
+ public static final Logger LOGGER = LoggerFactory.getLogger(ProfileUtils.class);
+ private static final long HYPIXEL_API_COOLDOWN = 300000; // 5min = 300000
+
+ public static Map<String, ObjectLongPair<JsonObject>> players = new HashMap<>();
+
+ public static void init() {
+ updateProfile();
+ }
+
+ public static CompletableFuture<JsonObject> updateProfile() {
+ return updateProfile(MinecraftClient.getInstance().getSession().getUsername());
+ }
+
+ public static CompletableFuture<JsonObject> updateProfile(String name) {
+ ObjectLongPair<JsonObject> playerCache = players.get(name);
+ if (playerCache != null && playerCache.rightLong() + HYPIXEL_API_COOLDOWN > System.currentTimeMillis()) {
+ return CompletableFuture.completedFuture(playerCache.left());
+ }
+
+ return CompletableFuture.supplyAsync(() -> {
+ String uuid = ApiUtils.name2Uuid(name);
+ try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/profiles", "?uuid=" + uuid)) {
+ if (!response.ok()) {
+ throw new IllegalStateException("Failed to get profile uuid for players " + name + "! Response: " + response.content());
+ }
+ JsonObject responseJson = SkyblockerMod.GSON.fromJson(response.content(), JsonObject.class);
+
+ JsonObject player = responseJson.getAsJsonArray("profiles").asList().stream()
+ .map(JsonElement::getAsJsonObject)
+ .filter(profile -> profile.getAsJsonPrimitive("selected").getAsBoolean())
+ .findFirst()
+ .orElseThrow(() -> new IllegalStateException("No selected profile found!?"))
+ .getAsJsonObject("members").get(uuid).getAsJsonObject();
+
+ players.put(name, ObjectLongPair.of(player, System.currentTimeMillis()));
+ return player;
+ } catch (Exception e) {
+ LOGGER.error("[Skyblocker Profile Utils] Failed to get Player Profile Data for players {}, is the API Down/Limited?", name, e);
+ }
+ return null;
+ });
+ }
+}