remove from data.yml first, then cache if first step is successful.

This commit is contained in:
Ryder Belserion 2024-10-13 20:17:27 -04:00
parent d82b7fbffd
commit 0394518e75
No known key found for this signature in database
5 changed files with 138 additions and 40 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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));

View File

@ -25,7 +25,31 @@ public class UserManager {
private final Map<UUID, List<ExpiredItem>> 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<AuctionItem> 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<UUID, List<AuctionItem>> getAuctions() {
return this.auctions;
}
public final void updateExpiredCache() {
@ -116,24 +176,6 @@ public class UserManager {
}
}
public final Map<UUID, List<AuctionItem>> 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<ExpiredItem> 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;