From 988b2728538c45aba6ca3f240c3c718b8608fd12 Mon Sep 17 00:00:00 2001 From: symt <28537235+symt@users.noreply.github.com> Date: Mon, 25 Mar 2024 15:30:27 -0500 Subject: [PATCH] 1.7.2: Resources and API fixes (#128) * New Bazaar API (#127) * version bump * Fix item names (#130) --------- Co-authored-by: Detlev1 <71934197+Detlev1@users.noreply.github.com> --- build.gradle.kts | 2 +- src/main/java/dev/meyi/bn/BazaarNotifier.java | 8 +- .../bn/commands/BazaarNotifierCommand.java | 549 +++++++++--------- .../java/dev/meyi/bn/utilities/Utils.java | 42 +- 4 files changed, 314 insertions(+), 287 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 3a8884d..a66fd8c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,7 +12,7 @@ plugins { } group = "dev.meyi.bazaarnotifier" -version = "1.7.1" +version = "1.7.2" val mod_id = "bazaarnotifier" java { diff --git a/src/main/java/dev/meyi/bn/BazaarNotifier.java b/src/main/java/dev/meyi/bn/BazaarNotifier.java index 9e576f0..4f7850b 100644 --- a/src/main/java/dev/meyi/bn/BazaarNotifier.java +++ b/src/main/java/dev/meyi/bn/BazaarNotifier.java @@ -26,6 +26,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Objects; @@ -42,7 +44,7 @@ public class BazaarNotifier { public static final String MODID = "BazaarNotifier"; - public static final String VERSION = "1.7.1"; + public static final String VERSION = "1.7.2"; public static final String prefix = EnumChatFormatting.GOLD + "[" + EnumChatFormatting.YELLOW + "BN" + EnumChatFormatting.GOLD + "] " + EnumChatFormatting.RESET; @@ -98,7 +100,7 @@ public void preInit(FMLPreInitializationEvent event) { byte[] bytes = Files.readAllBytes(Paths.get(resourcesFile.getPath())); if (bytes.length == 0) throw new JsonSyntaxException("Invalid JSON in Resources File"); JsonReader resourcesReader = new JsonReader(new StringReader(new String( - bytes))); + bytes, StandardCharsets.UTF_8))); resourcesReader.setLenient(true); resources = gson.fromJson(resourcesReader, JsonObject.class); } catch (JsonSyntaxException | ClassCastException e) { @@ -120,7 +122,7 @@ public void preInit(FMLPreInitializationEvent event) { try { Utils.updateResources(); - } catch (IOException e) { + } catch (IOException | KeyManagementException | NoSuchAlgorithmException e) { System.err.println("Error while getting resources from GitHub"); e.printStackTrace(); JsonObject bazaarConversions = resources.getAsJsonObject("bazaarConversions"); diff --git a/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java b/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java index 94638b6..995e295 100644 --- a/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java +++ b/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java @@ -1,276 +1,273 @@ -package dev.meyi.bn.commands; - -import cc.polyfrost.oneconfig.gui.OneConfigGui; -import cc.polyfrost.oneconfig.gui.pages.ModConfigPage; -import dev.meyi.bn.BazaarNotifier; -import dev.meyi.bn.config.Configuration; -import dev.meyi.bn.json.resp.BazaarItem; -import dev.meyi.bn.modules.calc.BankCalculator; -import dev.meyi.bn.modules.calc.CraftingCalculator; -import dev.meyi.bn.modules.calc.SuggestionCalculator; -import dev.meyi.bn.utilities.Utils; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import net.minecraft.command.CommandBase; -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.event.ClickEvent; -import net.minecraft.event.ClickEvent.Action; -import net.minecraft.util.BlockPos; -import net.minecraft.util.ChatComponentText; -import net.minecraft.util.EnumChatFormatting; -import org.apache.commons.lang3.text.WordUtils; - - -public class BazaarNotifierCommand extends CommandBase { - - private static long date = 0; // This can be set to System.currentTimeMillis, but it makes testing annoying - - @Override - public List getCommandAliases() { - return new ArrayList() { - { - add("bn"); - } - }; - } - - - @Override - public String getCommandName() { - return "bazaarnotifier"; - } - - @Override - public String getCommandUsage(ICommandSender sender) { - return "/bazaarnotifier [subcommand]"; - } - - @Override - public void processCommand(ICommandSender ics, String[] args) { - if (ics instanceof EntityPlayer) { - EntityPlayer player = (EntityPlayer) ics; - if (args.length > 0 && args[0].equalsIgnoreCase("reset")) { - if (args.length == 1 || args[1].equalsIgnoreCase("all")) { - BazaarNotifier.resetMod(); - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "All module locations have been reset and the order list has been emptied.")); - } else if (args[1].equalsIgnoreCase("orders") && args.length == 2) { - BazaarNotifier.orders.clear(); - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "Your orders have been cleared.")); - } else if (args[1].equalsIgnoreCase("scale") && args.length == 2) { - BazaarNotifier.resetScale(); - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "Your scale for every module has been reset.")); - } else if (args[1].equalsIgnoreCase("bank") && args.length == 2) { - BankCalculator.reset(); - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "Your bank module has been reset.")); - } else { - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "That module doesn't exist.")); - } - } else if (args.length >= 1 && args[0].equalsIgnoreCase("find")) { - if (args.length == 1) { - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "Use the following format: /bn find (item)")); - } else { - String itemName = WordUtils - .capitalize(String.join(" ", args).substring(5).replaceAll("-", " ")); - if (BazaarNotifier.bazaarDataRaw != null) { - String[] itemSet = Utils.getItemIdFromName(itemName); - itemName = itemSet[0]; - String itemConv = itemSet[1]; - BazaarItem item = BazaarNotifier.bazaarDataRaw.products.get(itemConv); - String findItemString = BazaarNotifier.prefix + EnumChatFormatting.RED - + "Please provide a valid item to find."; - - String bulletPoint = EnumChatFormatting.WHITE + "\u2022 "; - String separator = EnumChatFormatting.RED + " / "; - - if (BazaarNotifier.bazaarConv.containsKey(itemConv)) { - findItemString = BazaarNotifier.header + "\n" + - EnumChatFormatting.DARK_RED + EnumChatFormatting.BOLD + WordUtils - .capitalize(itemName) + "\n " + bulletPoint + - EnumChatFormatting.RED + "Buy Order: " + - EnumChatFormatting.GRAY + BazaarNotifier.df.format(item.sell_summary.size() == 0 ? - 0 : item.sell_summary.get(0).pricePerUnit) + "\n " + bulletPoint + - EnumChatFormatting.RED + "Sell Offer: " + - EnumChatFormatting.GRAY + BazaarNotifier.df.format(item.buy_summary.size() == 0 ? - 0 : item.buy_summary.get(0).pricePerUnit) + "\n " + bulletPoint + - EnumChatFormatting.RED + "Estimated Profit: " + - EnumChatFormatting.GRAY + BazaarNotifier.df - .format(SuggestionCalculator.calculateEP(item)) + "\n"; - } - - if (BazaarNotifier.enchantCraftingList.getAsJsonObject("other").has(itemConv)) { - - String[] prices = CraftingCalculator.getEnchantCraft(itemConv); - - findItemString += - EnumChatFormatting.DARK_RED + "" + EnumChatFormatting.BOLD + "Crafting (" + - EnumChatFormatting.GRAY - + "Buy order" + separator + EnumChatFormatting.GRAY + "Instant buy" - + EnumChatFormatting.DARK_RED + EnumChatFormatting.BOLD + ")" + "\n " + - bulletPoint + - EnumChatFormatting.RED + "Profit (Instant Sell): " + - EnumChatFormatting.GRAY + BazaarNotifier.df - .format(Double.parseDouble(prices[0])) + separator + EnumChatFormatting.GRAY + - BazaarNotifier.df.format(Double.parseDouble(prices[3])) + "\n " + bulletPoint - + - EnumChatFormatting.RED + "Profit (Sell Offer): " + - EnumChatFormatting.GRAY + BazaarNotifier.df - .format(Double.parseDouble(prices[1])) + separator + EnumChatFormatting.GRAY + - BazaarNotifier.df.format(Double.parseDouble(prices[4])) + "\n " + bulletPoint - + - EnumChatFormatting.RED + "Profit per 1M: " + - EnumChatFormatting.GRAY + BazaarNotifier.df - .format(Double.parseDouble(prices[2])) + separator + EnumChatFormatting.GRAY + - BazaarNotifier.df.format(Double.parseDouble(prices[5])) + "\n" + - BazaarNotifier.header; - - } else if (BazaarNotifier.bazaarConv.containsKey(itemConv)) { - findItemString += BazaarNotifier.header; - } - - player.addChatMessage(new ChatComponentText(findItemString)); - - } else { - player.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "Please wait a moment for the mod to get bazaar information")); - } - } - } else if (args.length == 1 && args[0].equalsIgnoreCase("help")) { - player.addChatMessage(new ChatComponentText( - BazaarNotifier.header + "\n" - + EnumChatFormatting.RED + "/bn " + EnumChatFormatting.DARK_RED + "\u2192" - + EnumChatFormatting.GRAY + " Opens the GUI\n" - + EnumChatFormatting.RED + "/bn reset (value) " + EnumChatFormatting.DARK_RED - + "\u2192" + EnumChatFormatting.GRAY - + " Reset specific modules to default settings\n" - + EnumChatFormatting.RED + "/bn api (key) " + EnumChatFormatting.DARK_RED + "\u2192" - + EnumChatFormatting.GRAY + " Sets your api key for crafting module\n" - + EnumChatFormatting.RED + "/bn find (item) " + EnumChatFormatting.DARK_RED - + "\u2192" + EnumChatFormatting.GRAY + " Search specific item's prices and EP\n" - + EnumChatFormatting.RED + "/bn discord " + EnumChatFormatting.DARK_RED + "\u2192" - + EnumChatFormatting.GRAY + " Provides discord link (beta access + more)\n" - + BazaarNotifier.header - )); - } else if (args.length == 1 && args[0].equalsIgnoreCase("__force")) { - BazaarNotifier.forceRender ^= true; - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "This command is intended for testing purposes only, use it at your own peril. Forced rendering has been turned " - + EnumChatFormatting.DARK_RED + (BazaarNotifier.forceRender ? "on" : "off"))); - } else if (args.length == 1 && args[0].equalsIgnoreCase("discord")) { - ChatComponentText discordLink = new ChatComponentText( - EnumChatFormatting.DARK_GREEN + "" + EnumChatFormatting.BOLD - + "[DISCORD LINK]"); - discordLink - .setChatStyle(discordLink.getChatStyle().setChatClickEvent(new ClickEvent( - Action.OPEN_URL, - "https://discord.com/invite/wjpJSVSwvD"))); - ChatComponentText supportLink = new ChatComponentText( - EnumChatFormatting.DARK_GREEN + "" + EnumChatFormatting.BOLD - + "[PATREON LINK]"); - supportLink - .setChatStyle(supportLink.getChatStyle().setChatClickEvent(new ClickEvent( - Action.OPEN_URL, - "https://patreon.com/meyi"))); - - player.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + "\n" + EnumChatFormatting.GREEN + "Join the discord server: ") - .appendSibling(discordLink).appendSibling( - new ChatComponentText( - "\n" + EnumChatFormatting.GREEN + "If you want, you can support my work: ") - .appendSibling(supportLink)) - .appendSibling(new ChatComponentText("\n" + BazaarNotifier.prefix))); - - } else if (args.length == 1 && args[0].equalsIgnoreCase("update")) { - if (date < System.currentTimeMillis() - (10 * 60 * 1000)) { - new Thread(() -> { - try { - Utils.updateResources(); - date = System.currentTimeMillis(); - } catch (IOException e) { - player.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "Resource update failed. Please try again.")); - } - }).start(); - player.addChatMessage( - new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.GREEN - + "Updating required resources from GitHub")); - - } else { - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "Please wait 10 minutes before running that command again")); - } - } else if (args.length > 0) { - player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED - + "The command you just tried to do doesn't exist. Do /bn")); - } else { - BazaarNotifier.guiToOpen = "settings"; - } - } - } - - public boolean canCommandSenderUseCommand(final ICommandSender sender) { - return true; - } - - - @Override - public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { - List arguments = new ArrayList<>(); - - if (args.length <= 1) { - new ArrayList() { - { - add("discord"); - add("find"); - add("reset"); - add("update"); - } - }.forEach(cmd -> { - if (args[0].trim().length() == 0 || cmd.startsWith(args[0].toLowerCase())) { - arguments.add(cmd); - } - }); - } else { - if (args.length <= 2 && args[0].equalsIgnoreCase("reset")) { - new ArrayList() { - { - add("all"); - add("orders"); - add("scale"); - add("bank"); - } - }.forEach(cmd -> { - if (args[1].trim().length() == 0 || cmd.startsWith(args[1].toLowerCase())) { - arguments.add(cmd); - } - }); - } else if (args.length <= 2 && args[0].equalsIgnoreCase("find")) { - ArrayList a = new ArrayList<>(); - for (String s : BazaarNotifier.bazaarConv.values()) { - s = s.replace(' ', '-'); - a.add(s.toLowerCase()); - } - a.forEach(cmd -> { - if (args[1].trim().length() == 0 || cmd.startsWith(args[1].toLowerCase())) { - arguments.add(cmd); - } - if (!arguments.contains(cmd) && (cmd.contains(args[1].toLowerCase()))) { - arguments.add(cmd); - } - }); - } - } - return arguments; - } -} - - +package dev.meyi.bn.commands; + +import dev.meyi.bn.BazaarNotifier; +import dev.meyi.bn.json.resp.BazaarItem; +import dev.meyi.bn.modules.calc.BankCalculator; +import dev.meyi.bn.modules.calc.CraftingCalculator; +import dev.meyi.bn.modules.calc.SuggestionCalculator; +import dev.meyi.bn.utilities.Utils; +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.event.ClickEvent; +import net.minecraft.event.ClickEvent.Action; +import net.minecraft.util.BlockPos; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.EnumChatFormatting; +import org.apache.commons.lang3.text.WordUtils; + + +public class BazaarNotifierCommand extends CommandBase { + + private static long date = 0; // This can be set to System.currentTimeMillis, but it makes testing annoying + + @Override + public List getCommandAliases() { + return new ArrayList() { + { + add("bn"); + } + }; + } + + + @Override + public String getCommandName() { + return "bazaarnotifier"; + } + + @Override + public String getCommandUsage(ICommandSender sender) { + return "/bazaarnotifier [subcommand]"; + } + + @Override + public void processCommand(ICommandSender ics, String[] args) { + if (ics instanceof EntityPlayer) { + EntityPlayer player = (EntityPlayer) ics; + if (args.length > 0 && args[0].equalsIgnoreCase("reset")) { + if (args.length == 1 || args[1].equalsIgnoreCase("all")) { + BazaarNotifier.resetMod(); + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "All module locations have been reset and the order list has been emptied.")); + } else if (args[1].equalsIgnoreCase("orders") && args.length == 2) { + BazaarNotifier.orders.clear(); + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "Your orders have been cleared.")); + } else if (args[1].equalsIgnoreCase("scale") && args.length == 2) { + BazaarNotifier.resetScale(); + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "Your scale for every module has been reset.")); + } else if (args[1].equalsIgnoreCase("bank") && args.length == 2) { + BankCalculator.reset(); + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "Your bank module has been reset.")); + } else { + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "That module doesn't exist.")); + } + } else if (args.length >= 1 && args[0].equalsIgnoreCase("find")) { + if (args.length == 1) { + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "Use the following format: /bn find (item)")); + } else { + String itemName = WordUtils + .capitalize(String.join(" ", args).substring(5).replaceAll("-", " ")); + if (BazaarNotifier.bazaarDataRaw != null) { + String[] itemSet = Utils.getItemIdFromName(itemName); + itemName = itemSet[0]; + String itemConv = itemSet[1]; + BazaarItem item = BazaarNotifier.bazaarDataRaw.products.get(itemConv); + String findItemString = BazaarNotifier.prefix + EnumChatFormatting.RED + + "Please provide a valid item to find."; + + String bulletPoint = EnumChatFormatting.WHITE + "\u2022 "; + String separator = EnumChatFormatting.RED + " / "; + + if (BazaarNotifier.bazaarConv.containsKey(itemConv)) { + findItemString = BazaarNotifier.header + "\n" + + EnumChatFormatting.DARK_RED + EnumChatFormatting.BOLD + WordUtils + .capitalize(itemName) + "\n " + bulletPoint + + EnumChatFormatting.RED + "Buy Order: " + + EnumChatFormatting.GRAY + BazaarNotifier.df.format(item.sell_summary.size() == 0 ? + 0 : item.sell_summary.get(0).pricePerUnit) + "\n " + bulletPoint + + EnumChatFormatting.RED + "Sell Offer: " + + EnumChatFormatting.GRAY + BazaarNotifier.df.format(item.buy_summary.size() == 0 ? + 0 : item.buy_summary.get(0).pricePerUnit) + "\n " + bulletPoint + + EnumChatFormatting.RED + "Estimated Profit: " + + EnumChatFormatting.GRAY + BazaarNotifier.df + .format(SuggestionCalculator.calculateEP(item)) + "\n"; + } + + if (BazaarNotifier.enchantCraftingList.getAsJsonObject("other").has(itemConv)) { + + String[] prices = CraftingCalculator.getEnchantCraft(itemConv); + + findItemString += + EnumChatFormatting.DARK_RED + "" + EnumChatFormatting.BOLD + "Crafting (" + + EnumChatFormatting.GRAY + + "Buy order" + separator + EnumChatFormatting.GRAY + "Instant buy" + + EnumChatFormatting.DARK_RED + EnumChatFormatting.BOLD + ")" + "\n " + + bulletPoint + + EnumChatFormatting.RED + "Profit (Instant Sell): " + + EnumChatFormatting.GRAY + BazaarNotifier.df + .format(Double.parseDouble(prices[0])) + separator + EnumChatFormatting.GRAY + + BazaarNotifier.df.format(Double.parseDouble(prices[3])) + "\n " + bulletPoint + + + EnumChatFormatting.RED + "Profit (Sell Offer): " + + EnumChatFormatting.GRAY + BazaarNotifier.df + .format(Double.parseDouble(prices[1])) + separator + EnumChatFormatting.GRAY + + BazaarNotifier.df.format(Double.parseDouble(prices[4])) + "\n " + bulletPoint + + + EnumChatFormatting.RED + "Profit per 1M: " + + EnumChatFormatting.GRAY + BazaarNotifier.df + .format(Double.parseDouble(prices[2])) + separator + EnumChatFormatting.GRAY + + BazaarNotifier.df.format(Double.parseDouble(prices[5])) + "\n" + + BazaarNotifier.header; + + } else if (BazaarNotifier.bazaarConv.containsKey(itemConv)) { + findItemString += BazaarNotifier.header; + } + + player.addChatMessage(new ChatComponentText(findItemString)); + + } else { + player.addChatMessage(new ChatComponentText( + BazaarNotifier.prefix + EnumChatFormatting.RED + + "Please wait a moment for the mod to get bazaar information")); + } + } + } else if (args.length == 1 && args[0].equalsIgnoreCase("help")) { + player.addChatMessage(new ChatComponentText( + BazaarNotifier.header + "\n" + + EnumChatFormatting.RED + "/bn " + EnumChatFormatting.DARK_RED + "\u2192" + + EnumChatFormatting.GRAY + " Opens the GUI\n" + + EnumChatFormatting.RED + "/bn reset (value) " + EnumChatFormatting.DARK_RED + + "\u2192" + EnumChatFormatting.GRAY + + " Reset specific modules to default settings\n" + + EnumChatFormatting.RED + "/bn api (key) " + EnumChatFormatting.DARK_RED + "\u2192" + + EnumChatFormatting.GRAY + " Sets your api key for crafting module\n" + + EnumChatFormatting.RED + "/bn find (item) " + EnumChatFormatting.DARK_RED + + "\u2192" + EnumChatFormatting.GRAY + " Search specific item's prices and EP\n" + + EnumChatFormatting.RED + "/bn discord " + EnumChatFormatting.DARK_RED + "\u2192" + + EnumChatFormatting.GRAY + " Provides discord link (beta access + more)\n" + + BazaarNotifier.header + )); + } else if (args.length == 1 && args[0].equalsIgnoreCase("__force")) { + BazaarNotifier.forceRender ^= true; + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "This command is intended for testing purposes only, use it at your own peril. Forced rendering has been turned " + + EnumChatFormatting.DARK_RED + (BazaarNotifier.forceRender ? "on" : "off"))); + } else if (args.length == 1 && args[0].equalsIgnoreCase("discord")) { + ChatComponentText discordLink = new ChatComponentText( + EnumChatFormatting.DARK_GREEN + "" + EnumChatFormatting.BOLD + + "[DISCORD LINK]"); + discordLink + .setChatStyle(discordLink.getChatStyle().setChatClickEvent(new ClickEvent( + Action.OPEN_URL, + "https://discord.com/invite/wjpJSVSwvD"))); + ChatComponentText supportLink = new ChatComponentText( + EnumChatFormatting.DARK_GREEN + "" + EnumChatFormatting.BOLD + + "[PATREON LINK]"); + supportLink + .setChatStyle(supportLink.getChatStyle().setChatClickEvent(new ClickEvent( + Action.OPEN_URL, + "https://patreon.com/meyi"))); + + player.addChatMessage(new ChatComponentText( + BazaarNotifier.prefix + "\n" + EnumChatFormatting.GREEN + "Join the discord server: ") + .appendSibling(discordLink).appendSibling( + new ChatComponentText( + "\n" + EnumChatFormatting.GREEN + "If you want, you can support my work: ") + .appendSibling(supportLink)) + .appendSibling(new ChatComponentText("\n" + BazaarNotifier.prefix))); + + } else if (args.length == 1 && args[0].equalsIgnoreCase("update")) { + if (date < System.currentTimeMillis() - (10 * 60 * 1000)) { + new Thread(() -> { + try { + Utils.updateResources(); + date = System.currentTimeMillis(); + } catch (IOException | KeyManagementException | NoSuchAlgorithmException e) { + player.addChatMessage(new ChatComponentText( + BazaarNotifier.prefix + EnumChatFormatting.RED + + "Resource update failed. Please try again.")); + } + }).start(); + player.addChatMessage( + new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.GREEN + + "Updating required resources from GitHub")); + + } else { + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "Please wait 10 minutes before running that command again")); + } + } else if (args.length > 0) { + player.addChatMessage(new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED + + "The command you just tried to do doesn't exist. Do /bn")); + } else { + BazaarNotifier.guiToOpen = "settings"; + } + } + } + + public boolean canCommandSenderUseCommand(final ICommandSender sender) { + return true; + } + + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { + List arguments = new ArrayList<>(); + + if (args.length <= 1) { + new ArrayList() { + { + add("discord"); + add("find"); + add("reset"); + add("update"); + } + }.forEach(cmd -> { + if (args[0].trim().length() == 0 || cmd.startsWith(args[0].toLowerCase())) { + arguments.add(cmd); + } + }); + } else { + if (args.length <= 2 && args[0].equalsIgnoreCase("reset")) { + new ArrayList() { + { + add("all"); + add("orders"); + add("scale"); + add("bank"); + } + }.forEach(cmd -> { + if (args[1].trim().length() == 0 || cmd.startsWith(args[1].toLowerCase())) { + arguments.add(cmd); + } + }); + } else if (args.length <= 2 && args[0].equalsIgnoreCase("find")) { + ArrayList a = new ArrayList<>(); + for (String s : BazaarNotifier.bazaarConv.values()) { + s = s.replace(' ', '-'); + a.add(s.toLowerCase()); + } + a.forEach(cmd -> { + if (args[1].trim().length() == 0 || cmd.startsWith(args[1].toLowerCase())) { + arguments.add(cmd); + } + if (!arguments.contains(cmd) && (cmd.contains(args[1].toLowerCase()))) { + arguments.add(cmd); + } + }); + } + } + return arguments; + } +} \ No newline at end of file diff --git a/src/main/java/dev/meyi/bn/utilities/Utils.java b/src/main/java/dev/meyi/bn/utilities/Utils.java index ede3020..c86006d 100644 --- a/src/main/java/dev/meyi/bn/utilities/Utils.java +++ b/src/main/java/dev/meyi/bn/utilities/Utils.java @@ -16,6 +16,11 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -30,14 +35,35 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + public class Utils { + private static final TrustManager[] trustAllCerts = new TrustManager[] { + new X509TrustManager() { + @Override + public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException { + } + + @Override + public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + + } + }; private static String playerUUID = ""; public static BazaarResponse getBazaarData() throws IOException { Gson gson = new Gson(); CloseableHttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet( - "https://api.hypixel.net/skyblock/bazaar"); + "https://api.hypixel.net/v2/skyblock/bazaar"); HttpResponse response = client.execute(request); String result = IOUtils.toString(new BufferedReader @@ -96,23 +122,25 @@ public static boolean isJSONValid(String jsonInString) { } } - public static void updateResources() throws IOException { + public static void updateResources() throws IOException, KeyManagementException, NoSuchAlgorithmException { Gson gson = new Gson(); HttpGet request; HttpResponse response; - CloseableHttpClient client = HttpClientBuilder.create().build(); + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + CloseableHttpClient client = HttpClientBuilder.create().setSslcontext(sc).build(); request = new HttpGet(BazaarNotifier.RESOURCE_LOCATION); response = client.execute(request); JsonReader jsonReader = new JsonReader( - new BufferedReader(new InputStreamReader(response.getEntity().getContent()))); + new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))); jsonReader.setLenient(true); try { BazaarNotifier.resources = gson.fromJson(jsonReader, JsonObject.class); BazaarNotifier.bazaarConv = jsonToBimap( - BazaarNotifier.resources.getAsJsonObject("bazaarConversions")); + BazaarNotifier.resources.getAsJsonObject("bazaarConversions")); BazaarNotifier.enchantCraftingList = BazaarNotifier.resources - .getAsJsonObject("enchantCraftingList"); + .getAsJsonObject("enchantCraftingList"); } catch (JsonSyntaxException e) { e.printStackTrace(); } finally { @@ -204,4 +232,4 @@ public static int getOrderAmountLeft(List lore, int totalAmount) { } return amountLeft; } -} \ No newline at end of file +}