diff options
author | Fix3dll <serhanduzce@gmail.com> | 2022-02-21 02:49:56 +0300 |
---|---|---|
committer | Fix3dll <serhanduzce@gmail.com> | 2022-02-21 02:49:56 +0300 |
commit | 3bbf5f7ce3acfa6fa4bfe969a6f8125760618ad9 (patch) | |
tree | c1fb41aba62b2b2d56bf5c252cbaf05748913262 /src | |
parent | e02a38ae9f384776c227ef845ef6b1696938d22a (diff) | |
download | Skyblocker-3bbf5f7ce3acfa6fa4bfe969a6f8125760618ad9.tar.gz Skyblocker-3bbf5f7ce3acfa6fa4bfe969a6f8125760618ad9.tar.bz2 Skyblocker-3bbf5f7ce3acfa6fa4bfe969a6f8125760618ad9.zip |
little fix for other items,
change TimerTask to Scheduler
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java | 162 | ||||
-rw-r--r-- | src/main/resources/assets/skyblocker/lang/en_us.json | 1 |
2 files changed, 95 insertions, 68 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 daad1d26..b48bcbc9 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java @@ -2,6 +2,7 @@ package me.xmrvizzy.skyblocker.skyblock.item; import com.google.gson.Gson; import com.google.gson.JsonObject; +import me.xmrvizzy.skyblocker.SkyblockerMod; import me.xmrvizzy.skyblocker.config.SkyblockerConfig; import net.minecraft.client.MinecraftClient; import net.minecraft.client.item.TooltipContext; @@ -9,6 +10,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; @@ -20,105 +22,113 @@ import java.net.URL; import java.time.Month; import java.util.List; import java.util.Locale; -import java.util.Timer; -import java.util.TimerTask; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import java.util.zip.GZIPInputStream; public class PriceInfoTooltip { + private static final SkyblockerMod skyblocker = SkyblockerMod.getInstance(); private static JsonObject npcPricesJson; private static JsonObject bazaarPricesJson; private static JsonObject avgPricesJson; private static JsonObject lowestPricesJson; private static JsonObject isMuseumJson; + private static boolean nullMsgSend = false; + private final static Gson gson = new Gson(); public static void onInjectTooltip(ItemStack stack, TooltipContext context, List<Text> lines) { int count = stack.getCount(); String name = getInternalNameFromNBT(stack); String timestamp = getTimestamp(stack); List<String> listString = lines.stream() - .map(Text::getString) - .collect(Collectors.toList()); + .map(Text::getString).toList(); try { - if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice - && !listString.contains("NPC Price") && npcPricesJson != null && npcPricesJson.has(name)) { - - lines.add(new LiteralText(String.format("%-21s", "NPC Price:")) - .formatted(Formatting.YELLOW) - .append(getCoinsMessage(npcPricesJson.get(name).getAsDouble(), count))); + if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice && !listString.contains("NPC Price")) { + if (npcPricesJson.has(name)) { + lines.add(new LiteralText(String.format("%-21s", "NPC Price:")) + .formatted(Formatting.YELLOW) + .append(getCoinsMessage(npcPricesJson.get(name).getAsDouble(), count))); + } } - if ((!listString.contains("Avg. BIN Price") && avgPricesJson != null && avgPricesJson.has(name)) - || (!listString.contains("Lowest BIN Price") && lowestPricesJson != null && lowestPricesJson.has(name))) { + if ((!listString.contains("Avg. BIN Price") && avgPricesJson.has(name)) + || (!listString.contains("Lowest BIN Price") && lowestPricesJson.has(name))) { - if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN) { + if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN && lowestPricesJson.has(name)) { lines.add(new LiteralText(String.format("%-19s", "Lowest BIN Price:")) .formatted(Formatting.GOLD) .append(getCoinsMessage(lowestPricesJson.get(name).getAsDouble(), count))); } - // Change format from Skytils to Moulberry's for Avg. BIN if (name.contains("PET-")) { name = name.replace("PET-", "") - .replace("0", "COMMON") - .replace("1", "UNCOMMON") - .replace("2", "RARE") - .replace("3", "EPIC") - .replace("4", "LEGENDARY") - .replace("5", "MYTHIC") + .replace("COMMON", "0") + .replace("UNCOMMON", "1") + .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 = "POTION"; + } else if (name.contains("RUNE-")) { + name = "RUNE"; } else { name = name.replace(":", "-"); } - // has(name) check because Skytils keeps old data but Moulberry not if (SkyblockerConfig.get().general.itemTooltip.enableAvgBIN && avgPricesJson.has(name)) { lines.add(new LiteralText(String.format("%-21s", "Avg. BIN Price:")) .formatted(Formatting.GOLD) .append(getCoinsMessage(avgPricesJson.get(name).getAsDouble(), count))); } } else if (SkyblockerConfig.get().general.itemTooltip.enableBazaarPrice - && (!listString.contains("Bazaar buy Price") || !listString.contains("Bazaar sell Price")) - && bazaarPricesJson != null && bazaarPricesJson.has(name)) { + && !listString.contains("Bazaar buy Price") || !listString.contains("Bazaar sell Price")) { - JsonObject getItem = bazaarPricesJson.getAsJsonObject(name); - lines.add(new LiteralText(String.format("%-18s", "Bazaar buy Price:")) - .formatted(Formatting.GOLD) - .append(getCoinsMessage(getItem.get("buyPrice").getAsDouble(), count))); - lines.add(new LiteralText(String.format("%-19s", "Bazaar sell Price:")) - .formatted(Formatting.GOLD) - .append(getCoinsMessage(getItem.get("sellPrice").getAsDouble(), count))); + if (bazaarPricesJson.has(name)) { + JsonObject getItem = bazaarPricesJson.getAsJsonObject(name); + lines.add(new LiteralText(String.format("%-18s", "Bazaar buy Price:")) + .formatted(Formatting.GOLD) + .append(getCoinsMessage(getItem.get("buyPrice").getAsDouble(), count))); + lines.add(new LiteralText(String.format("%-19s", "Bazaar sell Price:")) + .formatted(Formatting.GOLD) + .append(getCoinsMessage(getItem.get("sellPrice").getAsDouble(), count))); + } } - if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate - && !listString.contains("Museum") && isMuseumJson != null && isMuseumJson.has(name)) { - - String itemCategory = isMuseumJson.get(name).toString().replaceAll("\"", ""); - String format = switch (itemCategory) { - case "Weapons" -> "%-18s"; - case "Armor" -> "%-19s"; - default -> "%-20s"; - }; - - lines.add(new LiteralText(String.format(format, "Museum: (" + itemCategory + ")")) - .formatted(Formatting.LIGHT_PURPLE) - .append(new LiteralText(timestamp != null ? timestamp : "").formatted(Formatting.RED))); - } else if (!listString.contains("Obtained") && timestamp != null - && SkyblockerConfig.get().general.itemTooltip.enableMuseumDate) { - + if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate && !listString.contains("Museum")) { + 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(new LiteralText(String.format(format, "Museum: (" + itemCategory + ")")) + .formatted(Formatting.LIGHT_PURPLE) + .append(new LiteralText(timestamp != null ? timestamp : "").formatted(Formatting.RED))); + } + } else if (!listString.contains("Obtained") && timestamp != null && SkyblockerConfig.get().general.itemTooltip.enableMuseumDate) { lines.add(new LiteralText(String.format("%-22s", "Obtained: ")) .formatted(Formatting.LIGHT_PURPLE) .append(new LiteralText(timestamp).formatted(Formatting.RED))); } - } catch (Exception e) { + } catch (NullPointerException ex) { + if ((npcPricesJson == null || bazaarPricesJson == null || avgPricesJson == null || lowestPricesJson == null || isMuseumJson == null) + && !nullMsgSend) { + MinecraftClient.getInstance().player.sendMessage(new TranslatableText("skyblocker.itemTooltip.NullMessage"), false); + nullMsgSend = true; + skyblocker.scheduler.schedule(() -> nullMsgSend = false, 1200); + } + } catch (Exception ex) { if (MinecraftClient.getInstance().player == null) { throw new RuntimeException("[Skyblocker] client.player cannot be null!"); } - MinecraftClient.getInstance().player.sendMessage(new LiteralText(e.toString()), false); + MinecraftClient.getInstance().player.sendMessage(new LiteralText(ex.toString()), false); } } @@ -156,13 +166,32 @@ public class PriceInfoTooltip { } if ("ENCHANTED_BOOK".equals(internalName)) { - NbtCompound enchants = ea.getCompound("enchantments"); - - for (String enchName : enchants.getKeys()) { - internalName += "-" + enchName.toUpperCase() + "-" + enchants.getInt(enchName); - break; + if (ea.contains("enchantments")) { + NbtCompound enchants = ea.getCompound("enchantments"); + String enchant = enchants.getKeys().stream().findFirst().get(); + internalName += "-" + enchant.toUpperCase() + "-" + 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() + "-" + 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() + "-" + runes.getInt(rune); } } + } return internalName; } @@ -182,24 +211,21 @@ public class PriceInfoTooltip { public static boolean firstRun = true; public static void init() { - TimerTask repeatedTask = new TimerTask() { - public void run() { - if (SkyblockerConfig.get().general.itemTooltip.enableAvgBIN || PriceInfoTooltip.firstRun) + skyblocker.scheduler.scheduleCyclic(() -> { + { + if (SkyblockerConfig.get().general.itemTooltip.enableAvgBIN || firstRun) CompletableFuture.runAsync(PriceInfoTooltip::downloadAvgPrices); - if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN || PriceInfoTooltip.firstRun) + if (SkyblockerConfig.get().general.itemTooltip.enableLowestBIN || firstRun) CompletableFuture.runAsync(PriceInfoTooltip::downloadLowestPrices); - if (SkyblockerConfig.get().general.itemTooltip.enableBazaarPrice || PriceInfoTooltip.firstRun) + if (SkyblockerConfig.get().general.itemTooltip.enableBazaarPrice || firstRun) CompletableFuture.runAsync(PriceInfoTooltip::downloadBazaarPrices); - if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice || PriceInfoTooltip.firstRun) + if (SkyblockerConfig.get().general.itemTooltip.enableNPCPrice || firstRun) CompletableFuture.runAsync(PriceInfoTooltip::downloadNPCPrices); - if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate || PriceInfoTooltip.firstRun) + if (SkyblockerConfig.get().general.itemTooltip.enableMuseumDate || firstRun) CompletableFuture.runAsync(PriceInfoTooltip::downloadIsMuseum); } - }; - + }, 1200); firstRun = false; - Timer timer = new Timer("PriceInfoDownloader"); - timer.scheduleAtFixedRate(repeatedTask, 0L, 1000L * 60L); } private static void downloadAvgPrices() { @@ -214,7 +240,7 @@ public class PriceInfoTooltip { } } } catch (IOException e) { - LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download auction item prices!", e); + LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download average BIN prices!", e); } avgPricesJson = result; } @@ -226,7 +252,7 @@ public class PriceInfoTooltip { InputStreamReader reader = new InputStreamReader(apiAddr.openStream()); result = new Gson().fromJson(reader, JsonObject.class); } catch (IOException e) { - LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download bazaar item prices!", e); + LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download bazaar prices!", e); } bazaarPricesJson = result; } @@ -238,7 +264,7 @@ public class PriceInfoTooltip { InputStreamReader reader = new InputStreamReader(apiAddr.openStream()); result = new Gson().fromJson(reader, JsonObject.class); } catch (IOException e) { - LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download lb item prices!", e); + LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download lowest BIN prices!", e); } lowestPricesJson = result; } @@ -250,7 +276,7 @@ public class PriceInfoTooltip { InputStreamReader reader = new InputStreamReader(apiAddr.openStream()); result = new Gson().fromJson(reader, JsonObject.class); } catch (IOException e) { - LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download shop item prices!", e); + LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download NPC prices!", e); } npcPricesJson = result; } diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index 52a7b441..11944a81 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -16,6 +16,7 @@ "text.autoconfig.skyblocker.option.general.itemTooltip.enableLowestBIN": "Enable Lowest BIN Price", "text.autoconfig.skyblocker.option.general.itemTooltip.enableBazaarPrice": "Enable Bazaar buy/sell Price", "text.autoconfig.skyblocker.option.general.itemTooltip.enableMuseumDate": "Enable Museum & Date", + "skyblocker.itemTooltip.NullMessage": "§b[§6Skyblocker§b] §cItem price info on tooltip will renew in max 60 seconds.", "text.autoconfig.skyblocker.category.richPresence": "Discord Rich Presence", "text.autoconfig.skyblocker.option.richPresence.info": "Skyblock Info", |