aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils
diff options
context:
space:
mode:
authorKevin <92656833+kevinthegreat1@users.noreply.github.com>2024-06-30 14:35:01 +0800
committerGitHub <noreply@github.com>2024-06-30 14:35:01 +0800
commitb75bfdbeae151253d60c01c2c268dfa511997883 (patch)
tree11735fc95f58a6d4e85bd0a44cb009ca418924e5 /src/main/java/de/hysky/skyblocker/utils
parentbc773c76b3362af799577c89fa6f7b79c1ee013b (diff)
parentd824171262521557c73688068804c5d9119fbc7c (diff)
downloadSkyblocker-b75bfdbeae151253d60c01c2c268dfa511997883.tar.gz
Skyblocker-b75bfdbeae151253d60c01c2c268dfa511997883.tar.bz2
Skyblocker-b75bfdbeae151253d60c01c2c268dfa511997883.zip
Merge pull request #708 from BigloBot/main
Profile Viewer
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/ApiUtils.java6
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/Http.java11
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java37
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java46
4 files changed, 70 insertions, 30 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/ApiUtils.java b/src/main/java/de/hysky/skyblocker/utils/ApiUtils.java
index c63af3ba..93e314a7 100644
--- a/src/main/java/de/hysky/skyblocker/utils/ApiUtils.java
+++ b/src/main/java/de/hysky/skyblocker/utils/ApiUtils.java
@@ -1,16 +1,14 @@
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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/*
* Contains only basic helpers for using Http APIs
diff --git a/src/main/java/de/hysky/skyblocker/utils/Http.java b/src/main/java/de/hysky/skyblocker/utils/Http.java
index 361bab14..1adf75d3 100644
--- a/src/main/java/de/hysky/skyblocker/utils/Http.java
+++ b/src/main/java/de/hysky/skyblocker/utils/Http.java
@@ -1,5 +1,10 @@
package de.hysky.skyblocker.utils;
+import de.hysky.skyblocker.SkyblockerMod;
+import net.minecraft.SharedConstants;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
@@ -16,12 +21,6 @@ import java.time.Duration;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import de.hysky.skyblocker.SkyblockerMod;
-import net.minecraft.SharedConstants;
-
/**
* @implNote All http requests are sent using HTTP 2
*/
diff --git a/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java b/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java
index a786e79f..aa7a0492 100644
--- a/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java
+++ b/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java
@@ -4,7 +4,6 @@ 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;
@@ -17,13 +16,24 @@ public class ProfileUtils {
private static final long HYPIXEL_API_COOLDOWN = 300000; // 5min = 300000
public static Map<String, ObjectLongPair<JsonObject>> players = new HashMap<>();
-
- public static CompletableFuture<JsonObject> updateProfile() {
- return updateProfile(MinecraftClient.getInstance().getSession().getUsername());
+ public static Map<String, ObjectLongPair<JsonObject>> profiles = new HashMap<>();
+
+ public static CompletableFuture<JsonObject> updateProfileByName(String name) {
+ return fetchFullProfile(name).thenApply(profile -> {
+ JsonObject player = profile.getAsJsonArray("profiles").asList().stream()
+ .map(JsonElement::getAsJsonObject)
+ .filter(profileObj -> profileObj.getAsJsonPrimitive("selected").getAsBoolean())
+ .findFirst()
+ .orElseThrow(() -> new IllegalStateException("No selected profile found!?"))
+ .getAsJsonObject("members").get(name).getAsJsonObject();
+
+ players.put(name, ObjectLongPair.of(player, System.currentTimeMillis()));
+ return player;
+ });
}
- public static CompletableFuture<JsonObject> updateProfile(String name) {
- ObjectLongPair<JsonObject> playerCache = players.get(name);
+ public static CompletableFuture<JsonObject> fetchFullProfile(String name) {
+ ObjectLongPair<JsonObject> playerCache = profiles.get(name);
if (playerCache != null && playerCache.rightLong() + HYPIXEL_API_COOLDOWN > System.currentTimeMillis()) {
return CompletableFuture.completedFuture(playerCache.left());
}
@@ -32,19 +42,12 @@ public class ProfileUtils {
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());
+ throw new IllegalStateException("Failed to get profile uuid for player: " + 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();
+ JsonObject profile = SkyblockerMod.GSON.fromJson(response.content(), JsonObject.class);
+ profiles.put(name, ObjectLongPair.of(profile, System.currentTimeMillis()));
- players.put(name, ObjectLongPair.of(player, System.currentTimeMillis()));
- return player;
+ return profile;
} catch (Exception e) {
LOGGER.error("[Skyblocker Profile Utils] Failed to get Player Profile Data for players {}, is the API Down/Limited?", name, e);
}
diff --git a/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java b/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java
index 2f5375fe..547adc5c 100644
--- a/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java
+++ b/src/main/java/de/hysky/skyblocker/utils/scheduler/Scheduler.java
@@ -2,6 +2,7 @@ package de.hysky.skyblocker.utils.scheduler;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.mojang.brigadier.Command;
+import com.mojang.brigadier.context.CommandContext;
import it.unimi.dsi.fastutil.ints.AbstractInt2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
@@ -14,6 +15,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.function.Function;
import java.util.function.Supplier;
/**
@@ -73,18 +75,56 @@ public class Scheduler {
}
}
+ /**
+ * Creates a command that queues a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
+ *
+ * @param screenFactory the factory of the screen to open
+ * @return the command
+ */
+ public static Command<FabricClientCommandSource> queueOpenScreenFactoryCommand(Function<CommandContext<FabricClientCommandSource>, Screen> screenFactory) {
+ return context -> queueOpenScreen(screenFactory.apply(context));
+ }
+
+ /**
+ * Creates a command that queues a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
+ *
+ * @param screenSupplier the supplier of the screen to open
+ * @return the command
+ */
public static Command<FabricClientCommandSource> queueOpenScreenCommand(Supplier<Screen> screenSupplier) {
- return context -> INSTANCE.queueOpenScreen(screenSupplier);
+ return context -> queueOpenScreen(screenSupplier.get());
+ }
+
+ /**
+ * Creates a command that queues a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
+ *
+ * @param screen the screen to open
+ * @return the command
+ */
+ public static Command<FabricClientCommandSource> queueOpenScreenCommand(Screen screen) {
+ return context -> queueOpenScreen(screen);
}
/**
* Schedules a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
*
+ * @deprecated Use {@link #queueOpenScreen(Screen)} instead
* @param screenSupplier the supplier of the screen to open
* @see #queueOpenScreenCommand(Supplier)
*/
- public int queueOpenScreen(Supplier<Screen> screenSupplier) {
- MinecraftClient.getInstance().send(() -> MinecraftClient.getInstance().setScreen(screenSupplier.get()));
+ @Deprecated(forRemoval = true)
+ public static int queueOpenScreen(Supplier<Screen> screenSupplier) {
+ return queueOpenScreen(screenSupplier.get());
+ }
+
+ /**
+ * Schedules a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
+ *
+ * @param screen the screen to open
+ * @see #queueOpenScreenFactoryCommand(Function)
+ */
+ public static int queueOpenScreen(Screen screen) {
+ MinecraftClient.getInstance().send(() -> MinecraftClient.getInstance().setScreen(screen));
return Command.SINGLE_SUCCESS;
}