aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/ApiUtils.java53
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/Http.java22
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/Utils.java7
3 files changed, 72 insertions, 10 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/ApiUtils.java b/src/main/java/de/hysky/skyblocker/utils/ApiUtils.java
new file mode 100644
index 00000000..c0648eba
--- /dev/null
+++ b/src/main/java/de/hysky/skyblocker/utils/ApiUtils.java
@@ -0,0 +1,53 @@
+package de.hysky.skyblocker.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gson.JsonParser;
+import com.mojang.util.UndashedUuid;
+
+import de.hysky.skyblocker.utils.Http.ApiResponse;
+import de.hysky.skyblocker.utils.scheduler.Scheduler;
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.session.Session;
+
+/*
+ * Contains only basic helpers for using Http APIs
+ */
+public class ApiUtils {
+ private static final Logger LOGGER = LoggerFactory.getLogger(ApiUtils.class);
+ /**
+ * Do not iterate over this map, it will be accessed and modified by multiple threads.
+ */
+ private static final Object2ObjectOpenHashMap<String, String> NAME_2_UUID_CACHE = new Object2ObjectOpenHashMap<>();
+
+ public static void init() {
+ //Clear cache every 20 minutes
+ Scheduler.INSTANCE.scheduleCyclic(NAME_2_UUID_CACHE::clear, 24_000, true);
+ }
+
+ /**
+ * Multithreading is to be handled by the method caller
+ */
+ public static String name2Uuid(String name) {
+ Session session = MinecraftClient.getInstance().getSession();
+
+ if (session.getUsername().equals(name)) return UndashedUuid.toString(session.getUuidOrNull());
+ if (NAME_2_UUID_CACHE.containsKey(name)) return NAME_2_UUID_CACHE.get(name);
+
+ try (ApiResponse response = Http.sendName2UuidRequest(name)) {
+ if (response.ok()) {
+ String uuid = JsonParser.parseString(response.content()).getAsJsonObject().get("id").getAsString();
+
+ NAME_2_UUID_CACHE.put(name, uuid);
+
+ return uuid;
+ }
+ } catch (Exception e) {
+ LOGGER.error("[Skyblocker] Name to uuid lookup failed! Name: {}", name, e);
+ }
+
+ return "";
+ }
+}
diff --git a/src/main/java/de/hysky/skyblocker/utils/Http.java b/src/main/java/de/hysky/skyblocker/utils/Http.java
index eabb02e4..573c3458 100644
--- a/src/main/java/de/hysky/skyblocker/utils/Http.java
+++ b/src/main/java/de/hysky/skyblocker/utils/Http.java
@@ -49,9 +49,11 @@ public class Http {
HttpResponse<InputStream> response = HTTP_CLIENT.send(request, BodyHandlers.ofInputStream());
InputStream decodedInputStream = getDecodedInputStream(response);
+
String body = new String(decodedInputStream.readAllBytes());
+ HttpHeaders headers = response.headers();
- return new ApiResponse(body, response.statusCode(), getCacheStatus(response.headers()));
+ return new ApiResponse(body, response.statusCode(), getCacheStatus(headers), getAge(headers));
}
public static HttpHeaders sendHeadRequest(String url) throws IOException, InterruptedException {
@@ -66,8 +68,8 @@ public class Http {
return response.headers();
}
- public static String sendName2UuidRequest(String name) throws IOException, InterruptedException {
- return sendGetRequest(NAME_2_UUID + name);
+ public static ApiResponse sendName2UuidRequest(String name) throws IOException, InterruptedException {
+ return sendCacheableGetRequest(NAME_2_UUID + name);
}
/**
@@ -115,12 +117,16 @@ public class Http {
*
* @see <a href="https://developers.cloudflare.com/cache/concepts/default-cache-behavior/#cloudflare-cache-responses">Cloudflare Cache Docs</a>
*/
- public static String getCacheStatus(HttpHeaders headers) {
+ private static String getCacheStatus(HttpHeaders headers) {
return headers.firstValue("CF-Cache-Status").orElse("UNKNOWN");
}
+ private static int getAge(HttpHeaders headers) {
+ return Integer.parseInt(headers.firstValue("Age").orElse("-1"));
+ }
+
//TODO If ever needed, we could just replace cache status with the response headers and go from there
- public record ApiResponse(String content, int statusCode, String cacheStatus) {
+ public record ApiResponse(String content, int statusCode, String cacheStatus, int age) implements AutoCloseable {
public boolean ok() {
return statusCode == 200;
@@ -129,5 +135,11 @@ public class Http {
public boolean cached() {
return cacheStatus.equals("HIT");
}
+
+ @Override
+ public void close() {
+ //Allows for nice syntax when dealing with api requests in try catch blocks
+ //Maybe one day we'll have some resources to free
+ }
}
}
diff --git a/src/main/java/de/hysky/skyblocker/utils/Utils.java b/src/main/java/de/hysky/skyblocker/utils/Utils.java
index b1a347f9..71e08865 100644
--- a/src/main/java/de/hysky/skyblocker/utils/Utils.java
+++ b/src/main/java/de/hysky/skyblocker/utils/Utils.java
@@ -33,10 +33,10 @@ import java.util.List;
public class Utils {
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
private static final String ALTERNATE_HYPIXEL_ADDRESS = System.getProperty("skyblocker.alternateHypixelAddress", "");
+ private static final String DUNGEONS_LOCATION = "dungeon";
private static final String PROFILE_PREFIX = "Profile: ";
private static boolean isOnHypixel = false;
private static boolean isOnSkyblock = false;
- private static boolean isInDungeons = false;
private static boolean isInjected = false;
/**
* The profile name parsed from the player list.
@@ -79,7 +79,7 @@ public class Utils {
}
public static boolean isInDungeons() {
- return isInDungeons;
+ return getLocationRaw().equals(DUNGEONS_LOCATION) || FabricLoader.getInstance().isDevelopmentEnvironment();
}
public static boolean isInTheRift() {
@@ -164,7 +164,6 @@ public class Utils {
sidebar = Collections.emptyList();
} else {
isOnSkyblock = false;
- isInDungeons = false;
return;
}
}
@@ -188,7 +187,6 @@ public class Utils {
} else {
onLeaveSkyblock();
}
- isInDungeons = fabricLoader.isDevelopmentEnvironment() || isOnSkyblock && string.contains("The Catacombs");
} else if (isOnHypixel) {
isOnHypixel = false;
onLeaveSkyblock();
@@ -205,7 +203,6 @@ public class Utils {
private static void onLeaveSkyblock() {
if (isOnSkyblock) {
isOnSkyblock = false;
- isInDungeons = false;
SkyblockEvents.LEAVE.invoker().onSkyblockLeave();
}
}