From 9f8867018ef7232c1e17af54c9818ddcafa26759 Mon Sep 17 00:00:00 2001 From: Lulonaut <67191924+Lulonaut@users.noreply.github.com> Date: Sat, 11 Dec 2021 05:13:04 +0100 Subject: Store auctionable items locally to only show price warning if the item can actually have a price (#29) --- .../notenoughupdates/ItemPriceInformation.java | 46 +++++++++++++++++++++- .../notenoughupdates/NotEnoughUpdates.java | 1 + .../notenoughupdates/auction/APIManager.java | 6 +++ 3 files changed, 51 insertions(+), 2 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/ItemPriceInformation.java b/src/main/java/io/github/moulberry/notenoughupdates/ItemPriceInformation.java index b563906e..43689d3a 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/ItemPriceInformation.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/ItemPriceInformation.java @@ -1,5 +1,6 @@ package io.github.moulberry.notenoughupdates; +import com.google.gson.Gson; import com.google.gson.JsonObject; import io.github.moulberry.notenoughupdates.auction.APIManager; import io.github.moulberry.notenoughupdates.core.config.KeybindHelper; @@ -10,16 +11,46 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumChatFormatting; import org.lwjgl.input.Keyboard; +import java.io.*; +import java.nio.charset.StandardCharsets; import java.text.NumberFormat; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Set; public class ItemPriceInformation { + private static File file; + private static HashSet auctionableItems = null; + private static Gson gson; public static boolean addToTooltip(List tooltip, String internalname, ItemStack stack) { return addToTooltip(tooltip, internalname, stack, true); } + public static void init(File saveLocation, Gson neuGson) { + file = saveLocation; + gson = neuGson; + if (file.exists()) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { + auctionableItems = gson.fromJson(reader, HashSet.class); + } catch (Exception ignored) {} + } + } + + public static void updateAuctionableItemsList() { + Set items = NotEnoughUpdates.INSTANCE.manager.auctionManager.getItemAuctionInfoKeySet(); + if (!items.isEmpty()) { + auctionableItems = (HashSet) items; + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) { + //noinspection ResultOfMethodCallIgnored + file.createNewFile(); + writer.write(gson.toJson(items)); + } catch (IOException ignored) { + } + } + } + public static boolean addToTooltip(List tooltip, String internalname, ItemStack stack, boolean useStackSize) { if (stack.getTagCompound().hasKey("disableNeuTooltip") && stack.getTagCompound().getBoolean("disableNeuTooltip")) { return false; @@ -27,6 +58,9 @@ public class ItemPriceInformation { if (NotEnoughUpdates.INSTANCE.config.tooltipTweaks.disablePriceKey && !KeybindHelper.isKeyDown(NotEnoughUpdates.INSTANCE.config.tooltipTweaks.disablePriceKeyKeybind)) { return false; } + if (internalname.equals("SKYBLOCK_MENU")) { + return false; + } JsonObject auctionInfo = NotEnoughUpdates.INSTANCE.manager.auctionManager.getItemAuctionInfo(internalname); JsonObject bazaarInfo = NotEnoughUpdates.INSTANCE.manager.auctionManager.getBazaarInfo(internalname); float lowestBinAvg = NotEnoughUpdates.INSTANCE.manager.auctionManager.getItemAvgBin(internalname); @@ -256,8 +290,16 @@ public class ItemPriceInformation { return added; } else if (auctionInfoErrored) { - tooltip.add(EnumChatFormatting.RED.toString() + EnumChatFormatting.BOLD + "[NEU] Can't find price info! Please be patient."); - return true; + String message = EnumChatFormatting.RED.toString() + EnumChatFormatting.BOLD + "[NEU] API is down"; + if (auctionableItems != null && !auctionableItems.isEmpty()) { + if (auctionableItems.contains(internalname)) { + tooltip.add(message); + return true; + } + } else { + tooltip.add(message + " and no item data is cached"); + return true; + } } return false; diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java b/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java index 29eaf38e..c1e1cd0d 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java @@ -127,6 +127,7 @@ public class NotEnoughUpdates { FairySouls.load(new File(neuDir, "collected_fairy_souls.json"), gson); PetInfoOverlay.loadConfig(new File(neuDir, "petCache.json")); SlotLocking.getInstance().loadConfig(new File(neuDir, "slotLocking.json")); + ItemPriceInformation.init(new File(neuDir, "auctionable_items.json"), gson); if (config == null) { config = new NEUConfig(); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java b/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java index 70b9e312..6726093d 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/auction/APIManager.java @@ -3,6 +3,7 @@ package io.github.moulberry.notenoughupdates.auction; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import io.github.moulberry.notenoughupdates.ItemPriceInformation; import io.github.moulberry.notenoughupdates.NEUManager; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; import io.github.moulberry.notenoughupdates.util.Constants; @@ -50,6 +51,7 @@ public class APIManager { public TreeMap>> extrasToAucIdMap = new TreeMap<>(); + private boolean didFirstUpdate = false; private long lastAuctionUpdate = 0; private long lastShortAuctionUpdate = 0; private long lastCustomAHSearch = 0; @@ -256,6 +258,10 @@ public class APIManager { for (Map.Entry entry : jsonObject.entrySet()) { lowestBins.add(entry.getKey(), entry.getValue()); } + if (!didFirstUpdate) { + ItemPriceInformation.updateAuctionableItemsList(); + didFirstUpdate = true; + } }, () -> {}); } -- cgit