From 4249788b0942d21076693a39b61c3bbea6113fbd Mon Sep 17 00:00:00 2001 From: symt Date: Tue, 23 Jan 2024 18:19:00 -0600 Subject: [PATCH 1/2] Removal of Hypixel API key from mod --- src/main/java/dev/meyi/bn/BazaarNotifier.java | 2 - .../bn/commands/BazaarNotifierCommand.java | 29 +--- .../dev/meyi/bn/config/Configuration.java | 18 +-- .../dev/meyi/bn/gui/ModuleSettingsGui.java | 10 +- .../java/dev/meyi/bn/gui/SettingsGui.java | 56 ------- .../dev/meyi/bn/handlers/EventHandler.java | 2 +- .../dev/meyi/bn/handlers/UpdateHandler.java | 6 - src/main/java/dev/meyi/bn/json/Order.java | 2 +- .../bn/modules/calc/SuggestionCalculator.java | 40 ++--- .../meyi/bn/utilities/ScheduledEvents.java | 2 +- .../java/dev/meyi/bn/utilities/Utils.java | 145 +++--------------- 11 files changed, 61 insertions(+), 251 deletions(-) diff --git a/src/main/java/dev/meyi/bn/BazaarNotifier.java b/src/main/java/dev/meyi/bn/BazaarNotifier.java index 82010f6..567a471 100644 --- a/src/main/java/dev/meyi/bn/BazaarNotifier.java +++ b/src/main/java/dev/meyi/bn/BazaarNotifier.java @@ -59,12 +59,10 @@ public class BazaarNotifier { public static boolean activeBazaar = true; public static boolean inBazaar = false; public static boolean forceRender = false; - public static boolean apiKeyDisabled = true; public static ArrayList orders = new ArrayList<>(); public static BazaarResponse bazaarDataRaw; - public static JsonObject playerDataFromAPI = new JsonObject(); public static ModuleList modules; public static Configuration config; public static JsonObject resources; diff --git a/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java b/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java index fe9cc31..bfc5d84 100644 --- a/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java +++ b/src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java @@ -48,33 +48,7 @@ public String getCommandUsage(ICommandSender sender) { public void processCommand(ICommandSender ics, String[] args) { if (ics instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) ics; - if (args.length >= 1 && args[0].equalsIgnoreCase("api")) { - if (args.length == 2) { - BazaarNotifier.config.api = args[1]; - try { - if (Utils.validateApiKey()) { - player.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "Your api key has been set.")); - BazaarNotifier.config.api = args[1]; - BazaarNotifier.activeBazaar = true; - } else { - player.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "Your api key is invalid. Please run /api new to get a fresh api key & use that in /bn api (key)")); - } - } catch (IOException e) { - player.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "An error occurred when trying to set your api key. Please re-run the command to try again.")); - e.printStackTrace(); - } - } else { - player.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "Run /bn api (key) to set your api key. Do /api if you need to get your api key.")); - } - } else if (args.length > 0 && args[0].equalsIgnoreCase("reset")) { + 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 @@ -252,7 +226,6 @@ public List addTabCompletionOptions(ICommandSender sender, String[] args if (args.length <= 1) { new ArrayList() { { - add("api"); add("discord"); add("find"); add("reset"); diff --git a/src/main/java/dev/meyi/bn/config/Configuration.java b/src/main/java/dev/meyi/bn/config/Configuration.java index 54ed9b8..95d5131 100644 --- a/src/main/java/dev/meyi/bn/config/Configuration.java +++ b/src/main/java/dev/meyi/bn/config/Configuration.java @@ -3,9 +3,7 @@ import cc.polyfrost.oneconfig.config.Config; import cc.polyfrost.oneconfig.config.annotations.Button; import cc.polyfrost.oneconfig.config.annotations.HUD; -import cc.polyfrost.oneconfig.config.annotations.HypixelKey; import cc.polyfrost.oneconfig.config.annotations.Switch; -import cc.polyfrost.oneconfig.config.annotations.Text; import cc.polyfrost.oneconfig.config.core.OneColor; import cc.polyfrost.oneconfig.config.data.Mod; import cc.polyfrost.oneconfig.config.data.ModType; @@ -27,10 +25,11 @@ public class Configuration extends Config { public Configuration() { super(new Mod("BazaarNotifier", ModType.SKYBLOCK,"/icon.png", new JsonMigrator("./config/BazaarNotifier/config.json")), "bazaarnotifier.json"); initialize(); - addDependency("collectionCheck", "Requires ApiKey", () -> { - if ("".equals(api)) collectionCheck = false; - return !api.isEmpty(); - }); + + /* + * Collection check will remain disabled until the mod reroutes through api.meyi.dev for the interfacing + */ + collectionCheck = false; } public boolean firstLoad = true; @@ -38,13 +37,6 @@ public Configuration() { @JsonName("version") public String version = BazaarNotifier.VERSION; - @JsonName("api") - @HypixelKey - @Text(name = "API-Key", - secure = true - ) - public String api = ""; - @HUD(name = "Suggestion Module", category = "Suggestion Module" ) diff --git a/src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java b/src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java index 7203c46..3789a6f 100644 --- a/src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java +++ b/src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java @@ -44,9 +44,13 @@ public void initGui() { buttonList.add(new GuiButton(ButtonIds.COLLECTION_CHECK.id, getButtonX(), getButtonY(), "Collection Check: " + SettingsGui.getOnOff(BazaarNotifier.config.collectionCheck))); - if ("".equals(BazaarNotifier.config.api)) { - buttonList.get(3).enabled = false; - } + + /* + * This will remain disabled as the way api keys work was changed to be application-based and not user-based + * TODO: Change the collection check to go through api.meyi.dev + */ + buttonList.get(3).enabled = false; + buttonList.add(new GuiButton(ButtonIds.SELLING_OPTION.id, getButtonX(), getButtonY(), "Sort By: " + (BazaarNotifier.config.craftingModule.craftingSortingOption == 0 ? "Instant Sell" : BazaarNotifier.config.craftingModule.craftingSortingOption == 1 ? "Sell Offer" diff --git a/src/main/java/dev/meyi/bn/gui/SettingsGui.java b/src/main/java/dev/meyi/bn/gui/SettingsGui.java index 9bdc8ed..d747d30 100644 --- a/src/main/java/dev/meyi/bn/gui/SettingsGui.java +++ b/src/main/java/dev/meyi/bn/gui/SettingsGui.java @@ -13,11 +13,6 @@ public class SettingsGui extends GuiScreen { - GuiTextField apiKey; - String previousApiKey; - boolean textCleared; - - @Override public void initGui() { super.initGui(); @@ -32,15 +27,6 @@ public void initGui() { buttonId++; buttonList.add(new GuiButton(buttonId, getButtonX(buttonId), getButtonY(buttonId), "Chat Messages: " + SettingsGui.getOnOff(BazaarNotifier.config.showChatMessages))); - buttonId++; - apiKey = new GuiTextField(buttonId, fontRendererObj, getButtonX(buttonId), getButtonY(buttonId), - 200, 20); - apiKey.setMaxStringLength(40); - apiKey.setFocused(true); - apiKey.setCanLoseFocus(false); - apiKey.setText(BazaarNotifier.config.api.equals("") ? "Api key missing" : "Api key set"); - previousApiKey = BazaarNotifier.config.api; - textCleared = false; } @Override @@ -60,7 +46,6 @@ protected void actionPerformed(GuiButton Button) { public void drawScreen(int mouseX, int mouseY, float partialTicks) { this.drawDefaultBackground(); super.drawScreen(mouseX, mouseY, partialTicks); - apiKey.drawTextBox(); } @Override @@ -68,47 +53,6 @@ public boolean doesGuiPauseGame() { return false; } - @Override - protected void keyTyped(char typedChar, int keyCode) throws IOException { - super.keyTyped(typedChar, keyCode); - if (apiKey != null && apiKey.isFocused()) { - if (!textCleared && (apiKey.getText().equalsIgnoreCase("Api key set") - || apiKey.getText().equals("Api key missing"))) { - apiKey.setText(""); - textCleared = true; - } - apiKey.textboxKeyTyped(typedChar, keyCode); - } - } - - @Override - public void onGuiClosed() { - super.onGuiClosed(); - try { - String key = apiKey.getText(); - if (key.equals("") || key.equals(previousApiKey) || key.equals("Api key missing") || key - .equals("Api key set")) { - return; - } - key = key.replaceAll(" ", ""); - if (Utils.validateApiKey(key)) { - BazaarNotifier.config.api = key; - Minecraft.getMinecraft().thePlayer.addChatMessage( - new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.GREEN + "A new api key has been set.")); - } else { - Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "Your api key was not saved because it was invalid.")); - } - - } catch (IOException e) { - e.printStackTrace(); - } catch (NullPointerException | IllegalArgumentException ignored) { - // this only happens if the GUI is closed during initialising - } - } - public int getButtonX(int id) { return SettingsGui.getButtonX(id, width); } diff --git a/src/main/java/dev/meyi/bn/handlers/EventHandler.java b/src/main/java/dev/meyi/bn/handlers/EventHandler.java index ab0ea3f..9985b6c 100644 --- a/src/main/java/dev/meyi/bn/handlers/EventHandler.java +++ b/src/main/java/dev/meyi/bn/handlers/EventHandler.java @@ -122,7 +122,7 @@ public void bazaarChatHandler(ClientChatReceivedEvent e) { @SubscribeEvent public void menuOpenedEvent(GuiOpenEvent e) { - if (e.gui instanceof GuiChest && (!BazaarNotifier.config.api.isEmpty() || BazaarNotifier.apiKeyDisabled)) { + if (e.gui instanceof GuiChest) { IInventory chest = ReflectionHelper.getLowerChestInventory((GuiChest) e.gui); if (chest != null && ((chest.hasCustomName() && ( StringUtils.stripControlCodes(chest.getDisplayName().getUnformattedText()) diff --git a/src/main/java/dev/meyi/bn/handlers/UpdateHandler.java b/src/main/java/dev/meyi/bn/handlers/UpdateHandler.java index 40fd545..dd900f9 100644 --- a/src/main/java/dev/meyi/bn/handlers/UpdateHandler.java +++ b/src/main/java/dev/meyi/bn/handlers/UpdateHandler.java @@ -128,12 +128,6 @@ public void onPlayerJoinEvent(FMLNetworkEvent.ClientConnectedToServerEvent event + "You are on a beta version. Please report any bugs you come across in the discord server.")); } } - - if (BazaarNotifier.config.api.isEmpty() && !BazaarNotifier.apiKeyDisabled) { - Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "The mod doesn't have access to a valid api key yet. Please run /bn api (key) to set your key")); - } } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/dev/meyi/bn/json/Order.java b/src/main/java/dev/meyi/bn/json/Order.java index 72ffdcb..34509c9 100644 --- a/src/main/java/dev/meyi/bn/json/Order.java +++ b/src/main/java/dev/meyi/bn/json/Order.java @@ -55,7 +55,7 @@ public String getProductId() { public void updateStatus() { OrderStatus newOrderStatus = null; - if (!(BazaarNotifier.activeBazaar && (!BazaarNotifier.config.api.isEmpty() || BazaarNotifier.apiKeyDisabled))) { + if (!BazaarNotifier.activeBazaar) { return; } if (OrderType.BUY.equals(this.type)) { diff --git a/src/main/java/dev/meyi/bn/modules/calc/SuggestionCalculator.java b/src/main/java/dev/meyi/bn/modules/calc/SuggestionCalculator.java index 31590d7..fe402c5 100644 --- a/src/main/java/dev/meyi/bn/modules/calc/SuggestionCalculator.java +++ b/src/main/java/dev/meyi/bn/modules/calc/SuggestionCalculator.java @@ -15,28 +15,28 @@ public class SuggestionCalculator { public static void basic() { try { - if (!BazaarNotifier.config.api.isEmpty() || BazaarNotifier.apiKeyDisabled) { - List list = new LinkedList<>(); - for (Map.Entry entry : BazaarNotifier.bazaarDataRaw.products - .entrySet()) { - String key = entry.getKey(); - BazaarItem product = BazaarNotifier.bazaarDataRaw.products.get(key); - - if (!BazaarNotifier.config.suggestionModule.suggestionShowEnchantments && key.startsWith("ENCHANTMENT")) { - continue; - } - - if (!BazaarNotifier.bazaarConv.containsKey(key)) { - BazaarNotifier.bazaarConv.put(key, key); - } - String productId = BazaarNotifier.bazaarConv.get(key); - - list.add(new String[]{productId, Double.toString(calculateEP(product))}); + List list = new LinkedList<>(); + for (Map.Entry entry : BazaarNotifier.bazaarDataRaw.products + .entrySet()) { + String key = entry.getKey(); + BazaarItem product = BazaarNotifier.bazaarDataRaw.products.get(key); + + if (!BazaarNotifier.config.suggestionModule.suggestionShowEnchantments && + key.startsWith("ENCHANTMENT")) { + continue; } - list.sort(Comparator.comparingDouble(o -> Double.parseDouble(o[1]))); - Collections.reverse(list); - SuggestionModule.list = list; + + if (!BazaarNotifier.bazaarConv.containsKey(key)) { + BazaarNotifier.bazaarConv.put(key, key); + } + String productId = BazaarNotifier.bazaarConv.get(key); + + list.add(new String[]{productId, Double.toString(calculateEP(product))}); } + list.sort(Comparator.comparingDouble(o -> Double.parseDouble(o[1]))); + Collections.reverse(list); + SuggestionModule.list = list; + } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/dev/meyi/bn/utilities/ScheduledEvents.java b/src/main/java/dev/meyi/bn/utilities/ScheduledEvents.java index 0b0336b..5e9c6da 100644 --- a/src/main/java/dev/meyi/bn/utilities/ScheduledEvents.java +++ b/src/main/java/dev/meyi/bn/utilities/ScheduledEvents.java @@ -86,7 +86,7 @@ public ScheduledExecutorService craftingLoop() { public ScheduledExecutorService getBazaarData() { ScheduledExecutorService ex = Executors.newScheduledThreadPool(1); ex.scheduleAtFixedRate(() -> { - if (BazaarNotifier.activeBazaar && (!BazaarNotifier.config.api.isEmpty() || BazaarNotifier.apiKeyDisabled)) { + if (BazaarNotifier.activeBazaar) { try { BazaarNotifier.bazaarDataRaw = Utils.getBazaarData(); } catch (Exception t) { diff --git a/src/main/java/dev/meyi/bn/utilities/Utils.java b/src/main/java/dev/meyi/bn/utilities/Utils.java index 16ccdf4..4a8441d 100644 --- a/src/main/java/dev/meyi/bn/utilities/Utils.java +++ b/src/main/java/dev/meyi/bn/utilities/Utils.java @@ -9,7 +9,6 @@ import com.google.gson.stream.JsonReader; import dev.meyi.bn.BazaarNotifier; import dev.meyi.bn.json.resp.BazaarResponse; - import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -17,11 +16,9 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; import net.minecraft.client.Minecraft; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -31,21 +28,13 @@ import org.apache.http.impl.client.HttpClientBuilder; public class Utils { - - private static final Pattern uuidMatcher = Pattern - .compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$", - Pattern.CASE_INSENSITIVE); private static String playerUUID = ""; public static BazaarResponse getBazaarData() throws IOException { Gson gson = new Gson(); CloseableHttpClient client = HttpClientBuilder.create().build(); - String apiBit = ""; - if (!BazaarNotifier.apiKeyDisabled) { - apiBit = "?key=" + BazaarNotifier.config.api; - } HttpGet request = new HttpGet( - "https://api.hypixel.net/skyblock/bazaar" + apiBit); + "https://api.hypixel.net/skyblock/bazaar"); HttpResponse response = client.execute(request); String result = IOUtils.toString(new BufferedReader @@ -62,87 +51,36 @@ public static BazaarResponse getBazaarData() throws IOException { } + /** + * Waiting until a proper backend is built out before completing this feature. + * + * @see the + * old code + */ public static List unlockedRecipes() throws IOException { Gson gson = new Gson(); - if (BazaarNotifier.config.collectionCheck && !BazaarNotifier.config.api.isEmpty()) { - - CloseableHttpClient client = HttpClientBuilder.create().build(); - if (playerUUID.equals("")) { - HttpGet request = new HttpGet( - "https://api.mojang.com/users/profiles/minecraft/" + Minecraft.getMinecraft() - .getSession().getUsername()); // Change this to your username if testing - HttpResponse response = client.execute(request); - - String uuidResponse = IOUtils - .toString(new BufferedReader(new InputStreamReader(response.getEntity().getContent()))); - - try { - playerUUID = gson.fromJson(uuidResponse, JsonObject.class).get("id").getAsString(); - } catch (JsonSyntaxException e) { - return null; - } - } - - HttpGet request = new HttpGet( - "https://api.hypixel.net/skyblock/profiles?key=" + BazaarNotifier.config.api + "&uuid=" - + playerUUID); - HttpResponse response = client.execute(request); - - JsonReader jsonReader = new JsonReader( - new BufferedReader(new InputStreamReader(response.getEntity().getContent()))); - jsonReader.setLenient(true); - - JsonObject results = null; - - try { - results = gson.fromJson(jsonReader, JsonObject.class); - } catch (Exception e) { - e.printStackTrace(); - } - - client.close(); - int profileIndex = 0; - if (results == null || !results.get("success").getAsBoolean() || !results.has("profiles")) { - return null; - } - - for (int i = 0; i < results.get("profiles").getAsJsonArray().size(); i++) { - JsonObject playerDataFromAPI = results.getAsJsonArray("profiles").get(i).getAsJsonObject(); - if (playerDataFromAPI.has("selected")) { - if (playerDataFromAPI.get("selected").getAsBoolean()) { - profileIndex = i; + if (BazaarNotifier.config.collectionCheck) { + try (CloseableHttpClient client = HttpClientBuilder.create().build()) { + if (playerUUID.equals("")) { + HttpGet request = new HttpGet( + "https://api.mojang.com/users/profiles/minecraft/" + Minecraft.getMinecraft() + .getSession().getUsername()); // Change this to your username if testing + HttpResponse response = client.execute(request); + + String uuidResponse = IOUtils + .toString( + new BufferedReader(new InputStreamReader(response.getEntity().getContent()))); + + try { + playerUUID = gson.fromJson(uuidResponse, JsonObject.class).get("id").getAsString(); + } catch (JsonSyntaxException e) { + return null; } } } - BazaarNotifier.playerDataFromAPI = results.getAsJsonArray("profiles") - .get(profileIndex).getAsJsonObject().getAsJsonObject("members") - .getAsJsonObject(playerUUID); - if (!BazaarNotifier.playerDataFromAPI.has("unlocked_coll_tiers") - || !BazaarNotifier.playerDataFromAPI.has("slayer_bosses")) { - System.out.println("could not load unlocked collection tiers from API"); - return null; - } - List unlockedCollections = new ArrayList<>(); - - BazaarNotifier.playerDataFromAPI.getAsJsonArray("unlocked_coll_tiers") - .forEach(cmd -> unlockedCollections.add(cmd.getAsString())); - - JsonObject slayer = BazaarNotifier.playerDataFromAPI.getAsJsonObject("slayer_bosses"); - Set> set = slayer.entrySet(); - for (Map.Entry entry : set) { - Set> set2 = slayer.getAsJsonObject(entry.getKey()) - .getAsJsonObject("claimed_levels").entrySet(); - for (Map.Entry entry2 : set2) { - unlockedCollections.add( - entry.getKey().toUpperCase() + entry2.getKey().replace("level", "").toUpperCase()); - } - } - return unlockedCollections; - } else { - BazaarNotifier.config.collectionCheck = false; - - return null; } + return null; } public static boolean isJSONValid(String jsonInString) { @@ -155,39 +93,6 @@ public static boolean isJSONValid(String jsonInString) { } } - public static boolean validateApiKey(String key) throws IOException { - Gson gson = new Gson(); - if (uuidMatcher.matcher(key).find()) { - try { - if (gson.fromJson(IOUtils.toString(new BufferedReader - (new InputStreamReader(HttpClientBuilder.create().build().execute(new HttpGet( - "https://api.hypixel.net/key?key=" + key)).getEntity() - .getContent()))), JsonObject.class).getAsJsonObject().get("success") - .getAsBoolean()) { - return true; - } else { - BazaarNotifier.config.collectionCheck = false; - return false; - } - } catch (JsonSyntaxException e) { - BazaarNotifier.config.collectionCheck = false; - return false; - } - } - BazaarNotifier.config.collectionCheck = false; - return false; - } - - public static boolean validateApiKey() throws IOException { - if (validateApiKey(BazaarNotifier.config.api)) { - return true; - } else { - BazaarNotifier.config.collectionCheck = false; - return false; - } - } - - public static void updateResources() throws IOException { Gson gson = new Gson(); HttpGet request; From e80cb5464501deb2ceb3cb0b10c6e380fcfa5574 Mon Sep 17 00:00:00 2001 From: symt Date: Thu, 25 Jan 2024 11:47:48 -0600 Subject: [PATCH 2/2] Fixing collection check disabler --- src/main/java/dev/meyi/bn/config/Configuration.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/dev/meyi/bn/config/Configuration.java b/src/main/java/dev/meyi/bn/config/Configuration.java index 95d5131..669d338 100644 --- a/src/main/java/dev/meyi/bn/config/Configuration.java +++ b/src/main/java/dev/meyi/bn/config/Configuration.java @@ -26,10 +26,9 @@ public Configuration() { super(new Mod("BazaarNotifier", ModType.SKYBLOCK,"/icon.png", new JsonMigrator("./config/BazaarNotifier/config.json")), "bazaarnotifier.json"); initialize(); - /* - * Collection check will remain disabled until the mod reroutes through api.meyi.dev for the interfacing - */ - collectionCheck = false; + addListener("collectionCheck", () -> { + collectionCheck = false; + }); } public boolean firstLoad = true;