Skip to content

Commit

Permalink
v1.6.0 Bank Module Improvements (#97)
Browse files Browse the repository at this point in the history
* Crash Fix & Bank Module Update

* Capitalization of P
  • Loading branch information
symt authored Dec 22, 2022
1 parent 05ebaff commit a236ac3
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 275 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "dev.meyi.bazaarnotifier"
version = "1.5.0"
version = "1.6.0"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/dev/meyi/bn/BazaarNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.MalformedJsonException;
import dev.meyi.bn.commands.BazaarNotifierCommand;
import dev.meyi.bn.config.Configuration;
import dev.meyi.bn.handlers.ChestTickHandler;
Expand All @@ -20,7 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -39,7 +41,7 @@
public class BazaarNotifier {

public static final String MODID = "BazaarNotifier";
public static final String VERSION = "1.5.0";
public static final String VERSION = "1.6.0";
public static final String prefix =
EnumChatFormatting.GOLD + "[" + EnumChatFormatting.YELLOW + "BN" + EnumChatFormatting.GOLD
+ "] " + EnumChatFormatting.RESET;
Expand All @@ -55,7 +57,6 @@ public class BazaarNotifier {

public static boolean activeBazaar = true;
public static boolean inBazaar = false;
public static boolean inBank = false;
public static boolean forceRender = false;
public static boolean validApiKey = false;
public static boolean apiKeyDisabled = true;
Expand Down Expand Up @@ -93,15 +94,16 @@ public void preInit(FMLPreInitializationEvent event) {
bnDir.mkdirs();
configFile = new File(bnDir, "config.json");
resourcesFile = new File(bnDir, "resources.json");
String configString = null;
JsonReader configString = null;
Gson gson = new Gson();
try {
if (configFile.isFile()) {
try {
configString = new String(Files.readAllBytes(Paths.get(configFile.getPath())));
configString = new JsonReader(new StringReader(new String(Files.readAllBytes(Paths.get(configFile.getPath())))));
configString.setLenient(true);
config = gson.fromJson(configString, Configuration.class);
config.version = BazaarNotifier.VERSION;
} catch (JsonSyntaxException e) {
} catch (JsonSyntaxException | MalformedJsonException e) {
e.printStackTrace();
config = Configuration.createDefaultConfig();
}
Expand All @@ -112,18 +114,21 @@ public void preInit(FMLPreInitializationEvent event) {
try {
if (resourcesFile.isFile()) {
try {
String resourcesString = new String(
Files.readAllBytes(Paths.get(resourcesFile.getPath())));
resources = gson.fromJson(resourcesString, JsonObject.class);
JsonReader resourcesReader = new JsonReader(new StringReader(new String(
Files.readAllBytes(Paths.get(resourcesFile.getPath())))));
resourcesReader.setLenient(true);
resources = gson.fromJson(resourcesReader, JsonObject.class);
} catch (JsonSyntaxException | ClassCastException e) {
e.printStackTrace();
Reader reader = new InputStreamReader(Objects.requireNonNull(
BazaarNotifier.class.getResourceAsStream("/resources.json")), StandardCharsets.UTF_8);
JsonReader reader = new JsonReader(new InputStreamReader(Objects.requireNonNull(
BazaarNotifier.class.getResourceAsStream("/resources.json")), StandardCharsets.UTF_8));
reader.setLenient(true);
resources = gson.fromJson(reader, JsonObject.class);
}
} else {
Reader reader = new InputStreamReader(Objects.requireNonNull(
BazaarNotifier.class.getResourceAsStream("/resources.json")), StandardCharsets.UTF_8);
JsonReader reader = new JsonReader(new InputStreamReader(Objects.requireNonNull(
BazaarNotifier.class.getResourceAsStream("/resources.json")), StandardCharsets.UTF_8));
reader.setLenient(true);
resources = gson.fromJson(reader, JsonObject.class);
}
} catch (IOException e) {
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/dev/meyi/bn/handlers/ChestTickHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,6 @@ public void onChestTick(TickEvent e) {
chest.getDisplayName().getUnformattedText());
}
}
} else if (BazaarNotifier.inBank
&& Minecraft.getMinecraft().currentScreen instanceof GuiChest) {
IInventory chest = ReflectionHelper.getLowerChestInventory(
(GuiChest) Minecraft.getMinecraft().currentScreen);
if (chest == null) {
return;
}
String chestName = chest.getDisplayName().getUnformattedText().toLowerCase();
if (chestName.contains("personal bank account") && !chestName.contains("upgrade")) {
BankCalculator.extractBankFromItemDescription(chest, false);
} else if (chestName.contains("co-op bank account") && !chestName.contains("upgrade")) {
BankCalculator.extractBankFromItemDescription(chest, true);
} else if (chestName.equals("bank deposit") || chestName.equals("bank withdrawal")) {
BankCalculator.isOnDangerousPage = true;
BankCalculator.purseInBank = BankCalculator.getPurse();
}

} else if (!BazaarNotifier.inBazaar) { // if you aren't in the bazaar, this should be clear
ChestTickHandler.lastScreenDisplayName = "";
}
Expand Down
72 changes: 10 additions & 62 deletions src/main/java/dev/meyi/bn/handlers/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ public void bazaarChatHandler(ClientChatReceivedEvent e) {
}
String message = StringUtils.stripControlCodes(e.message.getUnformattedText());

if (message.startsWith("[Bazaar] Claimed") || message.startsWith("[Bazaar] Bought") || message.startsWith("[Bazaar] Sold")) {
BankCalculator.evaluate(message);
}

if (message.startsWith("Buy Order Setup!") || message.startsWith("Sell Offer Setup!")
|| message.startsWith("[Bazaar] Buy Order Setup!") || message.startsWith(
"[Bazaar] Sell Offer Setup!")) {
if (productVerify[0] != null && productVerify[1] != null && productVerify[0].equals(
BazaarNotifier.bazaarConv.inverse().get(message.split("x ", 2)[1].split(" for ")[0]))
&& productVerify[1].equals(message.split("! ")[1].split(" for ")[0])) {
BazaarNotifier.orders.add(verify);
BankCalculator.getBazaarProfit();
verify = null;
productVerify = new String[2];
}
Expand Down Expand Up @@ -120,11 +123,7 @@ public void bazaarChatHandler(ClientChatReceivedEvent e) {
}
}
} else if (message.startsWith("Bazaar! Claimed ") || message.startsWith("[Bazaar] Claimed")) {
ChestTickHandler.lastScreenDisplayName = ""; // Force update on next tick
// ChestTickHandler.updateBazaarOrders(
// ((GuiChest) Minecraft.getMinecraft().currentScreen).lowerChestInventory);
} else if (message.startsWith("Welcome to Hypixel SkyBlock!")) {
BankCalculator.getPurse();
ChestTickHandler.lastScreenDisplayName = "";
} else if (message.startsWith("Your new API key is")) {
String apiKey = message.split("key is ")[1];
try {
Expand Down Expand Up @@ -157,77 +156,26 @@ public void menuOpenedEvent(GuiOpenEvent e) {
if (e.gui instanceof GuiChest && (BazaarNotifier.validApiKey
|| BazaarNotifier.apiKeyDisabled)) {
IInventory chest = ReflectionHelper.getLowerChestInventory((GuiChest) e.gui);
if (chest == null) {
return;
}
if ((chest.hasCustomName() && (

if (chest != null && ((chest.hasCustomName() && (
StringUtils.stripControlCodes(chest.getDisplayName().getUnformattedText())
.startsWith("Bazaar") || StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText())
.equalsIgnoreCase("How much do you want to pay?") || StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText())
.matches("Confirm (Buy|Sell) (Order|Offer)")) || StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText()).contains("Bazaar"))
|| BazaarNotifier.forceRender) {
if (!BazaarNotifier.inBazaar) {
BazaarNotifier.inBazaar = true;
if (!BankCalculator.orderWait) {
BankCalculator.purseLast = BankCalculator.getPurse();
}
}
|| BazaarNotifier.forceRender)) {
BazaarNotifier.inBazaar = true;
}
} else if (e.gui instanceof GuiEditSign) {
} else if (e.gui == null || e.gui instanceof GuiEditSign) {
BazaarNotifier.inBazaar = false;
}

if (e.gui == null && BazaarNotifier.inBazaar) {
BazaarNotifier.inBazaar = false;
Thread t = new Thread(() -> {
BankCalculator.orderWait = true;
try {
Thread.sleep(1000);
BankCalculator.getBazaarProfit();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
BankCalculator.orderWait = false;
});
t.start();
}

if (e.gui == null && BazaarNotifier.inBank) {
BazaarNotifier.inBank = false;
}

if (e.gui == null && BankCalculator.isOnDangerousPage) {
BankCalculator.isOnDangerousPage = false;
Thread t = new Thread(() -> {
try {
Thread.sleep(1000);
BankCalculator.bank += (BankCalculator.purseInBank - BankCalculator.getPurse());
} catch (InterruptedException ex) {
ex.printStackTrace();
}
});
t.start();

}

if (e.gui instanceof GuiChest) {
IInventory chest = ReflectionHelper.getLowerChestInventory((GuiChest) e.gui);
BazaarNotifier.inBank =
chest != null && chest.hasCustomName() && StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText()).contains("Bank")
&& !StringUtils.stripControlCodes(chest.getDisplayName().getUnformattedText())
.contains("Bank Account Upgrades");
}
}

@SubscribeEvent
public void disconnectEvent(ClientDisconnectionFromServerEvent e) {
BazaarNotifier.inBazaar = false;
BazaarNotifier.inBank = false;
BankCalculator.isOnDangerousPage = false;
}

@SubscribeEvent
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/dev/meyi/bn/json/Exchange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.meyi.bn.json;

import dev.meyi.bn.BazaarNotifier;
import dev.meyi.bn.json.Order.OrderType;
import dev.meyi.bn.modules.calc.CraftingCalculator;
import java.util.Map;

public class Exchange {

private final String productId;
private final double pricePerUnit;
private int amount;
private final OrderType type;

private final Map<String, Integer> craftingResources;

public Exchange(OrderType type, String productId, double pricePerUnit, int amount) {
this.type = type;
this.productId = productId;
this.pricePerUnit = pricePerUnit;
this.amount = amount;

if (canCraft()) {
craftingResources = CraftingCalculator.getMaterialsMap(productId);
} else {
craftingResources = null;
}
}

public String getProductId() {
return productId;
}

public Map<String, Integer> getCraftingResources() {
return craftingResources;
}

public double getPricePerUnit() {
return pricePerUnit;
}

public int getAmount() {
return amount;
}

public void removeAmount(int remove) {
this.amount -= remove;
}

public void addAmount(int add) {
this.amount += add;
}

public OrderType getType() {
return type;
}

public boolean matchesOrder(Exchange exchange) {
return this.type != exchange.type && this.productId.equals(exchange.productId);
}

public boolean canCraft() {
return BazaarNotifier.enchantCraftingList.getAsJsonObject("other").has(productId)
&& type == OrderType.SELL;
}

@Override
public boolean equals(Object obj) {
return (obj instanceof Exchange) && this.pricePerUnit == ((Exchange) obj).pricePerUnit
&& this.productId.equals(((Exchange) obj).productId);
}
}
Loading

0 comments on commit a236ac3

Please sign in to comment.