Skip to content

Commit

Permalink
1.7.5: More bug fixes (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Detlev1 authored and symt committed Sep 19, 2024
1 parent 51fec08 commit b4b089d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 117 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group = "dev.meyi.bazaarnotifier"
version = "1.7.4"
version = "1.7.5"
val mod_id = "bazaarnotifier"

java {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/meyi/bn/BazaarNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class BazaarNotifier {

public static final String MODID = "BazaarNotifier";
public static final String VERSION = "1.7.4";
public static final String VERSION = "1.7.5";
public static final String prefix =
EnumChatFormatting.GOLD + "[" + EnumChatFormatting.YELLOW + "BN" + EnumChatFormatting.GOLD
+ "] " + EnumChatFormatting.RESET;
Expand Down Expand Up @@ -122,7 +122,7 @@ public void preInit(FMLPreInitializationEvent event) {

try {
Utils.updateResources();
} catch (IOException | KeyManagementException | NoSuchAlgorithmException e) {
} catch (IOException | KeyManagementException | NoSuchAlgorithmException | ClassCastException e) {
System.err.println("Error while getting resources from GitHub");
e.printStackTrace();
JsonObject bazaarConversions = resources.getAsJsonObject("bazaarConversions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void processCommand(ICommandSender ics, String[] args) {
try {
Utils.updateResources();
date = System.currentTimeMillis();
} catch (IOException | KeyManagementException | NoSuchAlgorithmException e) {
} catch (IOException | KeyManagementException | NoSuchAlgorithmException | ClassCastException e) {
player.addChatMessage(new ChatComponentText(
BazaarNotifier.prefix + EnumChatFormatting.RED
+ "Resource update failed. Please try again."));
Expand Down
230 changes: 125 additions & 105 deletions src/main/java/dev/meyi/bn/handlers/ChestTickHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,84 +46,87 @@ public static void updateBazaarOrders(IInventory chest) {
}

for (ItemStack item : items) {
if (item != null && Item.itemRegistry.getIDForObject(item.getItem()) != 160 // Glass
&& Item.itemRegistry.getIDForObject(item.getItem()) != 102 // Glass
&& Item.itemRegistry.getIDForObject(item.getItem()) != 262 // Arrow
&& Item.itemRegistry.getIDForObject(item.getItem()) != 154 ) { //Hopper
List<String> lore = Utils.getLoreFromItemStack(item);

Pattern p2 = Pattern.compile("(BUY|SELL):? (.*)");
Matcher m2 = p2.matcher(StringUtils.stripControlCodes(item.getDisplayName()));
String displayName;
Order.OrderType type;
if (m2.find()) {
displayName = m2.group(2);
type = "sell".equalsIgnoreCase(m2.group(1)) ? Order.OrderType.SELL : Order.OrderType.BUY;
if(item == null) continue;
int itemID = Item.itemRegistry.getIDForObject(item.getItem());
if ( itemID == 160 // Glass
|| itemID == 102 // Glass
|| itemID == 262 // Arrow
|| (itemID == 154 && StringUtils.stripControlCodes(item.getDisplayName()).equals("Claim All Coins"))) { //Hopper
continue;
} //Hopper
List<String> lore = Utils.getLoreFromItemStack(item);

Pattern p2 = Pattern.compile("(BUY|SELL):? (.*)");
Matcher m2 = p2.matcher(StringUtils.stripControlCodes(item.getDisplayName()));
String displayName;
Order.OrderType type;
if (m2.find()) {
displayName = m2.group(2);
type = "sell".equalsIgnoreCase(m2.group(1)) ? Order.OrderType.SELL : Order.OrderType.BUY;
} else {
System.err.println("Bazaar item header incorrect. Aborting!");
return;
}

if (BazaarNotifier.bazaarConv.containsValue(displayName)) {
String priceString;
if (lore.get(4).toLowerCase().contains("expire")) {
priceString = StringUtils.stripControlCodes(lore.get(6)).replaceAll(",", "")
.split(" ")[3];
} else if (lore.get(5).toLowerCase().contains("expire")) {
priceString = StringUtils.stripControlCodes(lore.get(7)).replaceAll(",", "")
.split(" ")[3];
} else {
System.err.println("Bazaar item header incorrect. Aborting!");
return;
priceString = StringUtils.stripControlCodes(
lore.get((lore.get(3).startsWith("Filled:")) ? 5 : 4).replaceAll(",", "")
.split(" ")[3]);
}

if (BazaarNotifier.bazaarConv.containsValue(displayName)) {
String priceString;
if (lore.get(4).toLowerCase().contains("expire")) {
priceString = StringUtils.stripControlCodes(lore.get(6)).replaceAll(",", "")
.split(" ")[3];
} else if (lore.get(5).toLowerCase().contains("expire")) {
priceString = StringUtils.stripControlCodes(lore.get(7)).replaceAll(",", "")
.split(" ")[3];
} else {
priceString = StringUtils.stripControlCodes(
lore.get((lore.get(3).startsWith("Filled:")) ? 5 : 4).replaceAll(",", "")
.split(" ")[3]);
int orderInQuestion = -1;
for (int j = 0; j < BazaarNotifier.orders.size(); j++) {
Order order = BazaarNotifier.orders.get(j);
if (priceString.equalsIgnoreCase(order.priceString) && type.equals(
order.type)) { // Todo check product also causing problems
orderInQuestion = j;
break;
}
int orderInQuestion = -1;
for (int j = 0; j < BazaarNotifier.orders.size(); j++) {
Order order = BazaarNotifier.orders.get(j);
if (priceString.equalsIgnoreCase(order.priceString) && type.equals(
order.type)) { // Todo check product also causing problems
orderInQuestion = j;
}
if (orderInQuestion != -1) {
verifiedOrders[orderInQuestion] = 1;
Order order = BazaarNotifier.orders.get(orderInQuestion);
int totalAmount = order.startAmount;
int amountLeft = Utils.getOrderAmountLeft(lore, totalAmount);
if (amountLeft > 0) {
order.setAmountRemaining(amountLeft);
}
} else if (playerName != null) {
Pattern p3 = Pattern.compile("By: (?:\\[.*\\] )?(.*)");
String creator = "";
for (String line : lore) {
Matcher m3 = p3.matcher(line);
if (m3.find()) {
creator = m3.group(1);
break;
}
}
if (orderInQuestion != -1) {
verifiedOrders[orderInQuestion] = 1;
Order order = BazaarNotifier.orders.get(orderInQuestion);
int totalAmount = order.startAmount;
int amountLeft = Utils.getOrderAmountLeft(lore, totalAmount);
if (amountLeft > 0) {
order.setAmountRemaining(amountLeft);
}
} else if (playerName != null) {
Pattern p3 = Pattern.compile("By: (?:\\[.*\\] )?(.*)");
String creator = "";
for (String line : lore) {
Matcher m3 = p3.matcher(line);
if (m3.find()) {
creator = m3.group(1);
break;
}
if (creator.equals(playerName) || creator.isEmpty()) { //isEmpty for non Coop Islands
if (lore.get(4).toLowerCase().contains("expire") || lore.get(5).toLowerCase().contains("expire")) {
continue;
}
if (creator.equals(playerName) || creator.isEmpty()) { //isEmpty for non Coop Islands
if (lore.get(4).toLowerCase().contains("expire") || lore.get(5).toLowerCase().contains("expire")) {
continue;
}
String totalAmount = lore.get(2).split(" ")[2];
int startAmount = Integer.parseInt(totalAmount.substring(0, totalAmount.length()-1).replace(",", ""));
Order newOrder = new Order(displayName, startAmount, Double.parseDouble(priceString), priceString, type);
newOrder.setAmountRemaining(Utils.getOrderAmountLeft(lore, startAmount));
if (newOrder.getAmountRemaining() != 0) {
BazaarNotifier.orders.add(newOrder);
verifiedOrders = Arrays.copyOf(verifiedOrders, verifiedOrders.length + 1);
verifiedOrders[verifiedOrders.length-1] = 1;
}
String totalAmount = lore.get(2).split(" ")[2];
int startAmount = Integer.parseInt(totalAmount.substring(0, totalAmount.length()-1).replace(",", ""));
Order newOrder = new Order(displayName, startAmount, Double.parseDouble(priceString), priceString, type);
newOrder.setAmountRemaining(Utils.getOrderAmountLeft(lore, startAmount));
if (newOrder.getAmountRemaining() != 0) {
BazaarNotifier.orders.add(newOrder);
verifiedOrders = Arrays.copyOf(verifiedOrders, verifiedOrders.length + 1);
verifiedOrders[verifiedOrders.length-1] = 1;
}
}
} else {
System.out.println(BazaarNotifier.orders);
System.err.println("Some orders weren't found! Bad display name: " + displayName);
return;
}
} else {
System.out.println(BazaarNotifier.orders);
System.err.println("Some orders weren't found! Bad display name: " + displayName);
return;
}
}
for (int i = verifiedOrders.length - 1; i >= 0; i--) {
Expand All @@ -135,58 +138,74 @@ public static void updateBazaarOrders(IInventory chest) {

@SubscribeEvent
public void onChestTick(TickEvent e) {
if (e.phase == Phase.END) {
if (Minecraft.getMinecraft().currentScreen instanceof GuiChest && BazaarNotifier.inBazaar
&& BazaarNotifier.activeBazaar) {

IInventory chest = ReflectionHelper.getLowerChestInventory(
(GuiChest) Minecraft.getMinecraft().currentScreen);
if (chest == null) {
return;
}
String chestName = chest.getDisplayName().getUnformattedText().toLowerCase();
try {
if (e.phase == Phase.END) {
if (Minecraft.getMinecraft().currentScreen instanceof GuiChest && BazaarNotifier.inBazaar
&& BazaarNotifier.activeBazaar) {

if (chest.hasCustomName() && !lastScreenDisplayName.equalsIgnoreCase(chestName)) {
if (chestName.equals("confirm buy order") || chestName.equals("confirm sell offer")) {
IInventory chest = ReflectionHelper.getLowerChestInventory(
(GuiChest) Minecraft.getMinecraft().currentScreen);
if (chest == null) {
return;
}
String chestName = chest.getDisplayName().getUnformattedText().toLowerCase();

if (chest.getStackInSlot(13) != null) {
lastScreenDisplayName = StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText());
orderConfirmation(chest);
}
if (chest.hasCustomName() && !lastScreenDisplayName.equalsIgnoreCase(chestName)) {
if (chestName.equals("confirm buy order") || chestName.equals("confirm sell offer")) {
if (chest.getStackInSlot(13) != null) {
if (orderConfirmation(chest)) { // Don´t reset the lastScreenDisplayName to retry in the next tick
lastScreenDisplayName = StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText());
}
}

} else if (chestName.contains("bazaar orders")) {
if (chest.getStackInSlot(chest.getSizeInventory() - 5) != null &&
Item.itemRegistry.getIDForObject(
chest.getStackInSlot(chest.getSizeInventory() - 5).getItem()) == 262) {
} else if (chestName.contains("bazaar orders")) {
if (chest.getStackInSlot(chest.getSizeInventory() - 5) != null &&
Item.itemRegistry.getIDForObject(
chest.getStackInSlot(chest.getSizeInventory() - 5).getItem()) == 262) {
lastScreenDisplayName = StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText());
updateBazaarOrders(chest);
}
} else if (chestName.contains("bazaar")) {
lastScreenDisplayName = StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText());
updateBazaarOrders(chest);
chest.getDisplayName().getUnformattedText());
}
} else if (chestName.contains("bazaar")) {
lastScreenDisplayName = StringUtils.stripControlCodes(
chest.getDisplayName().getUnformattedText());
}
} else if (!BazaarNotifier.inBazaar) { // if you aren't in the bazaar, this should be clear
ChestTickHandler.lastScreenDisplayName = "";
}
} else if (!BazaarNotifier.inBazaar) { // if you aren't in the bazaar, this should be clear
ChestTickHandler.lastScreenDisplayName = "";
}
}catch (Exception ex){
Minecraft.getMinecraft().thePlayer.addChatMessage(
new ChatComponentText(BazaarNotifier.prefix + EnumChatFormatting.RED +
"The onChestTick method ran into an error. Please report this in the discord server"));
ex.printStackTrace();
}
}

private void orderConfirmation(IInventory chest) {
/**
* @return success
*/
private boolean orderConfirmation(IInventory chest) {

if (chest.getStackInSlot(13) != null) {
String priceString = "";
String product = "";
double price = 0;

String priceString = StringUtils.stripControlCodes(
chest.getStackInSlot(13).getTagCompound().getCompoundTag("display").getTagList("Lore", 8)
.getStringTagAt(2)).split(" ")[3].replaceAll(",", "");
double price = Double.parseDouble(priceString);

String product = StringUtils.stripControlCodes(
chest.getStackInSlot(13).getTagCompound().getCompoundTag("display").getTagList("Lore", 8)
.getStringTagAt(4)).split("x ", 2)[1];
try {
priceString = StringUtils.stripControlCodes(
chest.getStackInSlot(13).getTagCompound().getCompoundTag("display").getTagList("Lore", 8)
.getStringTagAt(2)).split(" ")[3].replaceAll(",", "");
price = Double.parseDouble(priceString);

product = StringUtils.stripControlCodes(
chest.getStackInSlot(13).getTagCompound().getCompoundTag("display").getTagList("Lore", 8)
.getStringTagAt(4)).split("x ", 2)[1];
}catch (Exception e){
return false;
}
String productName;

if (!BazaarNotifier.bazaarConv.containsValue(product)) {
Expand Down Expand Up @@ -230,6 +249,7 @@ private void orderConfirmation(IInventory chest) {
EventHandler.verify = new Order(product, amount, price, priceString, type);
}
}
return true;
}
@SubscribeEvent
public void renderInChest(GuiScreenEvent.BackgroundDrawnEvent e){
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/dev/meyi/bn/modules/module/CraftingModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ && getMouseCoordinateY()
(int) (Minecraft.getMinecraft().fontRendererObj.getStringWidth("/ Sell Offer ") * scale)
: 0;
width[3] = BazaarNotifier.config.craftingModule.showProfitPerMil ?
(int) (Minecraft.getMinecraft().fontRendererObj.getStringWidth("/ Profit %")
* scale)
(int) (Minecraft.getMinecraft().fontRendererObj.getStringWidth("/ Profit %") * scale)
: 0;

for (int i : width) {
Expand Down Expand Up @@ -163,7 +162,8 @@ && getMouseCoordinateY()
helperLine.add(new ColoredText(" Instant Sell",
BazaarNotifier.config.craftingModule.craftingSortingOption == 0 ?
new Color(141, 152, 201) : BazaarNotifier.config.infoColor.toJavaColor()));
if (BazaarNotifier.config.craftingModule.showSellOfferProfit) {
if (BazaarNotifier.config.craftingModule.showSellOfferProfit
|| BazaarNotifier.config.craftingModule.showProfitPerMil) {
helperLine.add(new ColoredText(" /", BazaarNotifier.config.infoColor.toJavaColor()));
}
}
Expand All @@ -172,11 +172,11 @@ && getMouseCoordinateY()
BazaarNotifier.config.craftingModule.craftingSortingOption == 1 ?
new Color(141, 152, 201) : BazaarNotifier.config.infoColor.toJavaColor()));
if (BazaarNotifier.config.craftingModule.showProfitPerMil) {
helperLine.add(new ColoredText(" / ", BazaarNotifier.config.infoColor.toJavaColor()));
helperLine.add(new ColoredText(" /", BazaarNotifier.config.infoColor.toJavaColor()));
}
}
if (BazaarNotifier.config.craftingModule.showProfitPerMil) {
helperLine.add(new ColoredText("Profit %",
helperLine.add(new ColoredText(" Profit %",
BazaarNotifier.config.craftingModule.craftingSortingOption == 2 ?
new Color(141, 152, 201) : BazaarNotifier.config.infoColor.toJavaColor()));
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/meyi/bn/utilities/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static boolean isJSONValid(String jsonInString) {
}
}

public static void updateResources() throws IOException, KeyManagementException, NoSuchAlgorithmException {
public static void updateResources() throws IOException, KeyManagementException, NoSuchAlgorithmException, ClassCastException {
Gson gson = new Gson();
HttpGet request;
HttpResponse response;
Expand All @@ -141,7 +141,7 @@ public static void updateResources() throws IOException, KeyManagementException,
BazaarNotifier.resources.getAsJsonObject("bazaarConversions"));
BazaarNotifier.enchantCraftingList = BazaarNotifier.resources
.getAsJsonObject("enchantCraftingList");
} catch (JsonSyntaxException e) {
} catch (JsonSyntaxException | ClassCastException e) { //ClassCastException is thrown when GitHub is down
e.printStackTrace();
} finally {
client.close();
Expand Down Expand Up @@ -213,7 +213,7 @@ public static List<String> getLoreFromItemStack(ItemStack item) {
public static int getOrderAmountLeft(List<String> lore, int totalAmount) {
int amountLeft;
if (lore.get(3).startsWith("Filled:")) {
if (lore.get(3).split(" ")[2].equals("100%")) {
if (lore.get(3).split(" ")[2].contains("100%")) {
amountLeft = 0;
} else {
String intToParse = lore.get(3).split(" ")[1].split("/")[0];
Expand Down

0 comments on commit b4b089d

Please sign in to comment.