diff options
author | Kevin <92656833+kevinthegreat1@users.noreply.github.com> | 2023-05-17 16:42:29 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-17 16:42:29 -0700 |
commit | 33b467fa42d4bc6bb841512ae839b678297d920a (patch) | |
tree | 1a569075ace64e29c30559928bf9a948eb7d149b /src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java | |
parent | d6caa2fe1a6f792c3ea310d2b5e76dfa25e27ec4 (diff) | |
parent | 02b6c6e8ae49e888605964ac8a70b2eaaac7b50d (diff) | |
download | Skyblocker-33b467fa42d4bc6bb841512ae839b678297d920a.tar.gz Skyblocker-33b467fa42d4bc6bb841512ae839b678297d920a.tar.bz2 Skyblocker-33b467fa42d4bc6bb841512ae839b678297d920a.zip |
Merge branch 'master' into master
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java')
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java | 363 |
1 files changed, 166 insertions, 197 deletions
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 8d8c86b4..d9263f9a 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java @@ -23,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; @@ -40,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; @@ -48,16 +51,13 @@ public class PriceInfoTooltip { if (name == null) return; int count = stack.getCount(); - String timestamp = getTimestamp(stack); boolean bazaarOpened = lines.stream().anyMatch(each -> each.getString().contains("Buy price:") || each.getString().contains("Sell price:")); if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice) { if (npcPricesJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(Text.translatable("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (npcPricesJson.has(name)) { + nullWarning(); + } + else if (npcPricesJson.has(name)) { lines.add(Text.literal(String.format("%-21s", "NPC Price:")) .formatted(Formatting.YELLOW) .append(getCoinsMessage(npcPricesJson.get(name).getAsDouble(), count))); @@ -67,11 +67,9 @@ public class PriceInfoTooltip { boolean bazaarExist = false; if (SkyblockerConfig.get().general.itemTooltip.enableBazaarPrice && !bazaarOpened) { if (bazaarPricesJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(Text.translatable("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (bazaarPricesJson.has(name)) { + nullWarning(); + } + else if (bazaarPricesJson.has(name)) { JsonObject getItem = bazaarPricesJson.getAsJsonObject(name); lines.add(Text.literal(String.format("%-18s", "Bazaar buy Price:")) .formatted(Formatting.GOLD) @@ -88,101 +86,115 @@ public class PriceInfoTooltip { } // bazaarOpened & bazaarExist check for lbin, because Skytils keeps some bazaar item data in lbin api + boolean lbinExist = false; if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN && !bazaarOpened && !bazaarExist) { if (lowestPricesJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(Text.translatable("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (lowestPricesJson.has(name)) { + nullWarning(); + } + else if (lowestPricesJson.has(name)) { lines.add(Text.literal(String.format("%-19s", "Lowest BIN Price:")) .formatted(Formatting.GOLD) .append(getCoinsMessage(lowestPricesJson.get(name).getAsDouble(), count))); + lbinExist = true; } } if (SkyblockerConfig.get().general.itemTooltip.enableAvgBIN) { if (threeDayAvgPricesJson == null || oneDayAvgPricesJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(Text.translatable("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (threeDayAvgPricesJson.has(name) || oneDayAvgPricesJson.has(name)) { + nullWarning(); + } + else { /* - We are skipping check average prices for potions and runes - because there is no data for their in API. + We are skipping check average prices for potions, runes + and enchanted books because there is no data for their in API. */ if (name.contains("PET-")) { name = name.replace("PET-", "") - .replace("COMMON", "0") .replace("UNCOMMON", "1") + .replace("COMMON", "0") .replace("RARE", "2") .replace("EPIC", "3") .replace("LEGENDARY", "4") .replace("MYTHIC", "5") .replace("-", ";"); - } else if (name.contains("ENCHANTED_BOOK-")) { - name = name.replace("ENCHANTED_BOOK-", "").replace("-", ";"); - } else if (name.contains("POTION-")) { - name = ""; } else if (name.contains("RUNE-")) { + name = name.replace("RUNE-", ""); + name = name.substring(0, name.indexOf("-")) + "_RUNE;" + name.substring(name.lastIndexOf("-") + 1); + } else if (name.contains("POTION-") || name.contains("ENCHANTED_BOOK-")) { name = ""; } else { name = name.replace(":", "-"); } - SkyblockerConfig.Average type = SkyblockerConfig.get().general.itemTooltip.avg; + if (!name.isEmpty() && lbinExist) { + SkyblockerConfig.Average type = SkyblockerConfig.get().general.itemTooltip.avg; - // "No data" line because of API not keeping old data, it causes NullPointerException - if (!name.isEmpty() && (type == SkyblockerConfig.Average.ONE_DAY || type == SkyblockerConfig.Average.BOTH)) { - lines.add(Text.literal(String.format("%-19s", "1 Day Avg. Price:")) - .formatted(Formatting.GOLD) - .append(oneDayAvgPricesJson.get(name) == null - ? Text.literal("No data").formatted(Formatting.RED) - : getCoinsMessage(oneDayAvgPricesJson.get(name).getAsDouble(), count))); - } - if (!name.isEmpty() && (type == SkyblockerConfig.Average.THREE_DAY || type == SkyblockerConfig.Average.BOTH)) { - lines.add(Text.literal(String.format("%-19s", "3 Day Avg. Price:")) - .formatted(Formatting.GOLD) - .append(threeDayAvgPricesJson.get(name) == null - ? Text.literal("No data").formatted(Formatting.RED) - : getCoinsMessage(threeDayAvgPricesJson.get(name).getAsDouble(), count))); + // "No data" line because of API not keeping old data, it causes NullPointerException + if (type == SkyblockerConfig.Average.ONE_DAY || type == SkyblockerConfig.Average.BOTH) { + lines.add( + Text.literal(String.format("%-19s", "1 Day Avg. Price:")) + .formatted(Formatting.GOLD) + .append(oneDayAvgPricesJson.get(name) == null + ? Text.literal("No data").formatted(Formatting.RED) + : getCoinsMessage(oneDayAvgPricesJson.get(name).getAsDouble(), count) + ) + ); + } + if (type == SkyblockerConfig.Average.THREE_DAY || type == SkyblockerConfig.Average.BOTH) { + lines.add( + Text.literal(String.format("%-19s", "3 Day Avg. Price:")) + .formatted(Formatting.GOLD) + .append(threeDayAvgPricesJson.get(name) == null + ? Text.literal("No data").formatted(Formatting.RED) + : getCoinsMessage(threeDayAvgPricesJson.get(name).getAsDouble(), count) + ) + ); + } } } } if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate && !bazaarOpened) { if (isMuseumJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(Text.translatable("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; + nullWarning(); + } + else { + String timestamp = getTimestamp(stack); + + if (isMuseumJson.has(name)) { + String itemCategory = isMuseumJson.get(name).toString().replaceAll("\"", ""); + String format = switch (itemCategory) { + case "Weapons" -> "%-18s"; + case "Armor" -> "%-19s"; + default -> "%-20s"; + }; + lines.add(Text.literal(String.format(format, "Museum: (" + itemCategory + ")")) + .formatted(Formatting.LIGHT_PURPLE) + .append(Text.literal(timestamp).formatted(Formatting.RED))); + } else if (!timestamp.isEmpty()) { + lines.add(Text.literal(String.format("%-21s", "Obtained: ")) + .formatted(Formatting.LIGHT_PURPLE) + .append(Text.literal(timestamp).formatted(Formatting.RED))); } - } else if (isMuseumJson.has(name)) { - String itemCategory = isMuseumJson.get(name).toString().replaceAll("\"", ""); - String format = switch (itemCategory) { - case "Weapons" -> "%-18s"; - case "Armor" -> "%-19s"; - default -> "%-20s"; - }; - lines.add(Text.literal(String.format(format, "Museum: (" + itemCategory + ")")) - .formatted(Formatting.LIGHT_PURPLE) - .append(Text.literal(timestamp != null ? timestamp : "").formatted(Formatting.RED))); - } else if (timestamp != null) { - lines.add(Text.literal(String.format("%-21s", "Obtained: ")) - .formatted(Formatting.LIGHT_PURPLE) - .append(Text.literal(timestamp).formatted(Formatting.RED))); } } } + + private static void nullWarning() { + if (!nullMsgSend && client.player != null) { + client.player.sendMessage(Text.translatable("skyblocker.itemTooltip.nullMessage"), false); + nullMsgSend = true; + } + } - public static NbtCompound getInternalNameForItem(ItemStack stack) { + public static NbtCompound getItemNBT(ItemStack stack) { if (stack == null) return null; return stack.getNbt(); } /** * 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 @@ -192,83 +204,89 @@ 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); - String internalName = null; + NbtCompound tag = getItemNBT(stack); + if (tag != null && tag.contains("ExtraAttributes", 10)) { NbtCompound ea = tag.getCompound("ExtraAttributes"); if (ea.contains("timestamp", 8)) { - internalName = ea.getString("timestamp"); - SimpleDateFormat dt = new SimpleDateFormat("MM/dd/yy"); + SimpleDateFormat nbtFormat = new SimpleDateFormat("MM/dd/yy"); try { - Date date = dt.parse(internalName); - SimpleDateFormat dt1 = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH); - internalName = dt1.format(date); + Date date = nbtFormat.parse(ea.getString("timestamp")); + SimpleDateFormat skyblockerFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH); + return skyblockerFormat.format(date); } catch (ParseException e) { LOGGER.warn("[Skyblocker-tooltip] getTimestamp", e); } } } - return internalName; + + return ""; } public static String getInternalNameFromNBT(ItemStack stack) { - NbtCompound tag = getInternalNameForItem(stack); - String internalName = null; + NbtCompound tag = getItemNBT(stack); if (tag != null && tag.contains("ExtraAttributes", 10)) { NbtCompound ea = tag.getCompound("ExtraAttributes"); if (ea.contains("id", 8)) { - internalName = ea.getString("id"); - } else { - return null; - } + String internalName = ea.getString("id"); - if ("ENCHANTED_BOOK".equals(internalName)) { - if (ea.contains("enchantments")) { - NbtCompound enchants = ea.getCompound("enchantments"); - String enchant = enchants.getKeys().stream().findFirst().get(); - internalName += "-" + enchant.toUpperCase(Locale.ENGLISH) + "-" + enchants.getInt(enchant); - } - } else if ("PET".equals(internalName)) { - if (ea.contains("petInfo")) { - JsonObject petInfo = gson.fromJson(ea.getString("petInfo"), JsonObject.class); - internalName += "-" + petInfo.get("type").getAsString() + "-" + petInfo.get("tier").getAsString(); - } - } else if ("POTION".equals(internalName)) { - String enhanced = ea.contains("enhanced") ? "-ENHANCED" : ""; - String extended = ea.contains("extended") ? "-EXTENDED" : ""; - String splash = ea.contains("splash") ? "-SPLASH" : ""; - if (ea.contains("potion") && ea.contains("potion_level")) { - internalName += "-" + ea.getString("potion").toUpperCase(Locale.ENGLISH) + "-" + ea.getInt("potion_level") - + enhanced + extended + splash; - } - } else if ("RUNE".equals(internalName)) { - if (ea.contains("runes")) { - NbtCompound runes = ea.getCompound("runes"); - String rune = runes.getKeys().stream().findFirst().get(); - internalName += "-" + rune.toUpperCase(Locale.ENGLISH) + "-" + runes.getInt(rune); + // Transformation to API format. + if ("ENCHANTED_BOOK".equals(internalName)) { + if (ea.contains("enchantments")) { + NbtCompound enchants = ea.getCompound("enchantments"); + String enchant = enchants.getKeys().stream().findFirst().get(); + return internalName + "-" + enchant.toUpperCase(Locale.ENGLISH) + "-" + enchants.getInt(enchant); + } + } else if ("PET".equals(internalName)) { + if (ea.contains("petInfo")) { + JsonObject petInfo = gson.fromJson(ea.getString("petInfo"), JsonObject.class); + return internalName + "-" + petInfo.get("type").getAsString() + "-" + petInfo.get("tier").getAsString(); + } + } else if ("POTION".equals(internalName)) { + // New API just contains 'enhanced' tag. + String enhanced = ea.contains("enhanced") ? "-ENHANCED" : ""; + //String extended = ea.contains("extended") ? "-EXTENDED" : ""; + //String splash = ea.contains("splash") ? "-SPLASH" : ""; + if (ea.contains("potion") && ea.contains("potion_level")) { + return internalName + "-" + ea.getString("potion").toUpperCase(Locale.ENGLISH) + "-" + ea.getInt("potion_level") + + enhanced; //+ extended + splash; + } + } else if ("RUNE".equals(internalName)) { + if (ea.contains("runes")) { + NbtCompound runes = ea.getCompound("runes"); + String rune = runes.getKeys().stream().findFirst().get(); + return internalName + "-" + rune.toUpperCase(Locale.ENGLISH) + "-" + runes.getInt(rune); + } } - } + return internalName; + } + else + return null; } - return internalName; + else + return null; } private static Text getCoinsMessage(double price, int count) { if (count == 1) { String priceString = String.format(Locale.ENGLISH, "%1$,.1f", price); return Text.literal(priceString + " Coins").formatted(Formatting.DARK_AQUA); - } else { - String priceString = String.format(Locale.ENGLISH, "%1$,.1f", price * count); - MutableText priceText = Text.literal(priceString + " Coins ").formatted(Formatting.DARK_AQUA); - priceString = String.format(Locale.ENGLISH, "%1$,.1f", price); - MutableText priceText2 = Text.literal( "(" + priceString + " each)").formatted(Formatting.GRAY); - return priceText.append(priceText2); + } + else { + String priceStringTotal = String.format(Locale.ENGLISH, "%1$,.1f", price * count); + MutableText priceTextTotal = Text.literal(priceStringTotal + " Coins ").formatted(Formatting.DARK_AQUA); + + String priceStringEach = String.format(Locale.ENGLISH, "%1$,.1f", price); + MutableText priceTextEach = Text.literal( "(" + priceStringEach + " each)").formatted(Formatting.GRAY); + + return priceTextTotal.append(priceTextEach); } } @@ -287,108 +305,59 @@ 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 bins"))); + + 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 bins")) + lowestPricesJson = downloadPrices("lowest bins 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; - } - - 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; + 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 bins", "https://lb.tricked.pro/lowestbins"); + apiAddresses.put("lowest bins backup", "https://lb2.tricked.pro/lowestbins"); + apiAddresses.put("npc", "https://hysky.de/api/npcprice"); + apiAddresses.put("museum", "https://hysky.de/api/museum"); } - } |