Skip to content

Commit

Permalink
Work on #75 and #79
Browse files Browse the repository at this point in the history
Refactor AutoTuneInventoryCheckEvent to be more stable (hopefully). Reset buy/sell limits on each time period regardless of player count.
  • Loading branch information
noahbclarkson committed Jul 28, 2022
1 parent 79e4723 commit 6dd4b82
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 40 deletions.
8 changes: 8 additions & 0 deletions src/main/java/unprotesting/com/github/data/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,14 +15,15 @@
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
* to update the collect first settings.
*/
public class AutoTuneInventoryCheckEvent extends AutoTuneEvent {

private static List<String> shopNames;
public static Map<UUID, List<String>> autosellItemMaxReached = new HashMap<>();

/**
* Checks all online players inventories for autosell items
Expand All @@ -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);
}
Expand All @@ -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<String> list = autosellItemMaxReached.get(uuid);
if (list == null) {
list = Arrays.asList(name);
autosellItemMaxReached.put(uuid, list);
} else {
list.add(name);
}
} else {
List<String> 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<Integer, ItemStack> 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<Integer, ItemStack> 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);
Expand All @@ -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);
}

}
13 changes: 13 additions & 0 deletions src/main/java/unprotesting/com/github/events/TimePeriodEvent.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<>();
}
}

0 comments on commit 6dd4b82

Please sign in to comment.