aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/cowtipper/cowlection/util')
-rw-r--r--src/main/java/de/cowtipper/cowlection/util/ApiUtils.java19
-rw-r--r--src/main/java/de/cowtipper/cowlection/util/GsonUtils.java31
2 files changed, 48 insertions, 2 deletions
diff --git a/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java b/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java
index 35d9542..293c6ad 100644
--- a/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java
+++ b/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java
@@ -6,6 +6,7 @@ import com.google.gson.JsonSyntaxException;
import com.mojang.util.UUIDTypeAdapter;
import de.cowtipper.cowlection.Cowlection;
import de.cowtipper.cowlection.chesttracker.HyBazaarData;
+import de.cowtipper.cowlection.chesttracker.LowestBinsCache;
import de.cowtipper.cowlection.command.exception.ThrowingConsumer;
import de.cowtipper.cowlection.config.CredentialStorage;
import de.cowtipper.cowlection.data.*;
@@ -29,6 +30,7 @@ public class ApiUtils {
private static final String ONLINE_STATUS_URL = "https://api.hypixel.net/status?key=%s&uuid=%s";
private static final String SKYBLOCK_STATS_URL = "https://api.hypixel.net/skyblock/profiles?key=%s&uuid=%s";
private static final String BAZAAR_URL = "https://api.hypixel.net/skyblock/bazaar";
+ public static final String LOWEST_BINS = "https://moulberry.codes/lowestbin.json";
private static final String PLAYER_URL = "https://api.hypixel.net/player?key=%s&uuid=%s";
private static final String API_KEY_URL = "https://api.hypixel.net/key?key=%s";
private static final ExecutorService pool = Executors.newCachedThreadPool();
@@ -118,6 +120,21 @@ public class ApiUtils {
return null;
}
+ public static void fetchLowestBins(ThrowingConsumer<LowestBinsCache> action) {
+ pool.execute(() -> action.accept(getLowestBins()));
+ }
+
+ private static LowestBinsCache getLowestBins() {
+ try (BufferedReader reader = makeApiCall(LOWEST_BINS)) {
+ if (reader != null) {
+ return GsonUtils.fromJson(reader, LowestBinsCache.class);
+ }
+ } catch (IOException | JsonSyntaxException e) {
+ e.printStackTrace();
+ }
+ return new LowestBinsCache();
+ }
+
public static void fetchHyPlayerDetails(Friend stalkedPlayer, ThrowingConsumer<HyPlayerData> action) {
pool.execute(() -> action.accept(stalkHyPlayer(stalkedPlayer)));
}
@@ -161,6 +178,8 @@ public class ApiUtils {
return null;
} else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://api.hypixel.net/")) { // http status 502 (cloudflare)
throw new IOException("Couldn't contact Hypixel API (502 Bad Gateway). API might be down, check https://status.hypixel.net for info.");
+ } else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://moulberry.codes/")) { // http status 502 (cloudflare)
+ throw new IOException("Couldn't contact Moulberry's API (502 Bad Gateway). API might be down, check if " + LOWEST_BINS + " is reachable.");
} else {
BufferedReader reader;
InputStream errorStream = connection.getErrorStream();
diff --git a/src/main/java/de/cowtipper/cowlection/util/GsonUtils.java b/src/main/java/de/cowtipper/cowlection/util/GsonUtils.java
index e97a88d..d2d3e28 100644
--- a/src/main/java/de/cowtipper/cowlection/util/GsonUtils.java
+++ b/src/main/java/de/cowtipper/cowlection/util/GsonUtils.java
@@ -2,6 +2,7 @@ package de.cowtipper.cowlection.util;
import com.google.gson.*;
import com.mojang.util.UUIDTypeAdapter;
+import de.cowtipper.cowlection.chesttracker.LowestBinsCache;
import de.cowtipper.cowlection.data.HyPlayerData;
import net.minecraft.nbt.*;
import net.minecraftforge.common.util.Constants;
@@ -19,8 +20,14 @@ import java.util.TreeMap;
import java.util.UUID;
public final class GsonUtils {
- private static final Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(HyPlayerData.class, new HyPlayerDataDeserializer()).create();
- private static final Gson gsonPrettyPrinter = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).setPrettyPrinting().create();
+ private static final Gson gson = new GsonBuilder()
+ .registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
+ .registerTypeAdapter(HyPlayerData.class, new HyPlayerDataDeserializer())
+ .registerTypeAdapter(LowestBinsCache.class, new LowestBinsDeserializer())
+ .create();
+ private static final Gson gsonPrettyPrinter = new GsonBuilder()
+ .registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
+ .setPrettyPrinting().create();
private GsonUtils() {
}
@@ -168,4 +175,24 @@ public final class GsonUtils {
return hyPlayerData;
}
}
+
+ public static class LowestBinsDeserializer implements JsonDeserializer<LowestBinsCache> {
+ @Override
+ public LowestBinsCache deserialize(JsonElement json, Type type, JsonDeserializationContext jdc) throws JsonParseException {
+ LowestBinsCache lowestBinsCache = new LowestBinsCache();
+ if (!json.isJsonObject()) {
+ // invalid JSON
+ return lowestBinsCache;
+ }
+ JsonObject lowestBins = json.getAsJsonObject();
+ for (Map.Entry<String, JsonElement> entry : lowestBins.entrySet()) {
+ try {
+ lowestBinsCache.put(entry.getKey(), entry.getValue().getAsInt());
+ } catch (ClassCastException | NumberFormatException ignored) {
+ // somehow not an integer
+ }
+ }
+ return lowestBinsCache;
+ }
+ }
}