From f82eafcf91f23c840630a3079a7a3e141751a5f9 Mon Sep 17 00:00:00 2001 From: Kiran Hart Date: Mon, 18 Apr 2022 14:49:21 -0400 Subject: [PATCH] improved garbage system, fixed minor bug Took 3 minutes --- .../java/ca/tweetzy/auctionhouse/AuctionHouse.java | 5 +++++ .../tweetzy/auctionhouse/database/DataManager.java | 13 +++++++------ .../tweetzy/auctionhouse/tasks/GarbageBinTask.java | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java b/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java index b799a21..6ed8a7e 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java +++ b/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java @@ -4,6 +4,7 @@ import ca.tweetzy.auctionhouse.api.UpdateChecker; import ca.tweetzy.auctionhouse.api.hook.PlaceholderAPIHook; import ca.tweetzy.auctionhouse.api.hook.UltraEconomyHook; import ca.tweetzy.auctionhouse.auction.AuctionPlayer; +import ca.tweetzy.auctionhouse.auction.AuctionedItem; import ca.tweetzy.auctionhouse.commands.*; import ca.tweetzy.auctionhouse.database.DataManager; import ca.tweetzy.auctionhouse.database.migrations.*; @@ -43,6 +44,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; import java.io.IOException; import java.util.List; +import java.util.stream.Collectors; /** @@ -295,6 +297,9 @@ public class AuctionHouse extends TweetyPlugin { @Override public void onPluginDisable() { if (this.dataManager != null) { + // clean up the garbage items + AuctionHouse.getInstance().getDataManager().deleteItems(AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().values().stream().map(AuctionedItem::getId).collect(Collectors.toList())); + this.auctionItemManager.end(); this.filterManager.saveFilterWhitelist(false); this.auctionBanManager.saveBans(false); diff --git a/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java b/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java index fe235a8..4ffd826 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java +++ b/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java @@ -11,9 +11,6 @@ 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 net.royawesome.jlibnoise.module.combiner.Min; -import org.bukkit.Bukkit; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; @@ -199,7 +196,7 @@ public class DataManager extends DataManagerAbstract { } } - deleteItems(toRemove); + deleteItemsAsync(toRemove); callback.accept(null, items); } catch (Exception e) { resolveCallback(callback, e); @@ -508,7 +505,7 @@ public class DataManager extends DataManagerAbstract { } public void deleteItems(Collection items) { - this.async(() -> this.databaseConnector.connect(connection -> { + this.databaseConnector.connect(connection -> { connection.setAutoCommit(false); PreparedStatement statement = connection.prepareStatement("DELETE FROM " + this.getTablePrefix() + "auctions WHERE id = ?"); for (UUID id : items) { @@ -518,7 +515,11 @@ public class DataManager extends DataManagerAbstract { statement.executeBatch(); connection.setAutoCommit(true); - })); + }); + } + + public void deleteItemsAsync(Collection items) { + this.thread.execute(() -> deleteItems(items)); } private AuctionedItem extractAuctionedItem(ResultSet resultSet) throws SQLException { diff --git a/src/main/java/ca/tweetzy/auctionhouse/tasks/GarbageBinTask.java b/src/main/java/ca/tweetzy/auctionhouse/tasks/GarbageBinTask.java index 0693acd..84ee7fd 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/tasks/GarbageBinTask.java +++ b/src/main/java/ca/tweetzy/auctionhouse/tasks/GarbageBinTask.java @@ -31,14 +31,14 @@ public final class GarbageBinTask extends BukkitRunnable { // timed mode if (Settings.GARBAGE_DELETION_TIMED_MODE.getBoolean()) { - AuctionHouse.getInstance().getDataManager().deleteItems(AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().values().stream().map(AuctionedItem::getId).collect(Collectors.toList())); + AuctionHouse.getInstance().getDataManager().deleteItemsAsync(AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().values().stream().map(AuctionedItem::getId).collect(Collectors.toList())); AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().clear(); return; } // item mode if (AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().size() >= Settings.GARBAGE_DELETION_MAX_ITEMS.getInt()) { - AuctionHouse.getInstance().getDataManager().deleteItems(AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().values().stream().map(AuctionedItem::getId).collect(Collectors.toList())); + AuctionHouse.getInstance().getDataManager().deleteItemsAsync(AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().values().stream().map(AuctionedItem::getId).collect(Collectors.toList())); AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().clear(); } }