From 0394518e759ea293dfd42da4acc70e4c169b2e5f Mon Sep 17 00:00:00 2001 From: Ryder Belserion Date: Sun, 13 Oct 2024 20:17:27 -0400 Subject: [PATCH] remove from data.yml first, then cache if first step is successful. --- .../api/guis/types/AuctionsMenu.java | 2 +- .../api/guis/types/CurrentMenu.java | 2 +- .../api/guis/types/ExpiredMenu.java | 6 +- .../commands/AuctionCommand.java | 2 +- .../crazyauctions/tasks/UserManager.java | 166 ++++++++++++++---- 5 files changed, 138 insertions(+), 40 deletions(-) diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/AuctionsMenu.java b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/AuctionsMenu.java index 20433c5..b35e3e7 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/AuctionsMenu.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/AuctionsMenu.java @@ -267,7 +267,7 @@ public class AuctionsMenu extends Holder { final UUID uuid = player.getUniqueId(); - final AuctionItem auction = this.userManager.getAuctionById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); + final AuctionItem auction = this.userManager.getAuctionItemById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); if (auction == null) return; diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/CurrentMenu.java b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/CurrentMenu.java index 64024f9..5d95cfb 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/CurrentMenu.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/CurrentMenu.java @@ -152,7 +152,7 @@ public class CurrentMenu extends Holder { final UUID uuid = player.getUniqueId(); - final AuctionItem auction = this.userManager.getAuctionById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); + final AuctionItem auction = this.userManager.getAuctionItemById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); if (auction == null) return; diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/ExpiredMenu.java b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/ExpiredMenu.java index f9421e2..5505bf4 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/ExpiredMenu.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/ExpiredMenu.java @@ -196,10 +196,10 @@ public class ExpiredMenu extends Holder { if (auction_item == null) continue; player_inventory.addItem(auction_item); - - player_section.set(key, null); } + this.userManager.removeExpiredItems(this.player); + Files.data.save(); player.sendMessage(Messages.GOT_ITEM_BACK.getMessage(player)); @@ -226,6 +226,8 @@ public class ExpiredMenu extends Holder { player.getInventory().addItem(auction.asItemStack()); + this.userManager.removeExpiredItem(auction); + Files.data.save(); menu.click(player); diff --git a/paper/src/main/java/com/badbones69/crazyauctions/commands/AuctionCommand.java b/paper/src/main/java/com/badbones69/crazyauctions/commands/AuctionCommand.java index 647ea5a..54873ed 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/commands/AuctionCommand.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/commands/AuctionCommand.java @@ -342,7 +342,7 @@ public class AuctionCommand implements CommandExecutor { ItemStack stack = item.clone(); stack.setAmount(amount); - this.userManager.addAuction(player, stack, price, args[0].equalsIgnoreCase("bid")); + this.userManager.addAuctionItem(player, stack, price, args[0].equalsIgnoreCase("bid")); this.plugin.getServer().getPluginManager().callEvent(new AuctionListEvent(player, type, stack, price)); diff --git a/paper/src/main/java/com/badbones69/crazyauctions/tasks/UserManager.java b/paper/src/main/java/com/badbones69/crazyauctions/tasks/UserManager.java index d14f038..7891890 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/tasks/UserManager.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/tasks/UserManager.java @@ -25,7 +25,31 @@ public class UserManager { private final Map> expired_items = new HashMap<>(); - public void addAuction(final Player player, final ItemStack itemStack, final long price, final boolean isBiddable) { + public final void updateAuctionsCache() { + this.auctions.clear(); + + final FileConfiguration data = Files.data.getConfiguration(); + + final ConfigurationSection section = data.getConfigurationSection("active_auctions"); + + if (section == null) return; + + for (String key : section.getKeys(false)) { + final ConfigurationSection player = section.getConfigurationSection(key); + + if (player == null) continue; + + for (String number : player.getKeys(false)) { + final ConfigurationSection auction = player.getConfigurationSection(number); + + if (auction == null) continue; + + addActiveAuction(key, auction); + } + } + } + + public void addAuctionItem(final Player player, final ItemStack itemStack, final long price, final boolean isBiddable) { final FileConfiguration data = Files.data.getConfiguration(); final FileConfiguration config = Files.config.getConfiguration(); @@ -56,11 +80,29 @@ public class UserManager { addItem(itemStack, uuid, price, isBiddable, config, section); } - public void removeAuction(final AuctionItem auction) { - if (!this.auctions.containsKey(auction.getUuid())) { - return; + public void removeAuctionItem(final AuctionItem auction) { + final FileConfiguration data = Files.data.getConfiguration(); + + final ConfigurationSection section = data.getConfigurationSection("active_auctions"); + + boolean isNuked = false; + + final UUID uuid = auction.getUuid(); + + if (section != null) { + final ConfigurationSection player_section = section.getConfigurationSection(uuid.toString()); + + final String id = auction.getId(); + + if (player_section != null && player_section.contains(id)) { + player_section.set(id, null); + + isNuked = true; + } } + if (!isNuked || !this.auctions.containsKey(uuid)) return; // only remove from the cache, if the data file removal was successful! + final List auctions = this.auctions.get(auction.getUuid()); auctions.remove(auction); @@ -68,28 +110,46 @@ public class UserManager { this.auctions.put(auction.getUuid(), auctions); } - public final void updateAuctionsCache() { - this.auctions.clear(); + public void removeAuctionItems(final Player player) { + final UUID uuid = player.getUniqueId(); final FileConfiguration data = Files.data.getConfiguration(); final ConfigurationSection section = data.getConfigurationSection("active_auctions"); - if (section == null) return; + boolean isNuked = false; - for (String key : section.getKeys(false)) { - final ConfigurationSection player = section.getConfigurationSection(key); + if (section != null) { + final ConfigurationSection player_section = section.getConfigurationSection(uuid.toString()); - if (player == null) continue; + if (player_section != null) { + section.set(uuid.toString(), null); - for (String number : player.getKeys(false)) { - final ConfigurationSection auction = player.getConfigurationSection(number); - - if (auction == null) continue; - - addActiveAuction(key, auction); + isNuked = true; } } + + if (!isNuked || !this.auctions.containsKey(uuid)) return; // only remove from cache, if successfully removed from file. + + this.auctions.remove(uuid); + } + + public final AuctionItem getAuctionItemById(final UUID uuid, final String store_id) { + AuctionItem key = null; + + for (final AuctionItem auction : this.auctions.get(uuid)) { + if (!auction.getStoreID().equals(store_id)) continue; + + key = auction; + + break; + } + + return key; + } + + public final Map> getAuctions() { + return this.auctions; } public final void updateExpiredCache() { @@ -116,24 +176,6 @@ public class UserManager { } } - public final Map> getAuctions() { - return this.auctions; - } - - public final AuctionItem getAuctionById(final UUID uuid, final String store_id) { - AuctionItem key = null; - - for (final AuctionItem auction : this.auctions.get(uuid)) { - if (!auction.getStoreID().equals(store_id)) continue; - - key = auction; - - break; - } - - return key; - } - public void addExpiredItem(final Player player, final AuctionItem auction) { final AuctionCancelledEvent auctionCancelledEvent = new AuctionCancelledEvent(player, auction.asItemStack(), Reasons.PLAYER_FORCE_CANCEL); this.plugin.getServer().getPluginManager().callEvent(auctionCancelledEvent); @@ -160,7 +202,7 @@ public class UserManager { data.set("active_auctions." + uuid+ "." + auction.getId(), null); if (!data.contains("active_auctions." + uuid + "." + auction.getId())) { - removeAuction(auction); + removeAuctionItem(auction); } Files.data.save(); @@ -190,6 +232,60 @@ public class UserManager { }}); } + public void removeExpiredItem(final ExpiredItem expiredItem) { + final FileConfiguration data = Files.data.getConfiguration(); + + final ConfigurationSection section = data.getConfigurationSection("expired_auctions"); + + boolean isNuked = false; + + final UUID uuid = expiredItem.getUuid(); + + if (section != null) { + final ConfigurationSection player_section = section.getConfigurationSection(uuid.toString()); + + final String id = expiredItem.getId(); + + if (player_section != null && player_section.contains(id)) { + player_section.set(id, null); + + isNuked = true; + } + } + + if (!isNuked || !this.expired_items.containsKey(uuid)) return; // only remove from the cache, if the data file removal was successful! + + final List auctions = this.expired_items.get(uuid); + + auctions.remove(expiredItem); + + this.expired_items.put(expiredItem.getUuid(), auctions); + } + + public void removeExpiredItems(final Player player) { + final UUID uuid = player.getUniqueId(); + + final FileConfiguration data = Files.data.getConfiguration(); + + final ConfigurationSection section = data.getConfigurationSection("expired_auctions"); + + boolean isNuked = false; + + if (section != null) { + final ConfigurationSection player_section = section.getConfigurationSection(uuid.toString()); + + if (player_section != null) { + section.set(uuid.toString(), null); + + isNuked = true; + } + } + + if (!isNuked || !this.expired_items.containsKey(uuid)) return; // only remove from cache, if successfully removed from file. + + this.expired_items.remove(uuid); + } + public final ExpiredItem getExpiredItemById(final UUID uuid, final String store_id) { ExpiredItem key = null;