diff --git a/build.gradle b/build.gradle index aa01f52..e3c2d07 100644 --- a/build.gradle +++ b/build.gradle @@ -21,8 +21,8 @@ compileJava { sourceCompatibility = '1.8' targetCompatibility = '1.8' } -version = "1.0.1" -group= "dev.meyi" +version = "1.1.0" +group= "dev.meyi.bn" archivesBaseName = "BazaarNotifier" minecraft { @@ -35,6 +35,12 @@ dependencies { compile group: 'org.json', name: 'json', version: '20200518' } +jar { + manifest { + attributes 'FMLAT': 'BazaarNotifier_at.cfg' + } +} + processResources { inputs.property "version", project.version @@ -46,6 +52,7 @@ processResources from(sourceSets.main.resources.srcDirs) { exclude 'mcmod.info' } + rename '(.+_at.cfg)', 'META-INF/$1' } shadowJar { diff --git a/src/main/java/dev/meyi/EventHandler.java b/src/main/java/dev/meyi/EventHandler.java deleted file mode 100644 index c77ae04..0000000 --- a/src/main/java/dev/meyi/EventHandler.java +++ /dev/null @@ -1,107 +0,0 @@ -package dev.meyi; - -import net.minecraft.client.Minecraft; -import net.minecraft.util.ChatComponentText; -import net.minecraft.util.EnumChatFormatting; -import net.minecraftforge.client.event.ClientChatReceivedEvent; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import org.json.JSONArray; -import org.json.JSONObject; - -public class EventHandler { - - @SubscribeEvent - public void bazaarChatHandler(ClientChatReceivedEvent e) { - String message = Utils.stripString(e.message.getUnformattedText()); - double price; - String type; - - if (message.startsWith("Buy Order Setup!") || message.startsWith("Sell Offer Setup!")) { - type = "buy"; - price = - Double.parseDouble(message.split(" ")[message.split(" ").length - 2].replaceAll(",", "")) - / Double - .parseDouble(message.split(" ")[3].substring(0, message.split(" ")[3].length() - 1) - .replaceAll(",", "")); - - if (message.startsWith("Sell Offer Setup!")) { - type = "sell"; - price /= .99; - } - price = Utils.round(price, 1); - - if (!BazaarNotifier.bazaarConversionsReversed - .has(message.split("x ")[1].split(" for ")[0])) { - Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( - BazaarNotifier.prefix + EnumChatFormatting.RED - + "The bazaar item you just put an order for doesn't exist. Please report this in the discord server")); - } else { - String productName = BazaarNotifier.bazaarConversionsReversed - .getString(message.split("x ")[1].split(" for ")[0]); - - String productWithAmount = message.split("! ")[1].split(" for ")[0]; - - if (!BazaarNotifier.orders.has(productName) || BazaarNotifier.orders.isNull(productName)) { - BazaarNotifier.orders.put(productName, new JSONArray()); - } - BazaarNotifier.orders.getJSONArray(productName) - .put(new JSONObject().put("product", productWithAmount).put("type", type) - .put("price", price)); - } - } else if (message.startsWith("[Bazaar] Your Buy Order for")) { - String productName = BazaarNotifier.bazaarConversionsReversed - .getString(message.split("x ")[1].split(" was ")[0]); - String productWithAmount = message.split("for ")[1].split(" was ")[0]; - - if (BazaarNotifier.orders.has(productName) && !BazaarNotifier.orders.isNull(productName)) { - int orderToRemove = 0; - boolean found = false; - double highestPrice = Double.MIN_VALUE; - for (int i = 0; i < BazaarNotifier.orders.getJSONArray(productName).length(); i++) { - JSONObject order = BazaarNotifier.orders.getJSONArray(productName).getJSONObject(i); - if (order.getString("type").equals("buy") && order.getString("product") - .equals(productWithAmount)) { - if (order.getDouble("price") > highestPrice) { - highestPrice = order.getDouble("price"); - orderToRemove = i; - found = true; - } - } - } - if (found) { - BazaarNotifier.orders.getJSONArray(productName).remove(orderToRemove); - } - if (BazaarNotifier.orders.getJSONArray(productName).length() == 0) { - BazaarNotifier.orders.remove(productName); - } - } - } else if (message.startsWith("[Bazaar] Your Sell Offer for")) { - String productName = BazaarNotifier.bazaarConversionsReversed - .getString(message.split("x ")[1].split(" was ")[0]); - String productWithAmount = message.split("for ")[1].split(" was ")[0]; - - if (BazaarNotifier.orders.has(productName) && !BazaarNotifier.orders.isNull(productName)) { - int orderToRemove = 0; - boolean found = false; - double lowestPrice = Double.MAX_VALUE; - for (int i = 0; i < BazaarNotifier.orders.getJSONArray(productName).length(); i++) { - JSONObject order = BazaarNotifier.orders.getJSONArray(productName).getJSONObject(i); - if (order.getString("type").equals("sell") && order.getString("product") - .equals(productWithAmount)) { - if (order.getDouble("price") < lowestPrice) { - lowestPrice = order.getDouble("price"); - orderToRemove = i; - found = true; - } - } - } - if (found) { - BazaarNotifier.orders.getJSONArray(productName).remove(orderToRemove); - } - if (BazaarNotifier.orders.getJSONArray(productName).length() == 0) { - BazaarNotifier.orders.remove(productName); - } - } - } - } -} diff --git a/src/main/java/dev/meyi/BazaarNotifier.java b/src/main/java/dev/meyi/bn/BazaarNotifier.java similarity index 93% rename from src/main/java/dev/meyi/BazaarNotifier.java rename to src/main/java/dev/meyi/bn/BazaarNotifier.java index 6ae8977..ef6cce9 100644 --- a/src/main/java/dev/meyi/BazaarNotifier.java +++ b/src/main/java/dev/meyi/bn/BazaarNotifier.java @@ -1,4 +1,4 @@ -package dev.meyi; +package dev.meyi.bn; import java.io.File; import java.io.IOException; @@ -22,7 +22,7 @@ public class BazaarNotifier { public static final String MODID = "BazaarNotifier"; - public static final String VERSION = "1.0.1"; + public static final String VERSION = "1.1.0"; public static String apiKey = ""; public static boolean activeBazaar = false; @@ -82,14 +82,14 @@ public void init(FMLInitializationEvent event) { .getDouble("pricePerUnit") - price > 0) { Minecraft.getMinecraft().thePlayer .addChatMessage(new ChatComponentText( - EnumChatFormatting.LIGHT_PURPLE + "Buy Order" + EnumChatFormatting.DARK_PURPLE + "Buy Order" + EnumChatFormatting.GRAY + " for " - + EnumChatFormatting.LIGHT_PURPLE + orders.getJSONArray(key) + + EnumChatFormatting.DARK_PURPLE + orders.getJSONArray(key) .getJSONObject(i).getString("product").split("x")[0] - + EnumChatFormatting.GRAY + "x " + EnumChatFormatting.LIGHT_PURPLE + + EnumChatFormatting.GRAY + "x " + EnumChatFormatting.DARK_PURPLE + bazaarConversions.get(key) + EnumChatFormatting.YELLOW + " OUTDATED " + EnumChatFormatting.GRAY + "(" - + EnumChatFormatting.LIGHT_PURPLE + price + + EnumChatFormatting.DARK_PURPLE + price + EnumChatFormatting.GRAY + ")" )); orders.getJSONArray(key).remove(i); diff --git a/src/main/java/dev/meyi/BazaarNotifierCommand.java b/src/main/java/dev/meyi/bn/BazaarNotifierCommand.java similarity index 99% rename from src/main/java/dev/meyi/BazaarNotifierCommand.java rename to src/main/java/dev/meyi/bn/BazaarNotifierCommand.java index fbbbde8..018cc92 100644 --- a/src/main/java/dev/meyi/BazaarNotifierCommand.java +++ b/src/main/java/dev/meyi/bn/BazaarNotifierCommand.java @@ -1,4 +1,4 @@ -package dev.meyi; +package dev.meyi.bn; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/dev/meyi/bn/EventHandler.java b/src/main/java/dev/meyi/bn/EventHandler.java new file mode 100644 index 0000000..c6faecb --- /dev/null +++ b/src/main/java/dev/meyi/bn/EventHandler.java @@ -0,0 +1,140 @@ +package dev.meyi.bn; + +import java.util.Timer; +import java.util.TimerTask; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.inventory.GuiChest; +import net.minecraft.inventory.IInventory; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.StringUtils; +import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.client.event.GuiOpenEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import org.json.JSONArray; +import org.json.JSONObject; + +public class EventHandler { + + JSONObject verify = null; + String[] productVerify = new String[2]; + + @SubscribeEvent + public void bazaarChatHandler(ClientChatReceivedEvent e) { + String message = Utils.stripString(e.message.getUnformattedText()); + if (message.startsWith("Buy Order Setup!") || message.startsWith("Sell Offer Setup!")) { + if (productVerify[0] != null && productVerify[1] != null && productVerify[0] + .equals(BazaarNotifier.bazaarConversionsReversed + .getString(message.split("x ")[1].split(" for ")[0])) && productVerify[1] + .equals(message.split("! ")[1].split(" for ")[0])) { + if (!BazaarNotifier.orders.has(productVerify[0]) || BazaarNotifier.orders + .isNull(productVerify[0])) { + BazaarNotifier.orders.put(productVerify[0], new JSONArray()); + } + BazaarNotifier.orders.getJSONArray(productVerify[0]).put(verify); + + verify = null; + productVerify = new String[2]; + } + } else if (message.startsWith("[Bazaar] Your Buy Order for")) { + String productName = BazaarNotifier.bazaarConversionsReversed + .getString(message.split("x ")[1].split(" was ")[0]); + String productWithAmount = message.split("for ")[1].split(" was ")[0]; + + if (BazaarNotifier.orders.has(productName) && !BazaarNotifier.orders.isNull(productName)) { + int orderToRemove = 0; + boolean found = false; + double highestPrice = Double.MIN_VALUE; + for (int i = 0; i < BazaarNotifier.orders.getJSONArray(productName).length(); i++) { + JSONObject order = BazaarNotifier.orders.getJSONArray(productName).getJSONObject(i); + if (order.getString("type").equals("buy") && order.getString("product") + .equals(productWithAmount)) { + if (order.getDouble("price") > highestPrice) { + highestPrice = order.getDouble("price"); + orderToRemove = i; + found = true; + } + } + } + if (found) { + BazaarNotifier.orders.getJSONArray(productName).remove(orderToRemove); + } + if (BazaarNotifier.orders.getJSONArray(productName).length() == 0) { + BazaarNotifier.orders.remove(productName); + } + } + } else if (message.startsWith("[Bazaar] Your Sell Offer for")) { + String productName = BazaarNotifier.bazaarConversionsReversed + .getString(message.split("x ")[1].split(" was ")[0]); + String productWithAmount = message.split("for ")[1].split(" was ")[0]; + + if (BazaarNotifier.orders.has(productName) && !BazaarNotifier.orders.isNull(productName)) { + int orderToRemove = 0; + boolean found = false; + double lowestPrice = Double.MAX_VALUE; + for (int i = 0; i < BazaarNotifier.orders.getJSONArray(productName).length(); i++) { + JSONObject order = BazaarNotifier.orders.getJSONArray(productName).getJSONObject(i); + if (order.getString("type").equals("sell") && order.getString("product") + .equals(productWithAmount)) { + if (order.getDouble("price") < lowestPrice) { + lowestPrice = order.getDouble("price"); + orderToRemove = i; + found = true; + } + } + } + if (found) { + BazaarNotifier.orders.getJSONArray(productName).remove(orderToRemove); + } + if (BazaarNotifier.orders.getJSONArray(productName).length() == 0) { + BazaarNotifier.orders.remove(productName); + } + } + } + } + + + @SubscribeEvent + public void menuOpenedEvent(GuiOpenEvent e) { + if (e.gui instanceof GuiChest) { + IInventory chestInventory = ((GuiChest) e.gui).lowerChestInventory; + if (chestInventory.hasCustomName()) { + if (Utils.stripString(chestInventory.getDisplayName().getUnformattedText()) + .contains("Confirm Buy Order") || Utils + .stripString(chestInventory.getDisplayName().getUnformattedText()) + .contains("Confirm Sell Offer")) { + final String type = Utils + .stripString(chestInventory.getDisplayName().getUnformattedText()) + .contains("Confirm Sell Offer") ? "sell" : "buy"; + new Timer().schedule(new TimerTask() { + @Override + public void run() { + double price = Double.parseDouble(StringUtils.stripControlCodes( + chestInventory.getStackInSlot(13).getTagCompound().getCompoundTag("display") + .getTagList("Lore", 8).getStringTagAt(2)).split(" ")[3].replaceAll(",", "")); + String product = StringUtils.stripControlCodes( + chestInventory.getStackInSlot(13).getTagCompound().getCompoundTag("display") + .getTagList("Lore", 8).getStringTagAt(4)).split("x ")[1]; + if (!BazaarNotifier.bazaarConversionsReversed + .has(product)) { + Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( + BazaarNotifier.prefix + EnumChatFormatting.RED + + "The bazaar item you just put an order for doesn't exist. Please report this in the discord server")); + } else { + String productName = BazaarNotifier.bazaarConversionsReversed + .getString(product); + String productWithAmount = StringUtils.stripControlCodes( + chestInventory.getStackInSlot(13).getTagCompound().getCompoundTag("display") + .getTagList("Lore", 8).getStringTagAt(4)).split(": ")[1]; + productVerify[0] = productName; + productVerify[1] = productWithAmount; + verify = new JSONObject().put("product", productWithAmount).put("type", type) + .put("price", price); + } + } + }, 50); + } + } + } + } +} diff --git a/src/main/java/dev/meyi/Utils.java b/src/main/java/dev/meyi/bn/Utils.java similarity index 99% rename from src/main/java/dev/meyi/Utils.java rename to src/main/java/dev/meyi/bn/Utils.java index 2c0d0b4..0860107 100644 --- a/src/main/java/dev/meyi/Utils.java +++ b/src/main/java/dev/meyi/bn/Utils.java @@ -1,4 +1,4 @@ -package dev.meyi; +package dev.meyi.bn; import java.io.BufferedReader; import java.io.File; diff --git a/src/main/resources/BazaarNotifier_at.cfg b/src/main/resources/BazaarNotifier_at.cfg new file mode 100644 index 0000000..9ae7aa7 --- /dev/null +++ b/src/main/resources/BazaarNotifier_at.cfg @@ -0,0 +1 @@ +public net.minecraft.client.gui.inventory.GuiChest field_147015_w # lowerChestInventory \ No newline at end of file