diff options
author | Trương Hoàng Long <longtruong2411@gmail.com> | 2022-07-29 19:08:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-29 19:08:32 +0200 |
commit | 023329936d43881959e37091776d0c01ba68a98c (patch) | |
tree | d001bb745c6449b0b86948728a542d7123df85ce /src | |
parent | 26e6a755c002a56db415d9b219ffafefbe384537 (diff) | |
parent | 8fb3881a48d1c90b7a8af43ec8af1545b81cae6b (diff) | |
download | Skyblocker-023329936d43881959e37091776d0c01ba68a98c.tar.gz Skyblocker-023329936d43881959e37091776d0c01ba68a98c.tar.bz2 Skyblocker-023329936d43881959e37091776d0c01ba68a98c.zip |
Merge pull request #76 from Fix3dll/master
code cleanup for PriceInfoTooltip
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java | 236 | ||||
-rw-r--r-- | src/main/resources/assets/skyblocker/lang/en_us.json | 1 |
2 files changed, 98 insertions, 139 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 ddac803e..e328c8c1 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java @@ -24,6 +24,7 @@ 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.concurrent.CompletableFuture; @@ -33,12 +34,12 @@ public class PriceInfoTooltip { private static final Logger LOGGER = LoggerFactory.getLogger(PriceInfoTooltip.class.getName()); private static final SkyblockerMod skyblocker = SkyblockerMod.getInstance(); private static final MinecraftClient client = MinecraftClient.getInstance(); - private static JsonObject npcPricesJson; - private static JsonObject bazaarPricesJson; - private static JsonObject oneDayAvgPricesJson; - private static JsonObject threeDayAvgPricesJson; - private static JsonObject lowestPricesJson; - private static JsonObject isMuseumJson; + private static JsonObject npcJson; + private static JsonObject bazaarJson; + private static JsonObject oneDayAvgJson; + private static JsonObject threeDayAvgJson; + private static JsonObject lowestBINJson; + private static JsonObject museumJson; private static boolean nullMsgSend = false; private final static Gson gson = new Gson(); @@ -50,39 +51,34 @@ public class PriceInfoTooltip { int count = stack.getCount(); String timestamp = getTimestamp(stack); - boolean bazaarOpened = lines.stream().anyMatch(each -> each.getString().contains("Buy price:") || each.getString().contains("Sell price:")); + 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(new TranslatableText("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (npcPricesJson.has(name)) { + if (npcJson == null) { + nullMessage(); + } else if (npcJson.has(name)) { lines.add(new LiteralText(String.format("%-21s", "NPC Price:")) .formatted(Formatting.YELLOW) - .append(getCoinsMessage(npcPricesJson.get(name).getAsDouble(), count))); + .append(getCoinsMessage(npcJson.get(name).getAsDouble(), count))); } } boolean bazaarExist = false; if (SkyblockerConfig.get().general.itemTooltip.enableBazaarPrice && !bazaarOpened) { - if (bazaarPricesJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(new TranslatableText("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (bazaarPricesJson.has(name)) { - JsonObject getItem = bazaarPricesJson.getAsJsonObject(name); + if (bazaarJson == null) { + nullMessage(); + } else if (bazaarJson.has(name)) { + JsonObject getItem = bazaarJson.getAsJsonObject(name); lines.add(new LiteralText(String.format("%-18s", "Bazaar buy Price:")) .formatted(Formatting.GOLD) .append(getItem.get("buyPrice").isJsonNull() - ? new LiteralText("No data").formatted(Formatting.RED) + ? new TranslatableText("skyblocker.itemTooltip.noData") : getCoinsMessage(getItem.get("buyPrice").getAsDouble(), count))); lines.add(new LiteralText(String.format("%-19s", "Bazaar sell Price:")) .formatted(Formatting.GOLD) .append(getItem.get("sellPrice").isJsonNull() - ? new LiteralText("No data").formatted(Formatting.RED) + ? new TranslatableText("skyblocker.itemTooltip.noData") : getCoinsMessage(getItem.get("sellPrice").getAsDouble(), count))); bazaarExist = true; } @@ -90,25 +86,19 @@ public class PriceInfoTooltip { // bazaarOpened & bazaarExist check for lbin, because Skytils keeps some bazaar item data in lbin api if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN && !bazaarOpened && !bazaarExist) { - if (lowestPricesJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(new TranslatableText("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (lowestPricesJson.has(name)) { + if (lowestBINJson == null) { + nullMessage(); + } else if (lowestBINJson.has(name)) { lines.add(new LiteralText(String.format("%-19s", "Lowest BIN Price:")) .formatted(Formatting.GOLD) - .append(getCoinsMessage(lowestPricesJson.get(name).getAsDouble(), count))); + .append(getCoinsMessage(lowestBINJson.get(name).getAsDouble(), count))); } } if (SkyblockerConfig.get().general.itemTooltip.enableAvgBIN) { - if (threeDayAvgPricesJson == null || oneDayAvgPricesJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(new TranslatableText("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (threeDayAvgPricesJson.has(name) || oneDayAvgPricesJson.has(name)) { + if (threeDayAvgJson == null || oneDayAvgJson == null) { + nullMessage(); + } else if (threeDayAvgJson.has(name) || oneDayAvgJson.has(name)) { /* We are skipping check average prices for potions and runes because there is no data for their in API. @@ -138,28 +128,25 @@ public class PriceInfoTooltip { if (!name.isEmpty() && (type == SkyblockerConfig.Average.ONE_DAY || type == SkyblockerConfig.Average.BOTH)) { lines.add(new LiteralText(String.format("%-19s", "1 Day Avg. Price:")) .formatted(Formatting.GOLD) - .append(oneDayAvgPricesJson.get(name) == null - ? new LiteralText("No data").formatted(Formatting.RED) - : getCoinsMessage(oneDayAvgPricesJson.get(name).getAsDouble(), count))); + .append(oneDayAvgJson.get(name) == null + ? new TranslatableText("skyblocker.itemTooltip.noData") + : getCoinsMessage(oneDayAvgJson.get(name).getAsDouble(), count))); } if (!name.isEmpty() && (type == SkyblockerConfig.Average.THREE_DAY || type == SkyblockerConfig.Average.BOTH)) { lines.add(new LiteralText(String.format("%-19s", "3 Day Avg. Price:")) .formatted(Formatting.GOLD) - .append(threeDayAvgPricesJson.get(name) == null - ? new LiteralText("No data").formatted(Formatting.RED) - : getCoinsMessage(threeDayAvgPricesJson.get(name).getAsDouble(), count))); + .append(threeDayAvgJson.get(name) == null + ? new TranslatableText("skyblocker.itemTooltip.noData") + : getCoinsMessage(threeDayAvgJson.get(name).getAsDouble(), count))); } } } if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate && !bazaarOpened) { - if (isMuseumJson == null) { - if (!nullMsgSend) { - client.player.sendMessage(new TranslatableText("skyblocker.itemTooltip.nullMessage"), false); - nullMsgSend = true; - } - } else if (isMuseumJson.has(name)) { - String itemCategory = isMuseumJson.get(name).toString().replaceAll("\"", ""); + if (museumJson == null) { + nullMessage(); + } else if (museumJson.has(name)) { + String itemCategory = museumJson.get(name).toString().replaceAll("\"", ""); String format = switch (itemCategory) { case "Weapons" -> "%-18s"; case "Armor" -> "%-19s"; @@ -176,6 +163,13 @@ public class PriceInfoTooltip { } } + public static void nullMessage() { + if (!nullMsgSend) { + client.player.sendMessage(new TranslatableText("skyblocker.itemTooltip.nullMessage"), false); + nullMsgSend = true; + } + } + public static NbtCompound getInternalNameForItem(ItemStack stack) { if (stack == null) return null; return stack.getNbt(); @@ -245,7 +239,8 @@ public class PriceInfoTooltip { 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") + internalName += "-" + ea.getString("potion").toUpperCase(Locale.ENGLISH) + + "-" + ea.getInt("potion_level") + enhanced + extended + splash; } } else if ("RUNE".equals(internalName)) { @@ -261,21 +256,24 @@ public class PriceInfoTooltip { } private static Text getCoinsMessage(double price, int count) { - if (count == 1) { - String priceString = String.format(Locale.ENGLISH, "%1$,.1f", price); - return new LiteralText(priceString + " Coins").formatted(Formatting.DARK_AQUA); - } else { - String priceString = String.format(Locale.ENGLISH, "%1$,.1f", price * count); - LiteralText priceText = (LiteralText) new LiteralText(priceString + " Coins ").formatted(Formatting.DARK_AQUA); - priceString = String.format(Locale.ENGLISH, "%1$,.1f", price); - LiteralText priceText2 = (LiteralText) new LiteralText( "(" + priceString + " each)").formatted(Formatting.GRAY); - return priceText.append(priceText2); + String slotPrice = String.format(Locale.ENGLISH, "%1$,.1f", price * count); + LiteralText slotText = (LiteralText) new LiteralText(slotPrice + " Coins ").formatted(Formatting.DARK_AQUA); + + if (count != 1) { + String unitPrice = String.format(Locale.ENGLISH, "%1$,.1f", price); + slotText.append(new LiteralText( "(" + unitPrice + " each)").formatted(Formatting.GRAY)); } + return slotText; } - // If these options is true beforehand, the client will get first data of these options while loading. - // After then, it will only fetch the data if it is on Skyblock. - public static int minute = -1; + private final static HashMap<String, String> downloadList; + + private static int minute = -1; + + /** + * If these options is true beforehand, the client will get first data of these options while loading. + * After then, it will only fetch the data if it is on Skyblock. + */ public static void init() { skyblocker.scheduler.scheduleCyclic(() -> { if (!Utils.isOnSkyblock && 0 < minute++) { @@ -284,105 +282,65 @@ public class PriceInfoTooltip { } List<CompletableFuture<Void>> futureList = new ArrayList<>(); - if ((SkyblockerConfig.get().general.itemTooltip.enableAvgBIN) && (oneDayAvgPricesJson == null || threeDayAvgPricesJson == null || minute % 5 == 0)) { + + if (SkyblockerConfig.get().general.itemTooltip.enableAvgBIN + && (oneDayAvgJson == null || threeDayAvgJson == null || minute % 5 == 0)) { 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))); - } + if (oneDayAvgJson == null || type == SkyblockerConfig.Average.ONE_DAY || type == SkyblockerConfig.Average.BOTH) + futureList.add(CompletableFuture.runAsync(() -> + oneDayAvgJson = download("average BIN prices", "1day.json.gz"))); + + if (threeDayAvgJson == null || type == SkyblockerConfig.Average.THREE_DAY || type == SkyblockerConfig.Average.BOTH) + futureList.add(CompletableFuture.runAsync(() -> + threeDayAvgJson = download("average BIN prices", "3day.json.gz"))); } if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN) { - futureList.add(CompletableFuture.runAsync(PriceInfoTooltip::downloadLowestPrices)); + futureList.add(CompletableFuture.runAsync(() -> + lowestBINJson = download("lowest BIN prices", null))); } if (SkyblockerConfig.get().general.itemTooltip.enableBazaarPrice) { - futureList.add(CompletableFuture.runAsync(PriceInfoTooltip::downloadBazaarPrices)); + futureList.add(CompletableFuture.runAsync(() -> + bazaarJson = download("bazaar prices", null))); } - if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice && npcPricesJson == null) { - futureList.add(CompletableFuture.runAsync(PriceInfoTooltip::downloadNPCPrices)); + if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice && npcJson == null) { + futureList.add(CompletableFuture.runAsync(() -> + npcJson = download("NPC prices", null))); } - if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate && isMuseumJson == null) { - futureList.add(CompletableFuture.runAsync(PriceInfoTooltip::downloadIsMuseum)); + if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate && museumJson == null) { + futureList.add(CompletableFuture.runAsync(() -> + museumJson = download("museum items", null))); } + 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 download(String key, String avgType) { + String rest = avgType == null ? "" : avgType; try { - URL apiAddr = new URL("https://moulberry.codes/auction_averages_lbin/" + avgDay); + URL apiAddr = new URL(downloadList.get(key).concat(rest)); try (InputStream src = apiAddr.openStream()) { - try (GZIPInputStream gzipOutput = new GZIPInputStream(src)) { - try (InputStreamReader reader = new InputStreamReader(gzipOutput)) { - result = new Gson().fromJson(reader, JsonObject.class); - } + GZIPInputStream gzipOutput = avgType == null ? null : new GZIPInputStream(src); + try (InputStreamReader reader = new InputStreamReader(avgType == null ? src : gzipOutput)) { + return new Gson().fromJson(reader, JsonObject.class); } } } catch (IOException e) { - LOGGER.warn("[Skyblocker] Failed to download average BIN prices!", e); + LOGGER.warn("[Skyblocker] Failed to download " + key + "!", e); + return null; } - switch (type) { - case ONE_DAY -> oneDayAvgPricesJson = result; - case THREE_DAY -> threeDayAvgPricesJson = result; - } - } - - 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; } - private static void downloadLowestPrices() { - JsonObject result = null; - try { - URL apiAddr = new URL("https://skytils.gg/api/auctions/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!", e); - } - 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 { + downloadList = new HashMap<>(); + downloadList.put("average BIN prices", "https://moulberry.codes/auction_averages_lbin/"); + downloadList.put("lowest BIN prices", "https://skytils.gg/api/auctions/lowestbins"); + downloadList.put("bazaar prices", "https://hysky.de/api/bazaar"); + downloadList.put("NPC prices", "https://hysky.de/api/npcprice"); + downloadList.put("museum items", "https://hysky.de/api/museum"); } -} +}
\ No newline at end of file diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index 0c3899d4..80684553 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -28,6 +28,7 @@ "text.autoconfig.skyblocker.option.general.hitbox.oldFarmlandHitbox": "Enable 1.8 farmland hitbox", "text.autoconfig.skyblocker.option.general.hitbox.oldLeverHitbox": "Enable 1.8 lever hitbox", "skyblocker.itemTooltip.nullMessage": "§b[§6Skyblocker§b] §cItem price information on tooltip will renew in max 60 seconds. If not, check latest.log", + "skyblocker.itemTooltip.noData": "§cNo Data", "text.autoconfig.skyblocker.category.richPresence": "Discord Rich Presence", "text.autoconfig.skyblocker.option.richPresence.info": "Skyblock Info", |