Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removal of Hypixel API key from mod #109

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/main/java/dev/meyi/bn/BazaarNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Order> 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;
Expand Down
29 changes: 1 addition & 28 deletions src/main/java/dev/meyi/bn/commands/BazaarNotifierCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -252,7 +226,6 @@ public List<String> addTabCompletionOptions(ICommandSender sender, String[] args
if (args.length <= 1) {
new ArrayList<String>() {
{
add("api");
add("discord");
add("find");
add("reset");
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/dev/meyi/bn/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,9 +25,9 @@ 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();

addListener("collectionCheck", () -> {
collectionCheck = false;
});
}

Expand All @@ -38,13 +36,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"
)
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/dev/meyi/bn/gui/ModuleSettingsGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 0 additions & 56 deletions src/main/java/dev/meyi/bn/gui/SettingsGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

public class SettingsGui extends GuiScreen {

GuiTextField apiKey;
String previousApiKey;
boolean textCleared;


@Override
public void initGui() {
super.initGui();
Expand All @@ -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
Expand All @@ -60,55 +46,13 @@ protected void actionPerformed(GuiButton Button) {
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawDefaultBackground();
super.drawScreen(mouseX, mouseY, partialTicks);
apiKey.drawTextBox();
}

@Override
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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/meyi/bn/handlers/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/dev/meyi/bn/handlers/UpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/meyi/bn/json/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/dev/meyi/bn/modules/calc/SuggestionCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ public class SuggestionCalculator {

public static void basic() {
try {
if (!BazaarNotifier.config.api.isEmpty() || BazaarNotifier.apiKeyDisabled) {
List<String[]> list = new LinkedList<>();
for (Map.Entry<String, BazaarItem> 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<String[]> list = new LinkedList<>();
for (Map.Entry<String, BazaarItem> 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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/meyi/bn/utilities/ScheduledEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading
Loading