make proper use of the auction cache in 2 of the menus

This commit is contained in:
Ryder Belserion 2024-10-13 17:05:41 -04:00
parent d205cdb500
commit 578b8076c2
No known key found for this signature in database
4 changed files with 138 additions and 176 deletions

View File

@ -3,23 +3,19 @@ package com.badbones69.crazyauctions.api.guis.types;
import com.badbones69.crazyauctions.Methods;
import com.badbones69.crazyauctions.api.builders.ItemBuilder;
import com.badbones69.crazyauctions.api.enums.Category;
import com.badbones69.crazyauctions.api.enums.Messages;
import com.badbones69.crazyauctions.api.enums.Reasons;
import com.badbones69.crazyauctions.api.enums.ShopType;
import com.badbones69.crazyauctions.api.enums.misc.Files;
import com.badbones69.crazyauctions.api.enums.misc.Keys;
import com.badbones69.crazyauctions.api.events.AuctionCancelledEvent;
import com.badbones69.crazyauctions.api.guis.Holder;
import com.badbones69.crazyauctions.api.guis.HolderManager;
import com.badbones69.crazyauctions.api.GuiManager;
import com.badbones69.crazyauctions.currency.VaultSupport;
import com.badbones69.crazyauctions.tasks.InventoryManager;
import com.badbones69.crazyauctions.tasks.objects.Auction;
import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable;
import io.papermc.paper.persistence.PersistentDataContainerView;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -30,7 +26,7 @@ import java.util.UUID;
public class AuctionsMenu extends Holder {
private List<ItemStack> items;
private List<Auction> items;
private List<String> options;
private int maxPages;
@ -71,8 +67,6 @@ public class AuctionsMenu extends Holder {
@Override
public final Holder build() {
Methods.updateAuction();
if (this.target != null) {
this.options.add("WhatIsThis.Viewing");
} else {
@ -144,10 +138,10 @@ public class AuctionsMenu extends Holder {
}
}
for (final ItemStack item : getPageItems(this.items, getPage(), getSize())) {
for (final Auction item : getPageItems(this.items, getPage(), getSize())) {
int slot = this.inventory.firstEmpty();
this.inventory.setItem(slot, item);
this.inventory.setItem(slot, item.getItemBuilder(this.shopType).build());
}
this.player.openInventory(this.inventory);
@ -178,7 +172,6 @@ public class AuctionsMenu extends Holder {
final Player player = (Player) event.getWhoClicked();
FileConfiguration config = Files.config.getConfiguration();
FileConfiguration data = Files.data.getConfiguration();
if (container.has(Keys.auction_button.getNamespacedKey())) {
String type = container.getOrDefault(Keys.auction_button.getNamespacedKey(), PersistentDataType.STRING, menu.target == null ? "Refresh" : "");
@ -272,59 +265,13 @@ public class AuctionsMenu extends Holder {
}
}
final UUID uuid = player.getUniqueId();
/*if (!data.contains("Items")) return;
final ConfigurationSection section = data.getConfigurationSection("Items");
if (section == null) return;
final String auction_id = container.getOrDefault(Keys.auction_item.getNamespacedKey(), PersistentDataType.STRING, "");
final ConfigurationSection auction = section.getConfigurationSection(auction_id);
final Auction auction = this.userManager.getAuctionById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, ""));
if (auction == null) return;
final UUID uuid = player.getUniqueId();
if (player.hasPermission("crazyauctions.admin") || player.hasPermission("crazyauctions.force-end")) {
if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
int num = 1;
for (;data.contains("OutOfTime/Cancelled." + num); num++);
String seller = auction.getString("Seller");
Player sellerPlayer = Methods.getPlayer(seller);
if (Methods.isOnline(seller) && sellerPlayer != null) {
sellerPlayer.sendMessage(Messages.ADMIN_FORCE_CANCELLED_TO_PLAYER.getMessage(player));
}
AuctionCancelledEvent auctionCancelledEvent = new AuctionCancelledEvent((sellerPlayer != null ? sellerPlayer : Methods.getOfflinePlayer(seller)), Methods.fromBase64(auction.getString("Item")), Reasons.ADMIN_FORCE_CANCEL);
this.server.getPluginManager().callEvent(auctionCancelledEvent);
data.set("OutOfTime/Cancelled." + num + ".Seller", section.getString("Seller"));
data.set("OutOfTime/Cancelled." + num + ".Name", section.getString("Name"));
data.set("OutOfTime/Cancelled." + num + ".Full-Time", section.getLong("Full-Time"));
data.set("OutOfTime/Cancelled." + num + ".StoreID", section.getInt("StoreID"));
data.set("OutOfTime/Cancelled." + num + ".Item", auction.getString("Item"));
data.set("Items." + auction_id, null);
Files.data.save();
player.sendMessage(Messages.ADMIN_FORCE_CANCELLED.getMessage(player));
menu.click(player);
GuiManager.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), menu.getPage());
return;
}
}
if (auction.getString("Seller", "").equalsIgnoreCase(uuid.toString())) {
if (uuid.toString().equalsIgnoreCase(auction.getUuid().toString())) {
String itemName = config.getString("Settings.GUISettings.OtherSettings.Your-Item.Item");
String name = config.getString("Settings.GUISettings.OtherSettings.Your-Item.Name");
@ -348,9 +295,13 @@ public class AuctionsMenu extends Holder {
return;
}
long cost = auction.getLong("Price");
final long price = auction.getPrice();
if (this.plugin.getSupport().getMoney(player) < cost) {
if (price == 0L) return;
final VaultSupport support = this.plugin.getSupport();
if (support.getMoney(player) < price) {
String itemName = config.getString("Settings.GUISettings.OtherSettings.Cant-Afford.Item");
String name = config.getString("Settings.GUISettings.OtherSettings.Cant-Afford.Name");
@ -373,8 +324,10 @@ public class AuctionsMenu extends Holder {
return;
}
if (auction.getBoolean("Biddable")) {
if (uuid.toString().equalsIgnoreCase(auction.getString("TopBidder"))) {
final String auction_id = auction.getId();
if (auction.isBiddable()) {
if (uuid.toString().equalsIgnoreCase(auction.getUuid().toString())) {
String itemName = config.getString("Settings.GUISettings.OtherSettings.Top-Bidder.Item");
String name = config.getString("Settings.GUISettings.OtherSettings.Top-Bidder.Name");
@ -401,16 +354,17 @@ public class AuctionsMenu extends Holder {
menu.click(player);
GuiManager.openBidding(player, auction_id);
} else {
return;
}
menu.click(player);
GuiManager.openBuying(player, auction_id);
}*/
}
private void getItems() {
final List<Auction> auctions = this.plugin.getUserManager().getAuctions();
this.userManager.getAuctions().forEach(((uuid, auctions) -> {
auctions.forEach(auction -> {
final ItemBuilder itemBuilder = auction.getItemBuilder(this.shopType);
@ -418,7 +372,8 @@ public class AuctionsMenu extends Holder {
return;
}
this.items.add(itemBuilder.build());
this.items.add(auction);
});
}));
}
}

View File

@ -10,6 +10,7 @@ import com.badbones69.crazyauctions.api.events.AuctionCancelledEvent;
import com.badbones69.crazyauctions.api.guis.Holder;
import com.badbones69.crazyauctions.api.guis.HolderManager;
import com.badbones69.crazyauctions.api.GuiManager;
import com.badbones69.crazyauctions.tasks.objects.Auction;
import io.papermc.paper.persistence.PersistentDataContainerView;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
@ -20,40 +21,28 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
public class CurrentMenu extends Holder {
private List<ItemStack> items;
private List<String> options;
private FileConfiguration config;
private FileConfiguration data;
private List<String> options;
public CurrentMenu(final Player player, final String title, final int size, final int page) {
super(player, title, size, page);
this.items = new ArrayList<>();
this.options = new ArrayList<>();
this.config = Files.config.getConfiguration();
this.data = Files.data.getConfiguration();
this.options = new ArrayList<>();
}
public CurrentMenu() {}
@Override
public final Holder build() {
Methods.updateAuction();
this.options.addAll(List.of(
"Back",
"WhatIsThis.CurrentItems"
));
getItems();
for (final String key : this.options) {
if (!this.config.contains("Settings.GUISettings.OtherSettings." + key)) {
continue;
@ -76,10 +65,10 @@ public class CurrentMenu extends Holder {
this.inventory.setItem(slot - 1, itemBuilder.build());
}
for (final ItemStack item : getPageItems(this.items, getPage(), getSize())) {
for (final Auction item : getPageItems(this.userManager.getAuctions().get(this.player.getUniqueId()), getPage(), getSize())) {
int slot = this.inventory.firstEmpty();
this.inventory.setItem(slot, item);
this.inventory.setItem(slot, item.getItemBuilder().build());
}
this.player.openInventory(this.inventory);
@ -125,7 +114,7 @@ public class CurrentMenu extends Holder {
return;
}
String id = container.getOrDefault(Keys.auction_item.getNamespacedKey(), PersistentDataType.STRING, "");
String id = container.getOrDefault(Keys.auction_number.getNamespacedKey(), PersistentDataType.STRING, "");
final FileConfiguration data = Files.data.getConfiguration();
@ -176,57 +165,4 @@ public class CurrentMenu extends Holder {
GuiManager.openPlayersCurrentList(player, 1);
}
private void getItems() {
final ConfigurationSection section = this.data.getConfigurationSection("Items");
if (section == null) return;
final UUID uuid = this.player.getUniqueId();
for (String key : section.getKeys(false)) {
final ConfigurationSection auction = section.getConfigurationSection(key);
if (auction == null) continue;
final String seller = auction.getString("Seller", "");
if (seller.isEmpty()) continue;
if (!seller.equalsIgnoreCase(uuid.toString())) continue;
final String item = auction.getString("Item", "");
if (item.isEmpty()) continue;
final ItemBuilder itemBuilder = ItemBuilder.convertItemStack(item);
if (itemBuilder == null) {
this.plugin.getLogger().warning("The item with store id " + auction.getString("StoreID", "current_menu") + " obtained from your data.yml could not be converted!");
continue;
}
final long price = auction.getLong("Price");
final String priceFormat = String.format(Locale.ENGLISH, "%,d", price);
final String time = Methods.convertToTime(auction.getLong("Time-Till-Expire"));
final List<String> lore = new ArrayList<>(itemBuilder.getUpdatedLore());
lore.add(" ");
for (final String line : this.config.getStringList("Settings.GUISettings.CurrentLore")) {
lore.add(line.replace("%Time%", time).replace("%time%", time).replace("%price%", priceFormat).replace("%Price%", priceFormat));
}
itemBuilder.setLore(lore);
itemBuilder.addInteger(auction.getInt("StoreID"), Keys.auction_id.getNamespacedKey());
itemBuilder.addString(auction.getName(), Keys.auction_item.getNamespacedKey());
this.items.add(itemBuilder.build());
}
}
}

View File

@ -9,13 +9,17 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class UserManager {
private final CrazyAuctions plugin = CrazyAuctions.getPlugin();
private final Map<UUID, List<Auction>> auctions = new HashMap<>();
public void addAuction(final Player player, final ItemStack itemStack, final long price, final boolean isBiddable) {
final FileConfiguration data = Files.data.getConfiguration();
final FileConfiguration config = Files.config.getConfiguration();
@ -31,7 +35,7 @@ public class UserManager {
if (section == null) return;
addItem(itemStack, price, isBiddable, config, section);
addItem(itemStack, uuid, price, isBiddable, config, section);
return;
}
@ -44,17 +48,17 @@ public class UserManager {
if (section == null) return;
addItem(itemStack, price, isBiddable, config, section);
addItem(itemStack, uuid, price, isBiddable, config, section);
}
public final List<Auction> getAuctions() {
final List<Auction> auctions = new ArrayList<>();
public final void updateAuctionsCache() {
this.auctions.clear();
final FileConfiguration data = Files.data.getConfiguration();
final ConfigurationSection section = data.getConfigurationSection("active_auctions");
if (section == null) return auctions;
if (section == null) return;
for (String key : section.getKeys(false)) {
final ConfigurationSection player = section.getConfigurationSection(key);
@ -66,20 +70,27 @@ public class UserManager {
if (auction == null) continue;
auctions.add(new Auction(key, auction.getString("name"),
number,
auction.getString("item"),
auction.getString("store_id"),
auction.getLong("price"),
auction.getLong("time.expire"),
auction.getLong("time.full"),
auction.getString("status.top_bidder.uuid", "None"),
auction.getString("status.top_bidder.name", "None"),
auction.getBoolean("status.biddable", false)));
addCache(key, auction);
}
}
}
return auctions;
public final Map<UUID, List<Auction>> getAuctions() {
return this.auctions;
}
public final Auction getAuctionById(final UUID uuid, final String store_id) {
Auction key = null;
for (final Auction auction : this.auctions.get(uuid)) {
if (!auction.getStoreID().equals(store_id)) continue;
key = auction;
break;
}
return key;
}
public void migrateAuctions() {
@ -148,7 +159,7 @@ public class UserManager {
* @param config the config.yml
* @param section the section in the data.yml
*/
private void addItem(final ItemStack itemStack, final long price, final boolean isBiddable, final FileConfiguration config, final ConfigurationSection section) {
private void addItem(final ItemStack itemStack, final String uuid, final long price, final boolean isBiddable, final FileConfiguration config, final ConfigurationSection section) {
section.set("item", Methods.toBase64(itemStack));
section.set("store_id", UUID.randomUUID().toString().replace("-", "").substring(0, 8));
section.set("price", price);
@ -165,6 +176,36 @@ public class UserManager {
section.set("status.top_bidder.name", "None");
section.set("status.biddable", isBiddable);
addCache(uuid, section);
Files.data.save();
}
private void addCache(final String uuid, final ConfigurationSection section) {
final Auction new_auction = new Auction(uuid, section.getString("name"),
section.getName(),
section.getString("item"),
section.getString("store_id"),
section.getLong("price"),
section.getLong("time.expire"),
section.getLong("time.full"),
section.getString("status.top_bidder.uuid", "None"),
section.getString("status.top_bidder.name", "None"),
section.getBoolean("status.biddable")
);
final UUID fromString = UUID.fromString(uuid);
if (this.auctions.containsKey(fromString)) {
final List<Auction> auctions = this.auctions.get(fromString);
auctions.add(new_auction);
this.auctions.put(fromString, auctions);
} else {
this.auctions.put(fromString, new ArrayList<>() {{
add(new_auction);
}});
}
}
}

View File

@ -91,7 +91,7 @@ public class Auction {
public final ItemBuilder getItemBuilder(final ShopType shopType) {
final FileConfiguration configuration = Files.config.getConfiguration();
final ItemBuilder itemBuilder = ItemBuilder.convertItemStack(this.itemStack);
final ItemBuilder itemBuilder = ItemBuilder.convertItemStack(this.itemStack.clone());
final String priceFormat = String.format(Locale.ENGLISH, "%,d", this.price);
@ -103,29 +103,59 @@ public class Auction {
if (shopType == ShopType.BID && isBiddable()) {
for (final String line : configuration.getStringList("Settings.GUISettings.Bidding")) {
String newLine = line.replace("%TopBid%", priceFormat).replace("%topbid%", priceFormat);
String newLine = line.replace("%TopBid%", priceFormat)
.replace("%topbid%", priceFormat);
newLine = line.replace("%Seller%", getName()).replace("%seller%", getName());
newLine = line.replace("%Seller%", getName())
.replace("%seller%", getName());
newLine = line.replace("%TopBidder%", getBidderName()).replace("%topbid%", getBidderName());
newLine = line.replace("%TopBidder%", getBidderName())
.replace("%topbid%", getBidderName());
lore.add(newLine.replace("%Time%", time).replace("%time%", time));
lore.add(newLine.replace("%Time%", time)
.replace("%time%", time));
}
} else {
for (final String line : configuration.getStringList("Settings.GUISettings.SellingItemLore")) {
String newLine = line.replace("%TopBid%", priceFormat).replace("%topbid%", priceFormat);
String newLine = line.replace("%TopBid%", priceFormat)
.replace("%topbid%", priceFormat);
newLine = line.replace("%Seller%", getName()).replace("%seller%", getName());
newLine = line.replace("%Seller%", getName())
.replace("%seller%", getName());
lore.add(newLine.replace("%Time%", time).replace("%time%", time).replace("%price%", priceFormat).replace("%Price%", priceFormat));
lore.add(newLine.replace("%Time%", time)
.replace("%time%", time)
.replace("%price%", priceFormat).replace("%Price%", priceFormat));
}
}
itemBuilder.setLore(lore);
itemBuilder.setLore(lore)
.addString(getStoreID(), Keys.auction_store_id.getNamespacedKey());
itemBuilder.addString(getStoreID(), Keys.auction_id.getNamespacedKey());
itemBuilder.addString(String.valueOf(getUuid()), Keys.auction_uuid.getNamespacedKey());
itemBuilder.addString(getId(), Keys.auction_item.getNamespacedKey());
return itemBuilder;
}
public final ItemBuilder getItemBuilder() {
final FileConfiguration configuration = Files.config.getConfiguration();
final ItemBuilder itemBuilder = ItemBuilder.convertItemStack(this.itemStack.clone());
final String priceFormat = String.format(Locale.ENGLISH, "%,d", this.price);
final String time = Methods.convertToTime(getTimeTillExpire());
final List<String> lore = new ArrayList<>(itemBuilder.getUpdatedLore());
lore.add(" ");
for (final String line : configuration.getStringList("Settings.GUISettings.CurrentLore")) {
lore.add(line.replace("%Time%", time).replace("%time%", time)
.replace("%price%", priceFormat)
.replace("%Price%", priceFormat));
}
itemBuilder.setLore(lore)
.addString(getStoreID(), Keys.auction_store_id.getNamespacedKey());;
return itemBuilder;
}