From 6dd4b82638565f7f05c83f1349e45a93b28ee1a3 Mon Sep 17 00:00:00 2001 From: Unprotesting Date: Thu, 28 Jul 2022 16:07:52 +1200 Subject: [PATCH] Work on #75 and #79 Refactor AutoTuneInventoryCheckEvent to be more stable (hopefully). Reset buy/sell limits on each time period regardless of player count. --- .../unprotesting/com/github/data/Shop.java | 8 ++ .../events/AutoTuneInventoryCheckEvent.java | 105 +++++++++++------- .../com/github/events/TimePeriodEvent.java | 13 +++ 3 files changed, 86 insertions(+), 40 deletions(-) diff --git a/src/main/java/unprotesting/com/github/data/Shop.java b/src/main/java/unprotesting/com/github/data/Shop.java index 8037c5e..fd84d08 100644 --- a/src/main/java/unprotesting/com/github/data/Shop.java +++ b/src/main/java/unprotesting/com/github/data/Shop.java @@ -275,6 +275,14 @@ public boolean isUnlocked(UUID player) { } } + /** + * Clear the most recent buys/sells. + */ + public void clearRecentPurchases() { + recentBuys.clear(); + recentSells.clear(); + } + private double getSpd() { if (customSpd != -1) { return customSpd; diff --git a/src/main/java/unprotesting/com/github/events/AutoTuneInventoryCheckEvent.java b/src/main/java/unprotesting/com/github/events/AutoTuneInventoryCheckEvent.java index d5b6815..6a9169b 100644 --- a/src/main/java/unprotesting/com/github/events/AutoTuneInventoryCheckEvent.java +++ b/src/main/java/unprotesting/com/github/events/AutoTuneInventoryCheckEvent.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.enchantments.Enchantment; @@ -14,6 +15,7 @@ import unprotesting.com.github.data.CollectFirst.CollectFirstSetting; import unprotesting.com.github.data.Shop; import unprotesting.com.github.data.ShopUtil; +import unprotesting.com.github.util.Format; /** * The event tp check players inventories for items they have auto-sold and @@ -21,7 +23,7 @@ */ public class AutoTuneInventoryCheckEvent extends AutoTuneEvent { - private static List shopNames; + public static Map> autosellItemMaxReached = new HashMap<>(); /** * Checks all online players inventories for autosell items @@ -31,7 +33,6 @@ public class AutoTuneInventoryCheckEvent extends AutoTuneEvent { */ public AutoTuneInventoryCheckEvent(boolean isAsync) { super(isAsync); - shopNames = Arrays.asList(ShopUtil.getShopNames()); for (Player player : Bukkit.getOnlinePlayers()) { checkInventory(player); } @@ -40,59 +41,88 @@ public AutoTuneInventoryCheckEvent(boolean isAsync) { private void checkInventory(Player player) { UUID uuid = player.getUniqueId(); for (ItemStack item : player.getInventory().getContents()) { - runUpdate(item, player, uuid); + + if (item == null) { + continue; + } + + runUpdate(item, player); + + if (item.getEnchantments().isEmpty()) { + continue; + } + + for (Enchantment enchantment : item.getEnchantments().keySet()) { + String name = enchantment.getKey().getKey().toLowerCase(); + Shop shop = getShop(name); + updateCf(name, shop, uuid); + } + } } - private void runUpdate(ItemStack item, @NotNull Player player, @NotNull UUID uuid) { - if (item == null) { + private void runUpdate(ItemStack item, @NotNull Player player) { + + String name = item.getType().toString().toLowerCase(); + Shop shop = getShop(name); + UUID uuid = player.getUniqueId(); + updateCf(name, shop, uuid); + boolean autosellEnabled = Config.get().getAutosell().getBoolean(uuid + "." + name, false); + + if (!autosellEnabled) { return; } - if (item.getEnchantments().size() > 0) { - for (Enchantment enchantment : item.getEnchantments().keySet()) { - String enchantmentName = enchantment.getKey().getKey(); - if (checkIfValidShop(enchantmentName)) { - Shop shop = ShopUtil.getShop(enchantmentName); - updateCf(enchantmentName, shop, uuid, false); + if (ShopUtil.getSellsLeft(player, name) - item.getAmount() < 0) { + if (!autosellItemMaxReached.containsKey(uuid)) { + List list = autosellItemMaxReached.get(uuid); + if (list == null) { + list = Arrays.asList(name); + autosellItemMaxReached.put(uuid, list); + } else { + list.add(name); + } + } else { + List list = autosellItemMaxReached.get(uuid); + if (!list.contains(name)) { + list.add(name); + Format.sendMessage(player, Config.get().getRunOutOfSells()); } } - } - - if (!checkIfValidShop(item.getType().toString())) { return; } - String name = item.getType().toString().toLowerCase(); - Shop shop = ShopUtil.getShop(name); - boolean autosellEnabled = Config.get().getAutosell().getBoolean(uuid + "." + name, false); - boolean update = false; + int amount = item.getAmount(); + HashMap map = player.getInventory().removeItemAnySlot(); - if (item.getEnchantments().size() > 0) { - autosellEnabled = false; + if (!map.isEmpty()) { + amount = amount - map.get(0).getAmount(); } - if (autosellEnabled) { - if (ShopUtil.getSellsLeft(player, name) - item.getAmount() < 0) { - return; - } - HashMap map = player.getInventory().removeItemAnySlot(item); - int amount = item.getAmount(); - if (!map.isEmpty()) { - amount = amount - map.get(0).getAmount(); - } - shop.addAutosell(uuid, amount); - shop.addSells(uuid, amount); - update = true; + shop.addAutosell(uuid, amount); + shop.addSells(uuid, amount); + + } + + private Shop getShop(String shopName) { + if (shopName == null) { + return null; } - updateCf(name, shop, uuid, update); + Shop shop = ShopUtil.getShop(shopName); + + if (shop == null) { + return null; + } + return shop; } - private void updateCf(@NotNull String name, @NotNull Shop shop, - @NotNull UUID uuid, boolean update) { + private void updateCf(@NotNull String name, @NotNull Shop shop, @NotNull UUID uuid) { + + boolean update = false; CollectFirst cf = shop.getSetting(); + if (cf.getSetting().equals(CollectFirstSetting.SERVER)) { if (!cf.isFoundInServer()) { cf.setFoundInServer(true); @@ -110,9 +140,4 @@ private void updateCf(@NotNull String name, @NotNull Shop shop, } } - private boolean checkIfValidShop(@NotNull String name) { - name = name.toLowerCase(); - return shopNames.contains(name); - } - } diff --git a/src/main/java/unprotesting/com/github/events/TimePeriodEvent.java b/src/main/java/unprotesting/com/github/events/TimePeriodEvent.java index 9123507..49e271f 100644 --- a/src/main/java/unprotesting/com/github/events/TimePeriodEvent.java +++ b/src/main/java/unprotesting/com/github/events/TimePeriodEvent.java @@ -1,5 +1,7 @@ package unprotesting.com.github.events; +import java.util.HashMap; + import org.bukkit.Bukkit; import unprotesting.com.github.config.Config; import unprotesting.com.github.config.CsvHandler; @@ -29,6 +31,7 @@ public TimePeriodEvent(boolean isAsync) { if (players < config.getMinimumPlayers() && isAsync) { logger.config("Not enough players to start price update. (" + players + " < " + config.getMinimumPlayers() + ")"); + resetRecentPurchases(); return; } @@ -57,5 +60,15 @@ private void updatePrices() { logger.finest("Strength: " + strength); } } + AutoTuneInventoryCheckEvent.autosellItemMaxReached = new HashMap<>(); + } + + private void resetRecentPurchases() { + for (String s : ShopUtil.getShopNames()) { + Shop shop = ShopUtil.getShop(s); + shop.clearRecentPurchases(); + ShopUtil.putShop(s, shop); + } + AutoTuneInventoryCheckEvent.autosellItemMaxReached = new HashMap<>(); } }