From 4dde80e0fdfcafe9250317ceefde37f65b2055ef Mon Sep 17 00:00:00 2001 From: "Noah Clarkson (Unprotesting)" Date: Fri, 22 Jan 2021 15:27:06 +0000 Subject: [PATCH] Added transaction GUI and improved a number of functions + Improved sell command stability + Greatly improved /transaction command, new GUI, improved stability and ease of use. + Cleaned up some code and error statements + Improved stability of the enchantment algorithm + Improved HTTP request stability + Fixed illegal enchants + Updated to 0.13.0-pre-release-3 --- Auto-Tune/dependency-reduced-pom.xml | 2 +- Auto-Tune/pom.xml | 2 +- Auto-Tune/src/resources/config.yml | 6 +- .../github/Commands/AutoTuneBuyCommand.java | 5 + .../Commands/AutoTuneGUIShopUserCommand.java | 15 +- .../github/Commands/AutoTuneSellCommand.java | 10 +- .../Commands/AutoTuneTransactionCommand.java | 218 ++++++++++++++---- .../src/unprotesting/com/github/Main.java | 10 +- .../com/github/util/EnchantmentAlgorithm.java | 50 ++++ .../com/github/util/HttpPostRequestor.java | 21 +- .../com/github/util/ItemPriceData.java | 13 +- .../github/util/PriceCalculationHandler.java | 7 +- .../com/github/util/TextHandler.java | 2 - .../com/github/util/Transaction.java | 11 +- .../github/util/TransactionSerializer.java | 2 +- 15 files changed, 278 insertions(+), 96 deletions(-) diff --git a/Auto-Tune/dependency-reduced-pom.xml b/Auto-Tune/dependency-reduced-pom.xml index 7d06ab9..248da3f 100644 --- a/Auto-Tune/dependency-reduced-pom.xml +++ b/Auto-Tune/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ unprotesting.com.github Auto-Tune Auto-Tune - 0.13.0-pre-release-2 + 0.13.0-pre-release-3 The automatic pricing plugin for minecraft https://github.com/Unprotesting/Auto-Tune diff --git a/Auto-Tune/pom.xml b/Auto-Tune/pom.xml index e7bf6dd..9616926 100644 --- a/Auto-Tune/pom.xml +++ b/Auto-Tune/pom.xml @@ -9,7 +9,7 @@ unprotesting.com.github Auto-Tune - 0.13.0-pre-release-2 + 0.13.0-pre-release-3 Auto-Tune https://github.com/Unprotesting/Auto-Tune diff --git a/Auto-Tune/src/resources/config.yml b/Auto-Tune/src/resources/config.yml index 755b317..86e157c 100644 --- a/Auto-Tune/src/resources/config.yml +++ b/Auto-Tune/src/resources/config.yml @@ -24,8 +24,8 @@ web-server-enabled: true port: 8123 ## The maximum length in data points that the trade-short.html will show (this doesn't affect data) -## Info: When the time-period is set to 5, 288 is one day. -maximum-short-trade-length: 288 +## Info: When the time-period is set to 144, 1 is one day. +maximum-short-trade-length: 144 ## Server name that will show up in commands and requests server-name: 'My Server' @@ -33,7 +33,7 @@ server-name: 'My Server' ## Time Period in minutes ## Info: This should be around a tenth of the total items in your shop (i.e with 150 items this would be 15) to prevent overload ## Info: When decreasing or increasing this adjust your volatility settings accordingly -time-period: 5 +time-period: 10 ## The amount of menu rows in the GUI shop, value of 4-6. menu-rows: 6 diff --git a/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneBuyCommand.java b/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneBuyCommand.java index 9df9d8d..d984b4a 100644 --- a/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneBuyCommand.java +++ b/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneBuyCommand.java @@ -15,6 +15,7 @@ import unprotesting.com.github.Main; import unprotesting.com.github.util.Config; +import unprotesting.com.github.util.EnchantmentAlgorithm; import unprotesting.com.github.util.EnchantmentSetting; import unprotesting.com.github.util.TextHandler; import unprotesting.com.github.util.Transaction; @@ -72,6 +73,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St boolean enchantExists = false; Map map = is.getEnchantments(); Enchantment ench = Enchantment.getByName(setting.name); + if (EnchantmentAlgorithm.checkForEnchantConflicts(is, ench)){ + player.sendMessage(ChatColor.RED + "Cannot enchant item: " + is.getType().toString() + " with enchantment " + setting.name); + return true; + } if (map.get(ench) != null){ enchantExists = true; } diff --git a/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneGUIShopUserCommand.java b/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneGUIShopUserCommand.java index 5feca8f..40030d9 100644 --- a/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneGUIShopUserCommand.java +++ b/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneGUIShopUserCommand.java @@ -102,7 +102,14 @@ public static void loadGUISECTIONS(Player player, boolean autosell) { Gui front = new Gui((lines + 2), Config.getMenuTitle()); OutlinePane pane = new OutlinePane(1, 1, 7, lines); for (int i = 0; i < Main.sectionedItems.length; i++) { - ItemStack is = new ItemStack((Main.sectionedItems[i].image)); + ItemStack is; + try{ + is = new ItemStack((Main.sectionedItems[i].image)); + } + catch(IllegalArgumentException ex){ + Main.log("Section: " + Main.sectionedItems[i].name + " has a null or invalid material-name"); + continue; + } ItemMeta im = is.getItemMeta(); im.setDisplayName(ChatColor.GOLD + Main.sectionedItems[i].name); if (!autosell){ @@ -632,7 +639,7 @@ public static StaticPane loadMainMenuBackPane(PaginatedPane pPane, boolean autos } public static void sendPlayerShopMessageAndUpdateGDP(int amount, Player player, String matClickedString, boolean sell){ - ItemStack itemstack = new ItemStack(Material.getMaterial(matClickedString)); + ItemStack itemstack = new ItemStack(Material.getMaterial(matClickedString), amount); if (!sell){ ConcurrentHashMap cMap = Main.maxBuyMap.get(player.getUniqueId()); cMap.put(matClickedString, (cMap.get(matClickedString)+amount)); @@ -641,7 +648,7 @@ public static void sendPlayerShopMessageAndUpdateGDP(int amount, Player player, Double[] arr = inputMap.get(inputMap.size()-1); Double[] outputArr = {arr[0], (arr[1]+amount), arr[2]}; Main.tempdatadata.put("GDP", (Main.tempdatadata.get("GDP")+(arr[0]*amount))); - Transaction transaction = new Transaction(player, itemstack, "Buy", arr[0]); + Transaction transaction = new Transaction(player.getName(), itemstack, "Buy", arr[0]); inputMap.put((inputMap.size()-1), outputArr); Main.map.put(matClickedString, inputMap); Main.getEconomy().withdrawPlayer(player, (arr[0]*amount)); @@ -658,7 +665,7 @@ else if (sell){ Double[] outputArr = {arr[0], arr[1], (arr[2]+amount)}; Double price = AutoTuneGUIShopUserCommand.getItemPrice(matClickedString, true); Main.tempdatadata.put("GDP", (Main.tempdatadata.get("GDP")+(price*amount))); - Transaction transaction = new Transaction(player, itemstack, "Sell", price); + Transaction transaction = new Transaction(player.getName(), itemstack, "Sell", price); inputMap.put((inputMap.size()-1), outputArr); Main.map.put(matClickedString, inputMap); Main.getEconomy().depositPlayer(player, (price*amount)); diff --git a/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneSellCommand.java b/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneSellCommand.java index b0a238d..f8d454d 100644 --- a/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneSellCommand.java +++ b/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneSellCommand.java @@ -1,5 +1,6 @@ package unprotesting.com.github.Commands; +import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -102,7 +103,7 @@ public static void sellItems(Player player, ItemStack[] items, Boolean autoSell) String item_name = item.getType().toString(); increaseMaxSells(player, quantity, item_name); increaseSells(item_name, quantity); - Transaction transaction = new Transaction(player, item, "Sell", totalPrice); + Transaction transaction = new Transaction(player.getName(), item, "Sell", totalPrice); transaction.loadIntoMap(); loadEnchantmentTransactions(item, player); increaseMaxSells(player, quantity, itemString); @@ -133,9 +134,12 @@ public static void loadEnchantmentTransactions (ItemStack item, Player player){ return; } else { - for (Enchantment ench : item.getEnchantments().keySet()){ - Transaction transaction = new Transaction(player, ench, "Sell"); + Map ench = item.getEnchantments(); + for (Map.Entry enchants : ench.entrySet()){ + Enchantment enchant = enchants.getKey(); + Transaction transaction = new Transaction(player, enchant, "Sell"); transaction.loadIntoMap(); + EnchantmentAlgorithm.updateEnchantSellData(enchant, enchants.getValue()); } } } diff --git a/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneTransactionCommand.java b/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneTransactionCommand.java index 20999b9..170536d 100644 --- a/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneTransactionCommand.java +++ b/Auto-Tune/src/unprotesting/com/github/Commands/AutoTuneTransactionCommand.java @@ -1,18 +1,29 @@ package unprotesting.com.github.Commands; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; -import com.earth2me.essentials.User; +import com.github.stefvanschie.inventoryframework.Gui; +import com.github.stefvanschie.inventoryframework.GuiItem; +import com.github.stefvanschie.inventoryframework.pane.OutlinePane; +import com.github.stefvanschie.inventoryframework.pane.StaticPane; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; +import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import net.md_5.bungee.api.ChatColor; import unprotesting.com.github.Main; +import unprotesting.com.github.util.Config; +import unprotesting.com.github.util.EnchantmentAlgorithm; import unprotesting.com.github.util.TextHandler; import unprotesting.com.github.util.Transaction; @@ -23,7 +34,7 @@ public boolean onCommand(CommandSender sender, Command command, String transacti if (command.getName().equalsIgnoreCase("transactions")) { if (sender instanceof Player) { Player player = (Player) sender; - if (args.length < 1) { + if (args.length == 0) { if (player.hasPermission("at.transactions") || player.isOp() || player.hasPermission("at.transactions.other")){ player.sendMessage(ChatColor.RED + "Error! Correct Usage: /transactions "); } @@ -32,56 +43,61 @@ public boolean onCommand(CommandSender sender, Command command, String transacti } return true; } - else if (args.length < 2) { + if (args.length == 1) { if (player.hasPermission("at.transactions") || player.isOp()){ Integer page = parsePage(args[0], player); - if (page != null && page > 0) { - setupTransactionViewForPlayer(player, page, player); + if (page == null || page < 0) { + player.sendMessage(ChatColor.RED + "Not enough data available / incorrect formatting"); + return true; } + player.sendMessage(ChatColor.GOLD + "Setting up transaction view for " + player.getName()); + loadTransactionGUI(player, setupTransactionViewForPlayer(page, player.getName()), page+1, player.getName()); } else{ TextHandler.noPermssion(player); } return true; - } else if (args[0].equals("all")) { + } + if (args[0].equals("all") && args.length == 2) { if (player.hasPermission("at.transactions.other") || player.isOp()){ - if (args.length < 2){ - player.sendMessage(ChatColor.RED + "Error! Correct Usage: /transactions "); - return true; - } Integer page = parsePage(args[1], player); if (page == null || page < 0) { + player.sendMessage(ChatColor.RED + "Not enough data available / incorrect formatting"); return true; } - setupTransactionViewAll(player, page); + player.sendMessage(ChatColor.GOLD + "Setting up transaction view for all players"); + loadTransactionGUI(player, setupTransactionViewAll(page), page+1, null); } else{ TextHandler.noPermssion(player); } return true; - } else if (args.length == 2) { + } + if (args.length == 2) { if (player.hasPermission("at.transactions.other") || player.isOp()){ Integer page = parsePage(args[1], player); if (page == null || page < 0) { + player.sendMessage(ChatColor.RED + "Not enough data available / incorrect formatting"); return true; } - Player select_player = parsePlayer(args[0], player); + String select_player = args[0]; if (select_player == null){ + player.sendMessage(ChatColor.RED + "Not enough data available / incorrect formatting"); return true; } - setupTransactionViewForPlayer(player, page, select_player); + player.sendMessage(ChatColor.GOLD + "Setting up transaction view for " + args[0]); + loadTransactionGUI(player, setupTransactionViewForPlayer(page, select_player), page+1, select_player) ; } else{ TextHandler.noPermssion(player); } return true; - } else { - if (player.hasPermission("at.transactions") || player.hasPermission("at.transactions.other") || player.isOp()){ - player.sendMessage("Error! Correct Usage: /transactions "); - } - else{ - TextHandler.noPermssion(player); - } + } + if (player.hasPermission("at.transactions") || player.hasPermission("at.transactions.other") || player.isOp()){ + player.sendMessage("Error! Correct Usage: /transactions "); + } + else{ + TextHandler.noPermssion(player); } } } @@ -89,37 +105,43 @@ else if (args.length < 2) { } @Deprecated - public void setupTransactionViewForPlayer(Player player, int page, Player input_player) { - String input_player_name = input_player.getName(); - player.sendMessage(ChatColor.YELLOW + "Page: " + (page+1)); + public Transaction[] setupTransactionViewForPlayer(int page, String input_player) { + Transaction[] output = new Transaction [28]; int size = (Main.getTransactions().size() - 1); - for (Integer i = (size - (page * 10)); i > size - ((page + 1) * 10); i--) { - try{ - Transaction transaction = Main.getTransactions().get(i); - if (transaction.player.equals(input_player_name)) { - player.sendMessage(ChatColor.RED + Integer.toString(i+1) + ": " + ChatColor.YELLOW + transaction.toDisplayString()); - } - else{ - page = page + (1/10); - } + int k = 0; + int i = size - (page*28); + for (int l = 0; l < 50000; l++){ + Transaction transaction = Main.getTransactions().get(i-l); + if (k > 27){ + break; } - catch(NullPointerException ex){ + if (transaction != null){ + if (transaction.player.equals(input_player)){ + output[k] = transaction; + k++; + } } } + return output; } @Deprecated - public void setupTransactionViewAll(Player player, int page) { - player.sendMessage(ChatColor.YELLOW + "Page: " + (page+1)); + public Transaction[] setupTransactionViewAll(int page) { + Transaction[] output = new Transaction [28]; int size = (Main.getTransactions().size() - 1); - for (Integer i = (size - (page * 10)); i > size - ((page + 1) * 10); i--) { - try{ - player.sendMessage(ChatColor.RED + Integer.toString(i+1) + ": " + ChatColor.YELLOW - + Main.getTransactions().get(i).toDisplayString()); + int k = 0; + int i = size - (page*28); + for (int l = 0; l < 1000; l++){ + if (k > 27){ + break; } - catch(NullPointerException ex){ + Transaction transaction = Main.getTransactions().get(i-l); + if (transaction != null){ + output[k] = transaction; + k++; } } + return output; } public Integer parsePage(String arg, Player player) { @@ -139,14 +161,112 @@ public Integer parsePage(String arg, Player player) { } @Deprecated - public Player parsePlayer(String arg, Player player) { - OfflinePlayer offlinePlayer = Bukkit.getServer().getOfflinePlayer(arg); - if (offlinePlayer == null){ - player.sendMessage("Error! Correct Usage: /transactions "); - return null; + public void loadTransactionGUI (Player player, Transaction[] transactions, int page, String queried_player) { + if (transactions.length > 28){ + player.sendMessage(ChatColor.RED + "Error when parsing transactions to GUI!"); + return; } - Player output = offlinePlayer.getPlayer(); + Gui GUI = new Gui(6, "Transactions"); + OutlinePane pane = new OutlinePane(1, 1, 7, 4); + for (Transaction transaction : transactions){ + if (transaction == null){ + continue; + } + String item_name = transaction.item; + if (item_name == null){ + continue; + } + int amount = transaction.amount; + ItemStack item_stack; + try{ + item_stack = new ItemStack(Material.matchMaterial(item_name), amount); + } + catch (IllegalArgumentException ex){ + item_stack = new ItemStack(Material.ENCHANTED_BOOK); + item_stack = EnchantmentAlgorithm.addBookEnchantment(item_stack, Enchantment.getByName(item_name), 1); + } + ItemMeta item_meta = item_stack.getItemMeta(); + item_meta.setDisplayName(ChatColor.GOLD + item_name); + item_meta.setLore(generateLoreFromTransaction(transaction)); + item_stack.setItemMeta(item_meta); + GuiItem gItem = new GuiItem (item_stack, event -> { + event.setCancelled(true); + }); + pane.addItem(gItem); + } + GUI.addPane(pane); + GUI.addPane(createNextPagePane(page, player, queried_player)); + if (page > 1){ + GUI.addPane(createBackPagePane(page, player, queried_player)); + } + CommandSender cSender = player; + GUI.update(); + GUI.show((HumanEntity) cSender); + + } + + @Deprecated + public List generateLoreFromTransaction (Transaction transaction) { + List output = new ArrayList(); + if (transaction.type.equals("Buy")){ + output.add(ChatColor.GREEN + "" + ChatColor.MAGIC + "⇵ " + ChatColor.GREEN + "Buy" + ChatColor.MAGIC + " ⇵"); + } + else { + output.add(ChatColor.RED + "" + ChatColor.MAGIC + "⇵ " + ChatColor.RED + "Sell" + ChatColor.MAGIC + " ⇵"); + } + output.add(ChatColor.YELLOW + "Player: " + ChatColor.GREEN + transaction.player); + output.add(ChatColor.YELLOW + "Price: " + Config.getCurrencySymbol() + ChatColor.GREEN + AutoTuneGUIShopUserCommand.df2.format(transaction.total_price)); + output.add(ChatColor.YELLOW + "Date: " + ChatColor.GREEN + transaction.date.toLocaleString()); return output; } + + public StaticPane createNextPagePane (int page, Player querying_player, String queried_player) { + StaticPane output = new StaticPane(8, 5, 1, 1); + ItemStack item_stack = new ItemStack(Material.ARROW); + ItemMeta item_meta = item_stack.getItemMeta(); + item_meta.setDisplayName(ChatColor.DARK_PURPLE + "Next"); + item_meta.setLore(Arrays.asList(ChatColor.GRAY + "Page " + ChatColor.WHITE + (page + 1))); + item_stack.setItemMeta(item_meta); + GuiItem gItem = new GuiItem(item_stack, event ->{ + event.setCancelled(true); + if (event.getClick() == ClickType.LEFT){ + querying_player.getOpenInventory().close(); + if (queried_player != null){ + querying_player.sendMessage(ChatColor.GOLD + "Opening page " + (page+1)); + loadTransactionGUI(querying_player, setupTransactionViewForPlayer(page + 1, queried_player), page + 1, queried_player); + } + else{ + loadTransactionGUI(querying_player, setupTransactionViewAll(page + 1), page + 1, null); + } + } + }); + output.addItem(gItem, 0, 0); + return output; + } + + public StaticPane createBackPagePane (int page, Player querying_player, String queried_player) { + StaticPane output = new StaticPane(0, 5, 1, 1); + ItemStack item_stack = new ItemStack(Material.ARROW); + ItemMeta item_meta = item_stack.getItemMeta(); + item_meta.setDisplayName(ChatColor.DARK_PURPLE + "Back"); + item_meta.setLore(Arrays.asList(ChatColor.GRAY + "Page " + ChatColor.WHITE + (page - 1))); + item_stack.setItemMeta(item_meta); + GuiItem gItem = new GuiItem(item_stack, event ->{ + event.setCancelled(true); + if (event.getClick() == ClickType.LEFT){ + querying_player.getOpenInventory().close(); + if (queried_player != null){ + querying_player.sendMessage(ChatColor.GOLD + "Opening page " + (page-1)); + loadTransactionGUI(querying_player, setupTransactionViewForPlayer(page - 1, queried_player), page - 1, queried_player); + } + else{ + loadTransactionGUI(querying_player, setupTransactionViewAll(page - 1), page - 1, null); + } + } + }); + output.addItem(gItem, 0, 0); + return output; + } + } diff --git a/Auto-Tune/src/unprotesting/com/github/Main.java b/Auto-Tune/src/unprotesting/com/github/Main.java index a6d3de5..8dd42b0 100644 --- a/Auto-Tune/src/unprotesting/com/github/Main.java +++ b/Auto-Tune/src/unprotesting/com/github/Main.java @@ -10,12 +10,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; import java.util.Collections; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -30,7 +25,6 @@ import com.sun.net.httpserver.HttpServer; import org.bstats.bukkit.Metrics; - import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -46,7 +40,6 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitScheduler; -import org.json.simple.parser.ParseException; import org.mapdb.DB; import org.mapdb.DBMaker; import org.mapdb.HTreeMap; @@ -71,7 +64,6 @@ import unprotesting.com.github.Commands.AutoTuneTransactionCommand; import unprotesting.com.github.util.AutoSellEventHandler; import unprotesting.com.github.util.AutoTunePlayerAutoSellEventHandler; -import unprotesting.com.github.util.CSVHandler; import unprotesting.com.github.util.ChatHandler; import unprotesting.com.github.util.Config; import unprotesting.com.github.util.EconomyShopConfigManager; @@ -85,7 +77,6 @@ import unprotesting.com.github.util.JoinEventHandler; import unprotesting.com.github.util.Loan; import unprotesting.com.github.util.LoanEventHandler; -import unprotesting.com.github.util.MathHandler; import unprotesting.com.github.util.PriceCalculationHandler; import unprotesting.com.github.util.Section; import unprotesting.com.github.util.StaticFileHandler; @@ -199,6 +190,7 @@ public void onDisable() { try { closeDataFiles(); } catch (ClassNotFoundException e) { + Main.log("Error: Could not close data files correctly."); } scheduler.cancelTasks(getINSTANCE()); log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion())); diff --git a/Auto-Tune/src/unprotesting/com/github/util/EnchantmentAlgorithm.java b/Auto-Tune/src/unprotesting/com/github/util/EnchantmentAlgorithm.java index a06124b..6a24e37 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/EnchantmentAlgorithm.java +++ b/Auto-Tune/src/unprotesting/com/github/util/EnchantmentAlgorithm.java @@ -7,9 +7,11 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; import unprotesting.com.github.Main; import unprotesting.com.github.Commands.AutoTuneGUIShopUserCommand; +import unprotesting.com.github.util.DurabilityAlgorithm; public class EnchantmentAlgorithm { @@ -118,4 +120,52 @@ public static void updateEnchantSellData(ItemStack is, Player player){ transaction.loadIntoMap(); } } + + @Deprecated + public static void updateEnchantSellData (Enchantment enchant, int level) { + String enchName = enchant.getName(); + EnchantmentSetting setting = Main.enchMap.get(enchName); + ConcurrentHashMap map; + try{ + map = setting.buySellData; + } + catch(NullPointerException e){ + map = new ConcurrentHashMap(); + map.put(0, new Double[]{0.0, 0.0, 0.0}); + } + Integer size = map.size()-1; + Double[] arr = map.get(size); + if (arr == null){ + arr = new Double[]{setting.price, 0.0, 0.0}; + } + if (arr[2] == null){ + arr[2] = 0.0; + } + if (arr[1] == null){ + arr[1] = 0.0; + } + arr[2] += level; + map.put(size, arr); + setting.buySellData = map; + Main.enchMap.put(enchName, setting); + } + + public static ItemStack addBookEnchantment(ItemStack item, Enchantment enchantment, int level) { + EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta(); + if (meta.hasConflictingEnchant(enchantment) || meta.hasConflictingStoredEnchant(enchantment)){ + return null; + } + meta.addStoredEnchant(enchantment, level, true); + item.setItemMeta(meta); + return item; + } + + public static boolean checkForEnchantConflicts (ItemStack is, Enchantment ench) { + for (Enchantment item_ench : is.getEnchantments().keySet()) { + if (ench.conflictsWith(item_ench)){ + return true; + } + } + return false; + } } diff --git a/Auto-Tune/src/unprotesting/com/github/util/HttpPostRequestor.java b/Auto-Tune/src/unprotesting/com/github/util/HttpPostRequestor.java index b336226..9aed4af 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/HttpPostRequestor.java +++ b/Auto-Tune/src/unprotesting/com/github/util/HttpPostRequestor.java @@ -24,10 +24,9 @@ public class HttpPostRequestor { - public static CloseableHttpClient client = HttpClients.createDefault(); - public static void updatePricesforItems(JSONObject json) throws ClientProtocolException, IOException { - HttpEntity entityResponse = sendPostRequest(json); + CloseableHttpClient client = HttpClients.createDefault(); + HttpEntity entityResponse = sendPostRequest(json, client); if (entityResponse != null) { JsonParser parser = new JsonParser(); String result = null; @@ -37,22 +36,26 @@ public static void updatePricesforItems(JSONObject json) throws ClientProtocolEx Main.debugLog("Socket Exception: Socket Closed. Reloading HttpClient.."); client.close(); client = HttpClients.createDefault(); - entityResponse = sendPostRequest(json); + entityResponse = sendPostRequest(json, client); try { result = EntityUtils.toString(entityResponse); } - catch (SocketException ex2) { + catch (ConnectionClosedException | SocketException ex2) { + Main.log("Connection was closed! Please ensure your firewall isn't blocking our connections!"); + return; } } catch (ConnectionClosedException ex){ Main.debugLog("Connection Closed Exception: Socket Closed. Reloading HttpClient.."); client.close(); client = HttpClients.createDefault(); - entityResponse = sendPostRequest(json); + entityResponse = sendPostRequest(json, client); try { result = EntityUtils.toString(entityResponse); } - catch (SocketException ex2) { + catch (ConnectionClosedException | SocketException ex2) { + Main.log("Connection was closed! Please ensure your firewall isn't blocking our connections!"); + return; } } if (result == null) { @@ -77,7 +80,7 @@ public static void updatePricesforItems(JSONObject json) throws ClientProtocolEx } } - public static HttpEntity sendPostRequest(JSONObject json) throws IOException { + public static HttpEntity sendPostRequest(JSONObject json, CloseableHttpClient client) throws IOException { HttpPost httpPost = new HttpPost("https://economy-api.herokuapp.com/"); StringEntity entity = new StringEntity(json.toJSONString()); httpPost.setEntity(entity); @@ -107,7 +110,7 @@ public static HttpEntity sendPostRequest(JSONObject json) throws IOException { } public static void updatePricesforEnchantments(JSONObject json) throws ClientProtocolException, IOException { - HttpEntity entityResponse = sendPostRequest(json); + HttpEntity entityResponse = sendPostRequest(json, HttpClients.createDefault()); if (entityResponse != null) { JsonParser parser = new JsonParser(); String result = EntityUtils.toString(entityResponse); diff --git a/Auto-Tune/src/unprotesting/com/github/util/ItemPriceData.java b/Auto-Tune/src/unprotesting/com/github/util/ItemPriceData.java index abf5c9d..c35ceaa 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/ItemPriceData.java +++ b/Auto-Tune/src/unprotesting/com/github/util/ItemPriceData.java @@ -19,11 +19,16 @@ public class ItemPriceData { public ItemPriceData(String name){ this.name = name; ConcurrentHashMap newMap = Main.map.get(name); - int size = (newMap.size())-1; + int size = newMap.size()-1; for (int i = 0; i < size; i++){ - this.prices.add(newMap.get(i)[0]); + try{ + this.price = newMap.get(i)[0]; + this.prices.add(price); + } + catch (NullPointerException ex){ + break; + } } - price = newMap.get(size)[0]; ConfigurationSection config = Main.getShopConfig().getConfigurationSection("shops." + name); Double sellDifference = Config.getSellPriceDifference(); try{ @@ -32,7 +37,7 @@ public ItemPriceData(String name){ catch(NullPointerException ex){ sellDifference = Config.getSellPriceDifference(); } - sellPrice = price - (price*0.01*sellDifference); + this.sellPrice = this.price - (this.price*0.01*sellDifference); } } diff --git a/Auto-Tune/src/unprotesting/com/github/util/PriceCalculationHandler.java b/Auto-Tune/src/unprotesting/com/github/util/PriceCalculationHandler.java index 29a52dc..28a4ae5 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/PriceCalculationHandler.java +++ b/Auto-Tune/src/unprotesting/com/github/util/PriceCalculationHandler.java @@ -17,6 +17,7 @@ import org.json.simple.parser.ParseException; import unprotesting.com.github.Main; +import unprotesting.com.github.Commands.AutoTuneGUIShopUserCommand; public class PriceCalculationHandler implements Runnable { @@ -47,7 +48,7 @@ public static void loadItemPricesAndCalculate() throws ParseException, ClientPro JSONArray itemData = new JSONArray(); for (String str : Main.map.keySet()) { ConcurrentHashMap buySellMap = Main.map.get(str); - Double price = buySellMap.get(buySellMap.size()-1)[0]; + Double price = AutoTuneGUIShopUserCommand.getItemPrice(str, false); Double[] arr = loadAverageBuyAndSellValue(buySellMap, price, str); JSONObject priceData = new JSONObject(); priceData.put("itemName", str); @@ -118,8 +119,8 @@ public static void loadEnchantmentPricesAndCalculate() throws ParseException, Cl } obj.put("itemData", itemData); - obj.put("maxVolatility", Config.getBasicMaxVariableVolatility()*6); - obj.put("minVolatility", Config.getBasicMinVariableVolatility()*6); + obj.put("maxVolatility", Config.getBasicMaxVariableVolatility()*5.5); + obj.put("minVolatility", Config.getBasicMinVariableVolatility()*5.5); HttpPostRequestor.updatePricesforEnchantments(obj); Date date = Calendar.getInstance().getTime(); Date newDate = MathHandler.addMinutesToJavaUtilDate(date, Config.getTimePeriod()*2); diff --git a/Auto-Tune/src/unprotesting/com/github/util/TextHandler.java b/Auto-Tune/src/unprotesting/com/github/util/TextHandler.java index 5b95110..fb7e23f 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/TextHandler.java +++ b/Auto-Tune/src/unprotesting/com/github/util/TextHandler.java @@ -2,8 +2,6 @@ import org.bukkit.entity.Player; -import unprotesting.com.github.Main; - public class TextHandler { public static void noPermssion(Player p){ diff --git a/Auto-Tune/src/unprotesting/com/github/util/Transaction.java b/Auto-Tune/src/unprotesting/com/github/util/Transaction.java index 64c1973..790f679 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/Transaction.java +++ b/Auto-Tune/src/unprotesting/com/github/util/Transaction.java @@ -2,11 +2,8 @@ import java.io.Serializable; import java.time.Instant; -import java.util.ArrayList; import java.util.Date; -import javax.lang.model.element.TypeElement; - import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.enchantments.Enchantment; @@ -26,9 +23,9 @@ public class Transaction implements Serializable{ public String type; public double total_price; - public Transaction(Player player, ItemStack is, String type, double total_price){ + public Transaction(String player, ItemStack is, String type, double total_price){ this.date = Date.from(Instant.now()); - this.player = player.getName(); + this.player = player; this.item = is.getType().toString(); this.amount = is.getAmount(); this.total_price = total_price; @@ -55,9 +52,9 @@ public Transaction(Player player, Enchantment enchantment, String type){ this.type = type; } - public Transaction(Date date, Player player, String item, int amount, String type, double total_price){ + public Transaction(Date date, String player, String item, int amount, String type, double total_price){ this.date = date; - this.player = player.getName(); + this.player = player; this.item = item; this.amount = amount; this.type = type; diff --git a/Auto-Tune/src/unprotesting/com/github/util/TransactionSerializer.java b/Auto-Tune/src/unprotesting/com/github/util/TransactionSerializer.java index a81762f..aedfa38 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/TransactionSerializer.java +++ b/Auto-Tune/src/unprotesting/com/github/util/TransactionSerializer.java @@ -27,7 +27,7 @@ public void serialize(DataOutput2 out, Transaction value) throws IOException { public Transaction deserialize(DataInput2 input, int available) throws IOException { Date date = new Date(Date.parse(input.readUTF())); String item = input.readUTF(); - Player player = Bukkit.getPlayer(input.readUTF()); + String player = input.readUTF(); String type = input.readUTF(); int amount = input.readInt(); double total_price = input.readDouble();