diff options
author | Fix3d <serhanduzce@gmail.com> | 2023-03-22 12:01:14 +0300 |
---|---|---|
committer | Fix3d <serhanduzce@gmail.com> | 2023-03-22 12:01:14 +0300 |
commit | 13b09e3be36ab38b92e14c7d808fb04ecdc65b82 (patch) | |
tree | 7ca8662dc1fe4804b4e9d3c80423dd7342d4beba /src/main | |
parent | 78215e4d9cf83635d7ab5a6474170ac673cae525 (diff) | |
download | Skyblocker-13b09e3be36ab38b92e14c7d808fb04ecdc65b82.tar.gz Skyblocker-13b09e3be36ab38b92e14c7d808fb04ecdc65b82.tar.bz2 Skyblocker-13b09e3be36ab38b92e14c7d808fb04ecdc65b82.zip |
a cleaner PriceInfoTooltip
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java | 5 | ||||
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java | 134 |
2 files changed, 44 insertions, 95 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java b/src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java index 97284bc1..4d3ce5e9 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java +++ b/src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java @@ -6,7 +6,6 @@ import me.shedaniel.autoconfig.annotation.Config; import me.shedaniel.autoconfig.annotation.ConfigEntry; import me.shedaniel.autoconfig.serializer.GsonConfigSerializer; import me.xmrvizzy.skyblocker.chat.ChatFilterResult; -import net.minecraft.item.ItemStack; import java.util.ArrayList; import java.util.List; @@ -219,8 +218,8 @@ public class SkyblockerConfig implements ConfigData { public String toString() { return switch (this) { - case ONE_DAY -> "1 day price"; - case THREE_DAY -> "3 day price"; + case ONE_DAY -> "1 day avg"; + case THREE_DAY -> "3 day avg"; case BOTH -> "Both"; }; } diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java index dfc498a1..c7df6a99 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java @@ -9,10 +9,8 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.item.TooltipContext; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; -import net.minecraft.text.LiteralTextContent; import net.minecraft.text.MutableText; import net.minecraft.text.Text; -import net.minecraft.text.TranslatableTextContent; import net.minecraft.util.Formatting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,8 +23,10 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.zip.GZIPInputStream; @@ -42,6 +42,7 @@ public class PriceInfoTooltip { private static JsonObject isMuseumJson; private static boolean nullMsgSend = false; private final static Gson gson = new Gson(); + private static final Map<String, String> apiAddresses; public static void onInjectTooltip(ItemStack stack, TooltipContext context, List<Text> lines) { if (!Utils.isOnSkyblock || client.player == null) return; @@ -184,7 +185,7 @@ public class PriceInfoTooltip { /** * this method converts the "timestamp" variable into the same date format as Hypixel represents it in the museum. - * Currently there are two types of timestamps the legacy which is built like this + * Currently, there are two types of timestamps the legacy which is built like this * "dd/MM/yy hh:mm" ("25/04/20 16:38") and the current which is built like this * "MM/dd/yy hh:mm aa" ("12/24/20 11:08 PM"). Since Hypixel transforms the two formats into one format without * taking into account of their formats, we do the same. The final result looks like this @@ -194,7 +195,7 @@ public class PriceInfoTooltip { * This causes the museum rank to be much worse than it should be. * * @param stack the item under the pointer - * @return if the item have an "Timestamp" it will be shown formated on the tooltip + * @return if the item have a "Timestamp" it will be shown formated on the tooltip */ public static String getTimestamp(ItemStack stack) { NbtCompound tag = getInternalNameForItem(stack); @@ -289,108 +290,57 @@ public class PriceInfoTooltip { SkyblockerConfig.Average type = SkyblockerConfig.get().general.itemTooltip.avg; if (type == SkyblockerConfig.Average.BOTH || oneDayAvgPricesJson == null || threeDayAvgPricesJson == null) { - futureList.add(CompletableFuture.runAsync(() -> downloadAvgPrices(SkyblockerConfig.Average.THREE_DAY))); - futureList.add(CompletableFuture.runAsync(() -> downloadAvgPrices(SkyblockerConfig.Average.ONE_DAY))); - } else { - futureList.add(CompletableFuture.runAsync(() -> downloadAvgPrices(type))); + futureList.add(CompletableFuture.runAsync(() -> oneDayAvgPricesJson = downloadPrices("1 day avg"))); + futureList.add(CompletableFuture.runAsync(() -> threeDayAvgPricesJson = downloadPrices("3 day avg"))); + } else if (type == SkyblockerConfig.Average.ONE_DAY) { + futureList.add(CompletableFuture.runAsync(() -> oneDayAvgPricesJson = downloadPrices("1 day avg"))); + } else if (type == SkyblockerConfig.Average.THREE_DAY) { + futureList.add(CompletableFuture.runAsync(() -> threeDayAvgPricesJson = downloadPrices("3 day avg"))); } } - if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN) { - futureList.add(CompletableFuture.runAsync(PriceInfoTooltip::downloadLowestPrices)); - } - if (SkyblockerConfig.get().general.itemTooltip.enableBazaarPrice) { - futureList.add(CompletableFuture.runAsync(PriceInfoTooltip::downloadBazaarPrices)); - } - if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice && npcPricesJson == null) { - futureList.add(CompletableFuture.runAsync(PriceInfoTooltip::downloadNPCPrices)); - } - if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate && isMuseumJson == null) { - futureList.add(CompletableFuture.runAsync(PriceInfoTooltip::downloadIsMuseum)); - } + if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN) + futureList.add(CompletableFuture.runAsync(() -> lowestPricesJson = downloadPrices("lowest bin"))); + + if (SkyblockerConfig.get().general.itemTooltip.enableBazaarPrice) + futureList.add(CompletableFuture.runAsync(() -> bazaarPricesJson = downloadPrices("bazaar"))); + + if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice && npcPricesJson == null) + futureList.add(CompletableFuture.runAsync(() -> npcPricesJson = downloadPrices("npc"))); + + if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate && isMuseumJson == null) + futureList.add(CompletableFuture.runAsync(() -> isMuseumJson = downloadPrices("museum"))); + minute++; CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])) .whenComplete((unused, throwable) -> nullMsgSend = false); }, 1200); } - private static void downloadAvgPrices(SkyblockerConfig.Average type) { - JsonObject result = null; - String avgDay = null; - switch (type) { - case ONE_DAY -> avgDay = "1day.json.gz"; - case THREE_DAY -> avgDay = "3day.json.gz"; - } + private static JsonObject downloadPrices(String type) { try { - URL apiAddr = new URL("https://moulberry.codes/auction_averages_lbin/" + avgDay); - try (InputStream src = apiAddr.openStream()) { - try (GZIPInputStream gzipOutput = new GZIPInputStream(src)) { - try (InputStreamReader reader = new InputStreamReader(gzipOutput)) { - result = new Gson().fromJson(reader, JsonObject.class); - } - } - } + String url = apiAddresses.get(type); + URL apiAddress = new URL(url); + InputStream src = apiAddress.openStream(); + InputStreamReader reader = new InputStreamReader(url.contains(".gz") ? new GZIPInputStream(src) : src); + return new Gson().fromJson(reader, JsonObject.class); } catch (IOException e) { - LOGGER.warn("[Skyblocker] Failed to download average BIN prices!", e); - } - switch (type) { - case ONE_DAY -> oneDayAvgPricesJson = result; - case THREE_DAY -> threeDayAvgPricesJson = result; - } - } + LOGGER.warn("[Skyblocker] Failed to download " + type + " prices!", e); - private static void downloadBazaarPrices() { - JsonObject result = null; - try { - URL apiAddr = new URL("https://hysky.de/api/bazaar"); - InputStreamReader reader = new InputStreamReader(apiAddr.openStream()); - result = new Gson().fromJson(reader, JsonObject.class); - } catch (IOException e) { - LOGGER.warn("[Skyblocker] Failed to download bazaar prices!", e); - } - bazaarPricesJson = result; - } + if (type.equals("lowest bin")) + lowestPricesJson = downloadPrices("lowest bin backup"); - private static void downloadLowestPrices() { - JsonObject result = null; - try { - URL apiAddr = new URL("https://lb.tricked.pro/lowestbins"); - InputStreamReader reader = new InputStreamReader(apiAddr.openStream()); - result = new Gson().fromJson(reader, JsonObject.class); - } catch (IOException e) { - LOGGER.warn("[Skyblocker] Failed to download lowest BIN prices from the main source!", e); - try { - URL apiAddr = new URL("https://lb2.tricked.pro/lowestbins"); - InputStreamReader reader = new InputStreamReader(apiAddr.openStream()); - result = new Gson().fromJson(reader, JsonObject.class); - } catch (IOException e2) { - LOGGER.warn("[Skyblocker] Failed to download lowest BIN prices from the backup source!", e2); - } + return null; } - lowestPricesJson = result; } - private static void downloadNPCPrices() { - JsonObject result = null; - try { - URL apiAddr = new URL("https://hysky.de/api/npcprice"); - InputStreamReader reader = new InputStreamReader(apiAddr.openStream()); - result = new Gson().fromJson(reader, JsonObject.class); - } catch (IOException e) { - LOGGER.warn("[Skyblocker] Failed to download NPC prices!", e); - } - npcPricesJson = result; + static { + apiAddresses = new HashMap<>(); + apiAddresses.put("1 day avg", "https://moulberry.codes/auction_averages_lbin/1day.json.gz"); + apiAddresses.put("3 day avg", "https://moulberry.codes/auction_averages_lbin/3day.json.gz"); + apiAddresses.put("bazaar", "https://hysky.de/api/bazaar"); + apiAddresses.put("lowest bin", "https://lb.tricked.pro/lowestbins"); + apiAddresses.put("lowest bin backup", "https://lb2.tricked.pro/lowestbins"); + apiAddresses.put("npc", "https://hysky.de/api/npcprice"); + apiAddresses.put("museum", "https://hysky.de/api/museum"); } - - private static void downloadIsMuseum() { - JsonObject result = null; - try { - URL apiAddr = new URL("https://hysky.de/api/museum"); - InputStreamReader reader = new InputStreamReader(apiAddr.openStream()); - result = new Gson().fromJson(reader, JsonObject.class); - } catch (IOException e) { - LOGGER.warn("[Skyblocker] Failed to download museum items!", e); - } - isMuseumJson = result; - } - } |