delete non claimed items after 7 days, disabled by default

Took 31 minutes
This commit is contained in:
Kiran Hart 2022-03-15 14:15:39 -04:00
parent 06e33f42ed
commit d666f99a9d
6 changed files with 42 additions and 21 deletions

View File

@ -36,8 +36,6 @@ import co.aikar.taskchain.TaskChain;
import co.aikar.taskchain.TaskChainFactory;
import lombok.Getter;
import lombok.Setter;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderHook;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
@ -53,7 +51,6 @@ import java.util.List;
* Usage of any code found within this class is prohibited unless given explicit permission otherwise
*/
@SuppressWarnings("unused")
public class AuctionHouse extends TweetyPlugin {
private static TaskChainFactory taskChainFactory;
@ -67,9 +64,6 @@ public class AuctionHouse extends TweetyPlugin {
@Getter
private final GuiManager guiManager = new GuiManager(this);
@Getter
private final Config data = new Config(this, "data.yml");
protected Metrics metrics;
@Getter
@ -165,9 +159,6 @@ public class AuctionHouse extends TweetyPlugin {
if (getServer().getPluginManager().isPluginEnabled("CMI"))
Bukkit.getServer().getPluginManager().registerEvents(new CMIListener(), this);
this.data.load();
// auction players
this.auctionPlayerManager = new AuctionPlayerManager();
Bukkit.getOnlinePlayers().forEach(p -> this.auctionPlayerManager.addPlayer(new AuctionPlayer(p)));

View File

@ -210,7 +210,6 @@ public class AuctionAPI {
try {
config.loadFromString(string);
} catch (IllegalArgumentException | InvalidConfigurationException e) {
e.printStackTrace();
return null;
}
return config.getItemStack("i", null);

View File

@ -6,20 +6,27 @@ import ca.tweetzy.auctionhouse.auction.*;
import ca.tweetzy.auctionhouse.auction.enums.AdminAction;
import ca.tweetzy.auctionhouse.auction.enums.AuctionItemCategory;
import ca.tweetzy.auctionhouse.auction.enums.AuctionSaleType;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.auctionhouse.transaction.Transaction;
import ca.tweetzy.core.database.DataManagerAbstract;
import ca.tweetzy.core.database.DatabaseConnector;
import ca.tweetzy.core.database.MySQLConnector;
import ca.tweetzy.core.utils.TextUtils;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.*;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.logging.Level;
/**
* The current file has been created by Kiran Hart
@ -179,10 +186,19 @@ public class DataManager extends DataManagerAbstract {
this.async(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "auctions")) {
ResultSet resultSet = statement.executeQuery();
final List<UUID> toRemove = new ArrayList<>();
while (resultSet.next()) {
items.add(extractAuctionedItem(resultSet));
if (resultSet.getBoolean("expired") && Settings.EXPIRATION_TIME_LIMIT_ENABLED.getBoolean() && Instant.ofEpochMilli(resultSet.getLong("expires_at")).isBefore(Instant.now().minus(Duration.ofHours(Settings.EXPIRATION_TIME_LIMIT.getInt())))) {
toRemove.add(UUID.fromString(resultSet.getString("id")));
} else {
final AuctionedItem item = extractAuctionedItem(resultSet);
if (item != null)
items.add(item);
}
}
deleteItems(toRemove);
callback.accept(null, items);
} catch (Exception e) {
resolveCallback(callback, e);
@ -428,10 +444,17 @@ public class DataManager extends DataManagerAbstract {
}
statement.executeBatch();
}));
}
private AuctionedItem extractAuctionedItem(ResultSet resultSet) throws SQLException {
final ItemStack item = AuctionAPI.decodeItem(resultSet.getString("item"));
if (item == null) {
AuctionHouse.getInstance().getLogger().log(Level.WARNING, "Auction Item with id " + resultSet.getString("id") + " is using an unknown material, it is being skipped!");
return null;
}
AuctionedItem auctionItem = new AuctionedItem(
UUID.fromString(resultSet.getString("id")),
UUID.fromString(resultSet.getString("owner")),
@ -439,7 +462,7 @@ public class DataManager extends DataManagerAbstract {
resultSet.getString("owner_name"),
resultSet.getString("highest_bidder_name"),
AuctionItemCategory.valueOf(resultSet.getString("category")),
AuctionAPI.decodeItem(resultSet.getString("item")),
item,
resultSet.getDouble("base_price"),
resultSet.getDouble("bid_start_price"),
resultSet.getDouble("bid_increment_price"),

View File

@ -33,7 +33,6 @@ public class AuctionItemManager {
private final ConcurrentHashMap<UUID, AuctionedItem> garbageBin = new ConcurrentHashMap<>();
public void start() {
// Attempt to convert from old serialization method
AuctionHouse.getInstance().getDataManager().getItems((error, results) -> {
if (error == null) {
for (AuctionedItem item : results) {
@ -55,6 +54,10 @@ public class AuctionItemManager {
this.garbageBin.put(auctionedItem.getId(), auctionedItem);
}
public void sendToGarbage(@NonNull UUID uuid) {
this.garbageBin.put(uuid, new AuctionedItem());
}
public AuctionedItem getItem(@NonNull UUID id) {
return this.items.getOrDefault(id, null);
}

View File

@ -93,6 +93,8 @@ public class Settings {
public static final ConfigSetting FORCE_CUSTOM_BID_AMOUNT = new ConfigSetting(config, "auction setting.force custom bid amount", false, "If enabled, the bid increment line on auction items will be hidden, bid increment values will be ignored, and when you go to bid on an item, it will ask you to enter a custom amount.");
public static final ConfigSetting LIST_ITEM_DELAY = new ConfigSetting(config, "auction setting.list item delay", -1, "If not set to -1 (disabled) how many seconds must a player wait to list another item after listing 1?");
public static final ConfigSetting FORCE_SYNC_MONEY_ACTIONS = new ConfigSetting(config, "auction setting.force sync money actions", false, "If true, auction house will forcefully run a sync task to withdraw/deposit cash, this does not apply when using the commands");
public static final ConfigSetting EXPIRATION_TIME_LIMIT_ENABLED = new ConfigSetting(config, "auction setting.expiration time limit.enabled", false, "If true, auction house will automatically delete un claimed expired items after 7 days (default)");
public static final ConfigSetting EXPIRATION_TIME_LIMIT = new ConfigSetting(config, "auction setting.expiration time limit.time", 24 * 7, "In hours, what should the minimum age of an unclaimed item be inorder for it to be deleted?");
public static final ConfigSetting ASK_FOR_CANCEL_CONFIRM_ON_BID_ITEMS = new ConfigSetting(config, "auction setting.ask for cancel confirm on bid items", true, "Should Auction House ask the user if they want to cancel the item?");
public static final ConfigSetting ASK_FOR_CANCEL_CONFIRM_ON_NON_BID_ITEMS = new ConfigSetting(config, "auction setting.ask for cancel confirm on non bid items", false, "Should Auction House ask the user if they want to cancel the item?");
@ -545,7 +547,6 @@ public class Settings {
"&7Click here to claim all of your expired auctions"
));
/* ===============================
* TRANSACTIONS TYPE GUI
* ===============================*/

View File

@ -3,9 +3,9 @@ package ca.tweetzy.auctionhouse.tasks;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.AuctionAPI;
import ca.tweetzy.auctionhouse.api.events.AuctionEndEvent;
import ca.tweetzy.auctionhouse.auction.enums.AuctionSaleType;
import ca.tweetzy.auctionhouse.auction.AuctionStat;
import ca.tweetzy.auctionhouse.auction.AuctionedItem;
import ca.tweetzy.auctionhouse.auction.enums.AuctionSaleType;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.core.hooks.EconomyManager;
import ca.tweetzy.core.utils.PlayerUtils;
@ -14,11 +14,7 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.*;
/**
* The current file has been created by Kiran Hart
@ -50,7 +46,15 @@ public class TickAuctionsTask extends BukkitRunnable {
ItemStack itemStack = auctionItem.getItem();
if (!AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().keySet().isEmpty()) {
AuctionHouse.getInstance().getDataManager().deleteItems(AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().values().stream().map(AuctionedItem::getId).collect(Collectors.toList()));
final List<UUID> toClear = new ArrayList<>();
Iterator<UUID> ids = AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().keySet().stream().iterator();
while (ids.hasNext()) {
toClear.add(ids.next());
ids.remove();
}
AuctionHouse.getInstance().getDataManager().deleteItems(toClear);
toClear.clear();
}
if (AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().containsKey(auctionItem.getId())) {