Skip to content

Commit

Permalink
remove from data.yml first, then cache if first step is successful.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Oct 14, 2024
1 parent d82b7fb commit 0394518
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void run(InventoryClickEvent event) {

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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void run(InventoryClickEvent event) {

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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ public void run(InventoryClickEvent event) {
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));
Expand All @@ -226,6 +226,8 @@ public void run(InventoryClickEvent event) {

player.getInventory().addItem(auction.asItemStack());

this.userManager.removeExpiredItem(auction);

Files.data.save();

menu.click(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
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));

Expand Down
164 changes: 130 additions & 34 deletions paper/src/main/java/com/badbones69/crazyauctions/tasks/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -56,40 +80,76 @@ public void addAuction(final Player player, final ItemStack itemStack, final lon
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);

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);
isNuked = true;
}
}

if (auction == null) continue;
if (!isNuked || !this.auctions.containsKey(uuid)) return; // only remove from cache, if successfully removed from file.

addActiveAuction(key, auction);
}
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() {
Expand All @@ -116,24 +176,6 @@ public final void updateExpiredCache() {
}
}

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);
Expand All @@ -160,7 +202,7 @@ public void addExpiredItem(final Player player, final AuctionItem auction) {
data.set("active_auctions." + uuid+ "." + auction.getId(), null);

if (!data.contains("active_auctions." + uuid + "." + auction.getId())) {
removeAuction(auction);
removeAuctionItem(auction);
}

Files.data.save();
Expand Down Expand Up @@ -190,6 +232,60 @@ public void addExpiredItem(final Player player, final AuctionItem auction) {
}});
}

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;

Expand Down

0 comments on commit 0394518

Please sign in to comment.