further improved garbage system

Took 11 minutes
This commit is contained in:
Kiran Hart 2022-04-20 10:12:28 -04:00
parent 66bdf2a99e
commit 0ccefff5b6
No known key found for this signature in database
GPG Key ID: 5F36C7BC79D3EBC3
5 changed files with 37 additions and 65 deletions

View File

@ -15,7 +15,6 @@ import ca.tweetzy.auctionhouse.managers.*;
import ca.tweetzy.auctionhouse.settings.LocaleSettings;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.auctionhouse.tasks.AutoSaveTask;
import ca.tweetzy.auctionhouse.tasks.GarbageBinTask;
import ca.tweetzy.auctionhouse.tasks.TickAuctionsTask;
import ca.tweetzy.core.TweetyCore;
import ca.tweetzy.core.TweetyPlugin;
@ -248,9 +247,6 @@ public class AuctionHouse extends TweetyPlugin {
// start the auction tick task
TickAuctionsTask.startTask();
// start the garbage collection task
GarbageBinTask.startTask();
// auto save task
if (Settings.AUTO_SAVE_ENABLED.getBoolean()) {
AutoSaveTask.startTask();
@ -298,7 +294,7 @@ public class AuctionHouse extends TweetyPlugin {
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()));
AuctionHouse.getInstance().getDataManager().deleteItems(AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().values().stream().map(AuctionedItem::getId).collect(Collectors.toList()));
this.auctionItemManager.end();
this.filterManager.saveFilterWhitelist(false);

View File

@ -17,21 +17,19 @@ import java.util.concurrent.ConcurrentHashMap;
public class AuctionItemManager {
/*
* If not using usingDynamicLoad, items will be loaded into this map during initialization or when a new item is added
*/
@Getter
private final ConcurrentHashMap<UUID, AuctionedItem> items = new ConcurrentHashMap<>();
@Getter
private final ConcurrentHashMap<AuctionedItem, Long> expiredItems = new ConcurrentHashMap<>();
/**
* Items that are in the garbage bin are essentially marked for disposal
*/
@Getter
private final ConcurrentHashMap<UUID, AuctionedItem> garbageBin = new ConcurrentHashMap<>();
@Getter
private final ConcurrentHashMap<UUID, AuctionedItem> deletedItems = new ConcurrentHashMap<>();
public void start() {
AuctionHouse.getInstance().getDataManager().getItems((error, results) -> {
if (error == null) {

View File

@ -59,6 +59,7 @@ public class Settings {
public static final ConfigSetting LOG_ADMIN_ACTIONS = new ConfigSetting(config, "auction setting.log admin actions", true, "If true, any admin actions made will be logged");
public static final ConfigSetting ROUND_ALL_PRICES = new ConfigSetting(config, "auction setting.round all prices", false, "If true, any decimal numbers will be rounded to the nearest whole number");
public static final ConfigSetting DISABLE_AUTO_SAVE_MSG = new ConfigSetting(config, "auction setting.disable auto save message", false, "If true, auction house will not log the auto save task to the console");
public static final ConfigSetting DISABLE_CLEANUP_MSG = new ConfigSetting(config, "auction setting.disable clean up message", false, "If true, auction house will not log the clean up process to the console");
public static final ConfigSetting TICK_UPDATE_TIME = new ConfigSetting(config, "auction setting.tick auctions every", 1, "How many seconds should pass before the plugin updates all the times on items?");

View File

@ -1,46 +0,0 @@
package ca.tweetzy.auctionhouse.tasks;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.auction.AuctionedItem;
import ca.tweetzy.auctionhouse.settings.Settings;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.stream.Collectors;
/**
* Date Created: April 18 2022
* Time Created: 1:35 p.m.
*
* @author Kiran Hart
*/
public final class GarbageBinTask extends BukkitRunnable {
private static GarbageBinTask instance;
public static GarbageBinTask startTask() {
if (instance == null) {
instance = new GarbageBinTask();
instance.runTaskTimerAsynchronously(AuctionHouse.getInstance(), 20 * 5, (long) 20 * (Settings.GARBAGE_DELETION_TIMED_MODE.getBoolean() ? Settings.GARBAGE_DELETION_TIMED_DELAY.getInt() : 1));
}
return instance;
}
@Override
public void run() {
if (!AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().keySet().isEmpty()) {
// timed mode
if (Settings.GARBAGE_DELETION_TIMED_MODE.getBoolean()) {
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().deleteItemsAsync(AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().values().stream().map(AuctionedItem::getId).collect(Collectors.toList()));
AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().clear();
}
}
}
}

View File

@ -9,12 +9,16 @@ 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;
import ca.tweetzy.core.utils.TextUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
/**
@ -26,9 +30,12 @@ import java.util.stream.Collectors;
public class TickAuctionsTask extends BukkitRunnable {
private static TickAuctionsTask instance;
private static long clock;
public static TickAuctionsTask startTask() {
if (instance == null) {
clock = 0L;
instance = new TickAuctionsTask();
instance.runTaskTimerAsynchronously(AuctionHouse.getInstance(), 0, (long) 20 * Settings.TICK_UPDATE_TIME.getInt());
}
@ -37,27 +44,43 @@ public class TickAuctionsTask extends BukkitRunnable {
@Override
public void run() {
clock += Settings.TICK_UPDATE_TIME.getInt();
Set<Map.Entry<UUID, AuctionedItem>> entrySet = AuctionHouse.getInstance().getAuctionItemManager().getItems().entrySet();
Iterator<Map.Entry<UUID, AuctionedItem>> auctionItemIterator = entrySet.iterator();
while (auctionItemIterator.hasNext()) {
Map.Entry<UUID, AuctionedItem> entry = auctionItemIterator.next();
AuctionedItem auctionItem = entry.getValue();
ItemStack itemStack = auctionItem.getItem();
// todo undo if new garbage deletion breaks
// if (!AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().keySet().isEmpty()) {
// AuctionHouse.getInstance().getDataManager().deleteItems(AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().values().stream().map(AuctionedItem::getId).collect(Collectors.toList()));
// }
if (AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().containsKey(auctionItem.getId())) {
// todo undo if breaks new garbage deletion
//AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().remove(auctionItem.getId());
AuctionHouse.getInstance().getAuctionItemManager().getGarbageBin().remove(auctionItem.getId());
AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().put(auctionItem.getId(), auctionItem);
auctionItemIterator.remove();
continue;
}
// begin the scuffed deletion
if (!AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().keySet().isEmpty()) {
if (Settings.GARBAGE_DELETION_TIMED_MODE.getBoolean() && clock % Settings.GARBAGE_DELETION_TIMED_DELAY.getInt() == 0) {
AuctionHouse.getInstance().getDataManager().deleteItems(AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().values().stream().map(AuctionedItem::getId).collect(Collectors.toList()));
if (!Settings.DISABLE_CLEANUP_MSG.getBoolean())
AuctionHouse.getInstance().getLocale().newMessage(TextUtils.formatText("&aCleaned a total of &e" + AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().size() + "&a items.")).sendPrefixedMessage(Bukkit.getConsoleSender());
AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().clear();
} else {
if (AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().size() >= Settings.GARBAGE_DELETION_MAX_ITEMS.getInt()) {
AuctionHouse.getInstance().getDataManager().deleteItemsAsync(AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().values().stream().map(AuctionedItem::getId).collect(Collectors.toList()));
if (!Settings.DISABLE_CLEANUP_MSG.getBoolean())
AuctionHouse.getInstance().getLocale().newMessage(TextUtils.formatText("&aCleaned a total of &e" + AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().size() + "&a items.")).sendPrefixedMessage(Bukkit.getConsoleSender());
AuctionHouse.getInstance().getAuctionItemManager().getDeletedItems().clear();
}
}
}
// end the scuffed deletion
if (auctionItem.isInfinite()) continue;
long timeRemaining = (auctionItem.getExpiresAt() - System.currentTimeMillis()) / 1000;