diff --git a/build.gradle.kts b/build.gradle.kts index a003f1e..9330f4b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "dev.meyi.bazaarnotifier" -version = "1.6.0" +version = "1.6.1" java { toolchain.languageVersion.set(JavaLanguageVersion.of(8)) diff --git a/src/main/java/dev/meyi/bn/BazaarNotifier.java b/src/main/java/dev/meyi/bn/BazaarNotifier.java index cf43e45..5fd11bd 100644 --- a/src/main/java/dev/meyi/bn/BazaarNotifier.java +++ b/src/main/java/dev/meyi/bn/BazaarNotifier.java @@ -16,6 +16,7 @@ import dev.meyi.bn.json.Order; import dev.meyi.bn.json.resp.BazaarResponse; import dev.meyi.bn.modules.ModuleList; +import dev.meyi.bn.modules.calc.BankCalculator; import dev.meyi.bn.utilities.ReflectionHelper; import dev.meyi.bn.utilities.ScheduledEvents; import dev.meyi.bn.utilities.Utils; @@ -41,7 +42,7 @@ public class BazaarNotifier { public static final String MODID = "BazaarNotifier"; - public static final String VERSION = "1.6.0"; + public static final String VERSION = "1.6.1"; public static final String prefix = EnumChatFormatting.GOLD + "[" + EnumChatFormatting.YELLOW + "BN" + EnumChatFormatting.GOLD + "] " + EnumChatFormatting.RESET; @@ -152,6 +153,8 @@ public void preInit(FMLPreInitializationEvent event) { enchantCraftingList = resources.getAsJsonObject("enchantCraftingList"); bazaarConv = Utils.jsonToBimap(bazaarConversions); } + + BankCalculator.reset(); } diff --git a/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java b/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java index 9dc3c74..5c44bd6 100644 --- a/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java +++ b/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java @@ -59,8 +59,6 @@ public void processCommand(ICommandSender ics, String[] args) { BazaarNotifier.config.api = args[1]; BazaarNotifier.validApiKey = true; BazaarNotifier.activeBazaar = true; - CraftingCalculator.getUnlockedRecipes(); - BazaarNotifier.config.collectionCheckDisabled = false; } else { player.addChatMessage(new ChatComponentText( BazaarNotifier.prefix + EnumChatFormatting.RED diff --git a/src/main/java/dev/meyi/bn/config/Configuration.java b/src/main/java/dev/meyi/bn/config/Configuration.java index de8d483..a565019 100644 --- a/src/main/java/dev/meyi/bn/config/Configuration.java +++ b/src/main/java/dev/meyi/bn/config/Configuration.java @@ -3,6 +3,7 @@ import com.google.gson.Gson; import dev.meyi.bn.BazaarNotifier; import dev.meyi.bn.modules.ModuleName; +import dev.meyi.bn.modules.calc.BankCalculator; import dev.meyi.bn.utilities.Defaults; import java.io.File; import java.io.IOException; @@ -14,7 +15,7 @@ public class Configuration { private static final int MODULE_LENGTH = 4; - public boolean collectionCheckDisabled; + public boolean collectionCheck; public int craftingSortingOption; public int craftingListLength; public int suggestionListLength; @@ -25,16 +26,18 @@ public class Configuration { public String version; public ModuleConfig[] modules; + public double bazaarProfit = 0; + private boolean showInstantSellProfit; private boolean showSellOfferProfit; private boolean showProfitPerMil; - public Configuration(boolean collectionCheckDisabled, int craftingSortingOption, + public Configuration(boolean collectionCheck, int craftingSortingOption, int craftingListLength, boolean suggestionShowEnchantments, boolean showInstantSellProfit, boolean showSellOfferProfit, boolean showProfitPerMil, - int suggestionListLength, boolean showChatMessages, String apiKey, boolean useBuyOrders, + int suggestionListLength, boolean showChatMessages, String apiKey, boolean useBuyOrders, double bazaarProfit, ModuleConfig[] modules) { - this.collectionCheckDisabled = collectionCheckDisabled; + this.collectionCheck = collectionCheck; this.craftingSortingOption = craftingSortingOption; this.craftingListLength = craftingListLength; this.suggestionShowEnchantments = suggestionShowEnchantments; @@ -42,6 +45,7 @@ public Configuration(boolean collectionCheckDisabled, int craftingSortingOption, this.showSellOfferProfit = showSellOfferProfit; this.showProfitPerMil = showProfitPerMil; this.suggestionListLength = suggestionListLength; + this.bazaarProfit = bazaarProfit; this.api = apiKey == null ? "" : apiKey; // It is fixed in createDefaultConfig, but redundancies. this.version = BazaarNotifier.VERSION; @@ -54,6 +58,7 @@ public Configuration(boolean collectionCheckDisabled, int craftingSortingOption, public static void saveConfig(File file, Configuration config) { Gson gson = new Gson(); BazaarNotifier.config.modules = BazaarNotifier.modules.generateConfig(); + BazaarNotifier.config.bazaarProfit = BankCalculator.getBazaarProfit(); try { if (!file.isFile()) { //noinspection ResultOfMethodCallIgnored @@ -72,12 +77,12 @@ public static Configuration createDefaultConfig() { for (ModuleName moduleName : ModuleName.values()) { c[i++] = ModuleConfig.generateDefaultConfig(moduleName.name()); } - return new Configuration(Defaults.COLLECTION_CHECKING_DISABLED, + return new Configuration(Defaults.COLLECTION_CHECKING, Defaults.CRAFTING_SORTING_OPTION, Defaults.CRAFTING_LIST_LENGTH, Defaults.SUGGESTION_SHOW_ENCHANTMENTS, Defaults.INSTANT_SELL_PROFIT, Defaults.SELL_OFFER_PROFIT, Defaults.PROFIT_PER_MIL, Defaults.SUGGESTION_LIST_LENGTH, Defaults.SEND_CHAT_MESSAGES, "", - Defaults.USE_BUY_ORDERS, c); + Defaults.USE_BUY_ORDERS, 0, c); } public boolean isShowSellOfferProfit() { diff --git a/src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java b/src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java index e6c43e4..fd76f5c 100644 --- a/src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java +++ b/src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java @@ -43,7 +43,7 @@ public void initGui() { 100, BazaarNotifier.config.craftingListLength, false, true)); buttonList.add(new GuiButton(ButtonIds.COLLECTION_CHECK.id, getButtonX(), getButtonY(), "Collection Check: " + - SettingsGui.getOnOff(!BazaarNotifier.config.collectionCheckDisabled))); + SettingsGui.getOnOff(BazaarNotifier.config.collectionCheck))); if (!BazaarNotifier.validApiKey) { buttonList.get(3).enabled = false; } @@ -87,16 +87,17 @@ protected void actionPerformed(GuiButton Button) { BazaarNotifier.config.craftingListLength = (int) Math.round(lengthSlider.getValue()); } } else if (Button.id == ButtonIds.COLLECTION_CHECK.id) { - BazaarNotifier.config.collectionCheckDisabled ^= true; + BazaarNotifier.config.collectionCheck ^= true; Button.displayString = - "Collection Check: " + SettingsGui.getOnOff(!BazaarNotifier.config.collectionCheckDisabled); + "Collection Check: " + SettingsGui.getOnOff(BazaarNotifier.config.collectionCheck); - if (!BazaarNotifier.config.collectionCheckDisabled) { + if (BazaarNotifier.config.collectionCheck) { new Thread(() -> { CraftingCalculator.getUnlockedRecipes(); Button.displayString = - "Collection Check: " + SettingsGui.getOnOff(!BazaarNotifier.config.collectionCheckDisabled); - if (BazaarNotifier.config.collectionCheckDisabled) { + "Collection Check: " + SettingsGui.getOnOff( + BazaarNotifier.config.collectionCheck); + if (!BazaarNotifier.config.collectionCheck) { Minecraft.getMinecraft().thePlayer .addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED diff --git a/src/main/java/dev/meyi/bn/handlers/ChestTickHandler.java b/src/main/java/dev/meyi/bn/handlers/ChestTickHandler.java index d32421c..a9c34c4 100644 --- a/src/main/java/dev/meyi/bn/handlers/ChestTickHandler.java +++ b/src/main/java/dev/meyi/bn/handlers/ChestTickHandler.java @@ -2,10 +2,8 @@ import dev.meyi.bn.BazaarNotifier; import dev.meyi.bn.json.Order; -import dev.meyi.bn.modules.calc.BankCalculator; import dev.meyi.bn.utilities.ReflectionHelper; import dev.meyi.bn.utilities.Utils; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -27,7 +25,6 @@ public class ChestTickHandler { public static String lastScreenDisplayName = ""; - private static long date = System.currentTimeMillis(); // /blockdata x y z {CustomName:"___"} << For Custom Chest Name Testing @@ -179,21 +176,33 @@ private void orderConfirmation(IInventory chest) { String product = StringUtils.stripControlCodes( chest.getStackInSlot(13).getTagCompound().getCompoundTag("display").getTagList("Lore", 8) - .getStringTagAt(4)).split("x ")[1]; + .getStringTagAt(4)).split("x ", 2)[1]; + + String productName; if (!BazaarNotifier.bazaarConv.containsValue(product)) { - Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "The bazaar item you just put an order for doesn't exist. Please report this in the discord server")); - if (System.currentTimeMillis() > date + (60 * 60 * 1000)) { - try { - Utils.updateResources(); - } catch (IOException ignored) { - } + String[] possibleConversion = Utils.getItemIdFromName(product); + productName = possibleConversion[1]; + + if (!possibleConversion[0].equals(product) && !productName.isEmpty()) { + BazaarNotifier.bazaarConv.put(productName, product); + Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( + BazaarNotifier.prefix + EnumChatFormatting.RED + + "A possible conversion was found. Please report this to the discord server:" + + EnumChatFormatting.GRAY + " \"" + + productName + "\" - \"" + product + "\" \"" + possibleConversion[0] + "\".")); + } else { + Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( + BazaarNotifier.prefix + EnumChatFormatting.RED + + "No item conversion was found for that item. Please report this to the discord server: " + + EnumChatFormatting.GRAY + "\"" + + product + "\".")); } - date = System.currentTimeMillis(); } else { - String productName = BazaarNotifier.bazaarConv.inverse().get(product); + productName = BazaarNotifier.bazaarConv.inverse().get(product); + } + + if (BazaarNotifier.bazaarConv.containsKey(productName)) { String productWithAmount = StringUtils.stripControlCodes( chest.getStackInSlot(13).getTagCompound().getCompoundTag("display") .getTagList("Lore", 8).getStringTagAt(4)).split(": ")[1]; diff --git a/src/main/java/dev/meyi/bn/handlers/EventHandler.java b/src/main/java/dev/meyi/bn/handlers/EventHandler.java index f8cca09..5d2a4d0 100644 --- a/src/main/java/dev/meyi/bn/handlers/EventHandler.java +++ b/src/main/java/dev/meyi/bn/handlers/EventHandler.java @@ -133,8 +133,6 @@ public void bazaarChatHandler(ClientChatReceivedEvent e) { BazaarNotifier.config.api = apiKey; BazaarNotifier.validApiKey = true; BazaarNotifier.activeBazaar = true; - CraftingCalculator.getUnlockedRecipes(); - BazaarNotifier.config.collectionCheckDisabled = false; } else { Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( BazaarNotifier.prefix + EnumChatFormatting.RED diff --git a/src/main/java/dev/meyi/bn/json/Exchange.java b/src/main/java/dev/meyi/bn/json/Exchange.java index c531aa4..e952663 100644 --- a/src/main/java/dev/meyi/bn/json/Exchange.java +++ b/src/main/java/dev/meyi/bn/json/Exchange.java @@ -60,8 +60,8 @@ public boolean matchesOrder(Exchange exchange) { } public boolean canCraft() { - return BazaarNotifier.enchantCraftingList.getAsJsonObject("other").has(productId) - && type == OrderType.SELL; + return type == OrderType.SELL && BazaarNotifier.enchantCraftingList.getAsJsonObject("other") + .has(productId); } @Override diff --git a/src/main/java/dev/meyi/bn/modules/calc/BankCalculator.java b/src/main/java/dev/meyi/bn/modules/calc/BankCalculator.java index c9c1c97..c484939 100644 --- a/src/main/java/dev/meyi/bn/modules/calc/BankCalculator.java +++ b/src/main/java/dev/meyi/bn/modules/calc/BankCalculator.java @@ -3,6 +3,7 @@ import dev.meyi.bn.BazaarNotifier; import dev.meyi.bn.json.Exchange; import dev.meyi.bn.json.Order.OrderType; +import dev.meyi.bn.utilities.Utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -14,111 +15,109 @@ public class BankCalculator { - private static boolean changed = false; private static final List orderHistory = new ArrayList<>(); + private static final Pattern sellOffer = Pattern.compile( + "\\[Bazaar] Claimed .* coins from selling (.*)x (.*) at (.*) each!"); + private static final Pattern buyOrder = Pattern.compile( + "\\[Bazaar] Claimed (.*)x (.*) worth .* coins bought for (.*) each!"); + private static final Pattern instantSell = Pattern.compile( + "\\[Bazaar] Sold (.*)x (.*) for (.*) coins!"); + private static final Pattern instantBuy = Pattern.compile( + "\\[Bazaar] Bought (.*)x (.*) for (.*) coins!"); private static double calculatedProfit = 0; + private static boolean startup = true; - public static synchronized double getBazaarProfit() { - if (changed) { - for (int i = orderHistory.size() - 1; i >= 0; i--) { - if (orderHistory.get(i).getAmount() != 0) { - for (int j = orderHistory.size() - 1; j >= 0; j--) { - if (orderHistory.get(j).getAmount() != 0 && orderHistory.get(i) - .matchesOrder(orderHistory.get(j))) { - Exchange buy, sell; - int buyIndex, sellIndex; - - if (orderHistory.get(i).getType() == OrderType.BUY) { - buy = orderHistory.get((buyIndex = i)); - sell = orderHistory.get((sellIndex = j)); - } else { - buy = orderHistory.get((buyIndex = j)); - sell = orderHistory.get((sellIndex = i)); - } + public static double getBazaarProfit() { + return calculatedProfit; + } - if (buy.getAmount() >= sell.getAmount()) { - calculatedProfit += - buy.getAmount() * (sell.getPricePerUnit() * .99 - buy.getPricePerUnit()); - buy.removeAmount(buy.getAmount()); - sell.removeAmount(buy.getAmount()); - } else { - calculatedProfit += - sell.getAmount() * (sell.getPricePerUnit() * .99 - buy.getPricePerUnit()); - sell.removeAmount(sell.getAmount()); - buy.removeAmount(sell.getAmount()); - } + public static void calculateBazaarProfit() { + for (int i = orderHistory.size() - 1; i >= 0; i--) { + Exchange sell = orderHistory.get(i); + if (sell.getAmount() != 0 && sell.getType() == OrderType.SELL) { + for (int j = orderHistory.size() - 1; j >= 0; j--) { + Exchange buy = orderHistory.get(j); + if (buy.getAmount() != 0 && sell.matchesOrder(buy)) { + if (buy.getAmount() >= sell.getAmount()) { + calculatedProfit += + sell.getAmount() * (sell.getPricePerUnit() * .99 - buy.getPricePerUnit()); + buy.removeAmount(sell.getAmount()); + sell.removeAmount(sell.getAmount()); + } else { + calculatedProfit += + buy.getAmount() * (sell.getPricePerUnit() * .99 - buy.getPricePerUnit()); + sell.removeAmount(buy.getAmount()); + buy.removeAmount(buy.getAmount()); + } - if ((buyIndex == i && buy.getAmount() == 0) || - (sellIndex == i && sell.getAmount() == 0)) { - break; - } + if (sell.getAmount() == 0) { + break; } } } } + } - orderHistory.removeIf(v -> v.getAmount() == 0); - - craftingLoop: - for (int i = orderHistory.size() - 1; i >= 0; i--) { - if (orderHistory.get(i).getAmount() != 0 && orderHistory.get(i).canCraft()) { - Map craftingResources = orderHistory.get(i).getCraftingResources(); - Map> availableResources = new HashMap<>(); - craftingResources.keySet().forEach(key -> availableResources.put(key, new ArrayList<>())); - for (Exchange exchange : orderHistory) { - if (craftingResources.containsKey(exchange.getProductId())) { - availableResources.get(exchange.getProductId()).add(exchange); - } + orderHistory.removeIf(v -> v.getAmount() == 0); + + craftingLoop: + for (int i = orderHistory.size() - 1; i >= 0; i--) { + if (orderHistory.get(i).getAmount() != 0 && orderHistory.get(i).canCraft()) { + Map craftingResources = orderHistory.get(i).getCraftingResources(); + Map> availableResources = new HashMap<>(); + craftingResources.keySet().forEach(key -> availableResources.put(key, new ArrayList<>())); + for (Exchange exchange : orderHistory) { + if (exchange.getType() == OrderType.BUY && craftingResources.containsKey( + exchange.getProductId())) { + availableResources.get(exchange.getProductId()).add(exchange); } + } - int maxCrafting = orderHistory.get(i).getAmount(); + int maxCrafting = orderHistory.get(i).getAmount(); - for (Entry> entry : availableResources.entrySet()) { - int amountAvailable = 0; - for (Exchange e : entry.getValue()) { - amountAvailable += e.getAmount(); - } + for (Entry> entry : availableResources.entrySet()) { + int amountAvailable = 0; + for (Exchange e : entry.getValue()) { + amountAvailable += e.getAmount(); + } - int craftCost = craftingResources.get(entry.getKey()); + int craftCost = craftingResources.get(entry.getKey()); - if (amountAvailable < craftCost) { - break craftingLoop; - } else { - maxCrafting = Math.min(maxCrafting, amountAvailable / craftCost); - } + if (amountAvailable < craftCost) { + break craftingLoop; + } else { + maxCrafting = Math.min(maxCrafting, amountAvailable / craftCost); } + } + + double buyValue = 0; - double buyValue = 0; - - if (maxCrafting > 0) { - for (String key : availableResources.keySet()) { - int valueToRemove = maxCrafting * craftingResources.get(key); - for (Exchange e : availableResources.get(key)) { - if (e.getAmount() >= valueToRemove) { - buyValue += valueToRemove * e.getPricePerUnit(); - e.removeAmount(valueToRemove); - valueToRemove = 0; - } else { - buyValue += e.getAmount() * e.getPricePerUnit(); - valueToRemove -= e.getAmount(); - e.removeAmount(e.getAmount()); - } + if (maxCrafting > 0) { + for (String key : availableResources.keySet()) { + int valueToRemove = maxCrafting * craftingResources.get(key); + for (Exchange e : availableResources.get(key)) { + if (e.getAmount() >= valueToRemove) { + buyValue += valueToRemove * e.getPricePerUnit(); + e.removeAmount(valueToRemove); + valueToRemove = 0; + } else { + buyValue += e.getAmount() * e.getPricePerUnit(); + valueToRemove -= e.getAmount(); + e.removeAmount(e.getAmount()); } } } - orderHistory.get(i).removeAmount(maxCrafting); - calculatedProfit += - ((double) maxCrafting * orderHistory.get(i).getPricePerUnit()) * .99 - buyValue; } + orderHistory.get(i).removeAmount(maxCrafting); + calculatedProfit += + ((double) maxCrafting * orderHistory.get(i).getPricePerUnit()) * .99 - buyValue; } - - orderHistory.removeIf(v -> v.getAmount() == 0); } - return calculatedProfit; + orderHistory.removeIf(v -> v.getAmount() == 0); } - public static synchronized void evaluate(String message) { + public static void evaluate(String message) { String productId; double pricePerUnit; int amount; @@ -148,33 +147,36 @@ public static synchronized void evaluate(String message) { return; } + if (BazaarNotifier.bazaarConv.containsValue(m.group(2))) { + productId = BazaarNotifier.bazaarConv.inverse().get(m.group(2)); + } else { + productId = Utils.getItemIdFromName(m.group(2))[1]; + } - productId = BazaarNotifier.bazaarConv.inverse().get(m.group(2)); + if (!productId.isEmpty()) { + Exchange e = new Exchange(type, productId, pricePerUnit, amount); - Exchange e = new Exchange(type, productId, pricePerUnit, amount); + int index; + if ((index = orderHistory.indexOf(e)) != -1) { + orderHistory.get(index).addAmount(amount); + } else { + orderHistory.add(e); + } - int index; - if ((index = orderHistory.indexOf(e)) != -1) { - orderHistory.get(index).addAmount(amount); - } else { - orderHistory.add(e); + // Regardless of type, because reverse flipping is possible + // aka selling an item you already own and buying back cheaper + calculateBazaarProfit(); } - - changed = type == OrderType.SELL; // Profit is only changed if something is sold } public static synchronized void reset() { + if (startup) { + startup = false; + calculatedProfit = BazaarNotifier.config.bazaarProfit; + } else { + calculatedProfit = 0; + } orderHistory.clear(); - calculatedProfit = 0; } - private static final Pattern sellOffer = Pattern.compile( - "\\[Bazaar] Claimed .* coins from selling (.*)x (.*) at (.*) each!"); - private static final Pattern buyOrder = Pattern.compile( - "\\[Bazaar] Claimed (.*)x (.*) worth .* coins bought for (.*) each!"); - private static final Pattern instantSell = Pattern.compile( - "\\[Bazaar] Sold (.*)x (.*) for (.*) coins!"); - private static final Pattern instantBuy = Pattern.compile( - "\\[Bazaar] Bought (.*)x (.*) for (.*) coins!"); - } \ No newline at end of file diff --git a/src/main/java/dev/meyi/bn/modules/calc/CraftingCalculator.java b/src/main/java/dev/meyi/bn/modules/calc/CraftingCalculator.java index 64bd1b9..b854980 100644 --- a/src/main/java/dev/meyi/bn/modules/calc/CraftingCalculator.java +++ b/src/main/java/dev/meyi/bn/modules/calc/CraftingCalculator.java @@ -33,7 +33,7 @@ public static void getBestEnchantRecipes() { .getAsJsonObject(itemName) .get("collection").getAsString(); - if (!BazaarNotifier.config.collectionCheckDisabled && !(unlockedRecipes.contains(collection) + if (BazaarNotifier.config.collectionCheck && !(unlockedRecipes.contains(collection) || collection.equals("NONE"))) { continue; } @@ -170,10 +170,10 @@ public static void getUnlockedRecipes() { // Honestly, if this is empty, we should just assume something went wrong and disable the collection check. if (unlockedRecipes.size() == 0) { - BazaarNotifier.config.collectionCheckDisabled = true; + BazaarNotifier.config.collectionCheck = false; } } else { - BazaarNotifier.config.collectionCheckDisabled = true; + BazaarNotifier.config.collectionCheck = false; } } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/dev/meyi/bn/utilities/Defaults.java b/src/main/java/dev/meyi/bn/utilities/Defaults.java index 8f93da9..f5586c7 100644 --- a/src/main/java/dev/meyi/bn/utilities/Defaults.java +++ b/src/main/java/dev/meyi/bn/utilities/Defaults.java @@ -17,7 +17,7 @@ public class Defaults { public static final boolean INSTANT_SELL_PROFIT = true; public static final boolean SELL_OFFER_PROFIT = true; public static final boolean PROFIT_PER_MIL = true; - public static final boolean COLLECTION_CHECKING_DISABLED = true; + public static final boolean COLLECTION_CHECKING = false; public static final boolean SEND_CHAT_MESSAGES = true; public static final boolean USE_BUY_ORDERS = true; public static final boolean SUGGESTION_SHOW_ENCHANTMENTS = true; diff --git a/src/main/java/dev/meyi/bn/utilities/Utils.java b/src/main/java/dev/meyi/bn/utilities/Utils.java index 9e72e85..1044278 100644 --- a/src/main/java/dev/meyi/bn/utilities/Utils.java +++ b/src/main/java/dev/meyi/bn/utilities/Utils.java @@ -47,7 +47,6 @@ public static BazaarResponse getBazaarData() throws IOException { "https://api.hypixel.net/skyblock/bazaar" + apiBit); HttpResponse response = client.execute(request); - String result = IOUtils.toString(new BufferedReader (new InputStreamReader( response.getEntity().getContent()))); @@ -64,8 +63,9 @@ public static BazaarResponse getBazaarData() throws IOException { public static List unlockedRecipes() throws IOException { Gson gson = new Gson(); - if (!BazaarNotifier.config.api.isEmpty() && (BazaarNotifier.validApiKey - || (BazaarNotifier.validApiKey = validateApiKey()))) { + if (BazaarNotifier.config.collectionCheck && !BazaarNotifier.config.api.isEmpty() && ( + BazaarNotifier.validApiKey + || (BazaarNotifier.validApiKey = validateApiKey()))) { CloseableHttpClient client = HttpClientBuilder.create().build(); if (playerUUID.equals("")) { @@ -89,13 +89,21 @@ public static List unlockedRecipes() throws IOException { + playerUUID); HttpResponse response = client.execute(request); - JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(response.getEntity().getContent()))); + JsonReader jsonReader = new JsonReader( + new BufferedReader(new InputStreamReader(response.getEntity().getContent()))); jsonReader.setLenient(true); - JsonObject results = gson.fromJson(jsonReader, JsonObject.class); + + JsonObject results = null; + + try { + results = gson.fromJson(jsonReader, JsonObject.class); + } catch (Exception e) { + e.printStackTrace(); + } client.close(); int profileIndex = 0; - if (!results.get("success").getAsBoolean() || !results.has("profiles")) { + if (results == null || !results.get("success").getAsBoolean() || !results.has("profiles")) { return null; } @@ -132,7 +140,7 @@ public static List unlockedRecipes() throws IOException { } return unlockedCollections; } else { - BazaarNotifier.config.collectionCheckDisabled = true; + BazaarNotifier.config.collectionCheck = false; return null; } @@ -141,9 +149,9 @@ public static List unlockedRecipes() throws IOException { public static boolean isJSONValid(String jsonInString) { Gson gson = new Gson(); try { - gson.fromJson(jsonInString, Object.class); + gson.fromJson(jsonInString, JsonObject.class); return true; - } catch (com.google.gson.JsonSyntaxException ex) { + } catch (Exception ex) { return false; } } @@ -159,13 +167,15 @@ public static boolean validateApiKey(String key) throws IOException { .getAsBoolean()) { return true; } else { - BazaarNotifier.config.collectionCheckDisabled = true; + BazaarNotifier.config.collectionCheck = false; return false; } } catch (JsonSyntaxException e) { + BazaarNotifier.config.collectionCheck = false; return false; } } + BazaarNotifier.config.collectionCheck = false; return false; } @@ -173,7 +183,7 @@ public static boolean validateApiKey() throws IOException { if (validateApiKey(BazaarNotifier.config.api)) { return true; } else { - BazaarNotifier.config.collectionCheckDisabled = true; + BazaarNotifier.config.collectionCheck = false; return false; } }