diff --git a/paper/src/main/java/com/badbones69/crazyauctions/CrazyAuctions.java b/paper/src/main/java/com/badbones69/crazyauctions/CrazyAuctions.java index 4234449..7691ebb 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/CrazyAuctions.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/CrazyAuctions.java @@ -58,8 +58,19 @@ public class CrazyAuctions extends Vital { this.userManager = new UserManager(); this.userManager.updateAuctionsCache(); - this.crazyManager = new CrazyManager(); + // we want to update this cache, after the cache above... because we will also calculate if items are expired! + this.userManager.updateExpiredCache(); + /*new FoliaRunnable(getServer().getGlobalRegionScheduler()) { + @Override + public void run() { + userManager.updateAuctionsCache(); + + //todo() update existing inventories. + } + }.runAtFixedRate(this, 20, 300 * 5);*/ + + this.crazyManager = new CrazyManager(); this.crazyManager.load(); final PluginManager manager = getServer().getPluginManager(); diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/Holder.java b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/Holder.java index cad3154..c659012 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/Holder.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/Holder.java @@ -3,10 +3,12 @@ package com.badbones69.crazyauctions.api.guis; import com.badbones69.crazyauctions.CrazyAuctions; import com.badbones69.crazyauctions.Methods; import com.badbones69.crazyauctions.api.CrazyManager; +import com.badbones69.crazyauctions.api.builders.ItemBuilder; import com.badbones69.crazyauctions.api.enums.ShopType; import com.badbones69.crazyauctions.api.enums.misc.Files; import com.badbones69.crazyauctions.tasks.UserManager; -import com.badbones69.crazyauctions.tasks.objects.Auction; +import com.badbones69.crazyauctions.tasks.objects.AuctionItem; +import com.badbones69.crazyauctions.tasks.objects.ExpiredItem; import com.ryderbelserion.vital.paper.api.enums.Support; import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.Server; @@ -94,8 +96,8 @@ public abstract class Holder implements InventoryHolder, Listener { return this.page; } - public final List getPageItems(final List list, int page, final int size) { - List items = new ArrayList<>(); + public final List getPageItem(final List list, int page, final int size) { + List items = new ArrayList<>(); if (page <= 0) page = 1; @@ -121,7 +123,40 @@ public abstract class Holder implements InventoryHolder, Listener { return items; } - public final int getMaxPage(final List list) { + public final List getPageItems(final List list, int page, final int size) { + List items = new ArrayList<>(); + + if (page <= 0) page = 1; + + int index = page * size - size; + int endIndex = index >= list.size() ? list.size() - 1 : index + size; + + for (;index < endIndex; index++) { + if (index < list.size()) items.add(list.get(index)); + } + + for (;items.isEmpty(); page--) { + if (page <= 0) break; + + index = page * size - size; + + endIndex = index >= list.size() ? list.size() - 1 : index + size; + + for (; index < endIndex; index++) { + if (index < list.size()) items.add(list.get(index)); + } + } + + return items; + } + + public final int getExpiredMaxPages(final List list) { + final int size = list.size(); + + return (int) Math.ceil((double) size / getSize()); + } + + public final int getMaxPage(final List list) { final int size = list.size(); return (int) Math.ceil((double) size / getSize()); 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 754c927..20433c5 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 @@ -11,7 +11,7 @@ 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.badbones69.crazyauctions.tasks.objects.AuctionItem; import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; import io.papermc.paper.persistence.PersistentDataContainerView; import org.bukkit.configuration.file.FileConfiguration; @@ -26,7 +26,7 @@ import java.util.UUID; public class AuctionsMenu extends Holder { - private List items; + private List items; private List options; private int maxPages; @@ -138,10 +138,10 @@ public class AuctionsMenu extends Holder { } } - for (final Auction item : getPageItems(this.items, getPage(), getSize())) { + for (final AuctionItem item : getPageItems(this.items, getPage(), getSize())) { int slot = this.inventory.firstEmpty(); - this.inventory.setItem(slot, item.getItemBuilder(this.shopType).build()); + this.inventory.setItem(slot, item.getActiveItem(this.shopType).build()); } this.player.openInventory(this.inventory); @@ -267,7 +267,7 @@ public class AuctionsMenu extends Holder { final UUID uuid = player.getUniqueId(); - final Auction auction = this.userManager.getAuctionById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); + final AuctionItem auction = this.userManager.getAuctionById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); if (auction == null) return; @@ -364,16 +364,14 @@ public class AuctionsMenu extends Holder { } private void getItems() { - this.userManager.getAuctions().forEach(((uuid, auctions) -> { - auctions.forEach(auction -> { - final ItemBuilder itemBuilder = auction.getItemBuilder(this.shopType); + this.userManager.getAuctions().forEach(((uuid, auctions) -> auctions.forEach(auction -> { + final ItemBuilder itemBuilder = auction.getActiveItem(this.shopType); - if (this.category != null && this.category != Category.NONE && !this.category.getItems().contains(itemBuilder.getMaterial())) { - return; - } + if (this.category != null && this.category != Category.NONE && !this.category.getItems().contains(itemBuilder.getMaterial())) { + return; + } - this.items.add(auction); - }); - })); + this.items.add(auction); + }))); } } \ No newline at end of file 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 f2ae642..64024f9 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 @@ -2,16 +2,13 @@ package com.badbones69.crazyauctions.api.guis.types; import com.badbones69.crazyauctions.api.builders.ItemBuilder; import com.badbones69.crazyauctions.api.enums.Messages; -import com.badbones69.crazyauctions.api.enums.Reasons; 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.tasks.objects.Auction; +import com.badbones69.crazyauctions.tasks.objects.AuctionItem; 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.InventoryClickEvent; @@ -26,7 +23,7 @@ public class CurrentMenu extends Holder { private FileConfiguration config; private List options; - private List items; + private List items; private int maxPages; public CurrentMenu(final Player player, final String title, final int size, final int page) { @@ -74,10 +71,10 @@ public class CurrentMenu extends Holder { this.inventory.setItem(slot - 1, itemBuilder.build()); } - for (final Auction item : getPageItems(this.items, getPage(), getSize())) { + for (final AuctionItem item : getPageItems(this.items, getPage(), getSize())) { int slot = this.inventory.firstEmpty(); - this.inventory.setItem(slot, item.getItemBuilder().build()); + this.inventory.setItem(slot, item.getCurrentItem().build()); } this.player.openInventory(this.inventory); @@ -155,36 +152,11 @@ public class CurrentMenu extends Holder { final UUID uuid = player.getUniqueId(); - final Auction auction = this.userManager.getAuctionById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); + final AuctionItem auction = this.userManager.getAuctionById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); if (auction == null) return; - AuctionCancelledEvent auctionCancelledEvent = new AuctionCancelledEvent(player, auction.asItemStack(), Reasons.PLAYER_FORCE_CANCEL); - this.plugin.getServer().getPluginManager().callEvent(auctionCancelledEvent); - - final FileConfiguration data = Files.data.getConfiguration(); - - int number = 1; - - for (;data.contains("expired_auctions." + uuid + "." + number); number++); - - data.set("expired_auctions." + uuid + "." + number + ".name", auction.getName()); - - final ConfigurationSection section = data.getConfigurationSection("expired_auctions." + uuid + "." + number); - - if (section == null) return; - - section.set("item", auction.asBase64()); - section.set("store_id", auction.getStoreID()); - section.set("full_time", auction.getFullExpire()); - - data.set("active_auctions." + uuid+ "." + auction.getId(), null); - - if (!data.contains("active_auctions." + uuid + "." + auction.getId())) { - this.userManager.removeAuction(auction); - } - - Files.data.save(); + this.userManager.addExpiredItem(player, auction); player.sendMessage(Messages.CANCELLED_ITEM.getMessage(player)); 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 53a5572..f9421e2 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 @@ -9,6 +9,7 @@ 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.InventoryManager; +import com.badbones69.crazyauctions.tasks.objects.ExpiredItem; import io.papermc.paper.persistence.PersistentDataContainerView; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; @@ -19,17 +20,14 @@ 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 ExpiredMenu extends Holder { - private List items; + private List items; private List options; private FileConfiguration config; - private FileConfiguration data; - private int maxPages; public ExpiredMenu(final Player player, final String title, final int size, final int page) { @@ -39,7 +37,6 @@ public class ExpiredMenu extends Holder { this.options = new ArrayList<>(); this.config = Files.config.getConfiguration(); - this.data = Files.data.getConfiguration(); } public ExpiredMenu() {} @@ -54,9 +51,9 @@ public class ExpiredMenu extends Holder { "WhatIsThis.Cancelled/ExpiredItems" )); - getItems(); + this.items = this.userManager.getExpiredItems().get(this.player.getUniqueId()); - this.maxPages = getMaxPage(this.items); + this.maxPages = getExpiredMaxPages(this.items); for (final String key : this.options) { if (!this.config.contains("Settings.GUISettings.OtherSettings." + key)) { @@ -86,10 +83,10 @@ public class ExpiredMenu extends Holder { } } - for (final ItemStack item : getPageItems(this.items, getPage(), getSize())) { + for (final ExpiredItem item : getPageItem(this.items, getPage(), getSize())) { int slot = this.inventory.firstEmpty(); - this.inventory.setItem(slot, item); + this.inventory.setItem(slot, item.getExpiredItem().build()); } this.player.openInventory(this.inventory); @@ -165,34 +162,48 @@ public class ExpiredMenu extends Holder { } case "Return" -> { - FileConfiguration data = Files.data.getConfiguration(); + if (Methods.isInvFull(player)) { // run this first obviously, just because + player.sendMessage(Messages.INVENTORY_FULL.getMessage(player)); - if (data.contains("OutOfTime/Cancelled")) { - for (String i : data.getConfigurationSection("OutOfTime/Cancelled").getKeys(false)) { - if (data.getString("OutOfTime/Cancelled." + i + ".Seller").equalsIgnoreCase(player.getUniqueId().toString())) { - if (Methods.isInvFull(player)) { - player.sendMessage(Messages.INVENTORY_FULL.getMessage(player)); - - break; - } else { - final ItemStack yoink = Methods.fromBase64(data.getString("OutOfTime/Cancelled." + i + ".Item", "")); - - if (yoink != null) { - player.getInventory().addItem(yoink); - - data.set("OutOfTime/Cancelled." + i, null); - } else { - this.plugin.getLogger().warning("The player " + player.getName() + " tried to redeem an invalid item in the expired menu."); - } - } - } - } + return; } - player.sendMessage(Messages.GOT_ITEM_BACK.getMessage(player)); + final FileConfiguration data = Files.data.getConfiguration(); + + final ConfigurationSection section = data.getConfigurationSection("expired_auctions"); + + if (section == null) return; + + final ConfigurationSection player_section = section.getConfigurationSection(player.getUniqueId().toString()); + + if (player_section == null) return; + + final Inventory player_inventory = player.getInventory(); + + for (final String key : section.getKeys(false)) { + if (Methods.isInvFull(player)) { // run this here obviously as well + player.sendMessage(Messages.INVENTORY_FULL.getMessage(player)); + + break; + } + + final ConfigurationSection auction_section = section.getConfigurationSection(key); + + if (auction_section == null) continue; + + final ItemStack auction_item = Methods.fromBase64(auction_section.getString("item")); + + if (auction_item == null) continue; + + player_inventory.addItem(auction_item); + + player_section.set(key, null); + } Files.data.save(); + player.sendMessage(Messages.GOT_ITEM_BACK.getMessage(player)); + menu.click(player); GuiManager.openPlayersExpiredList(player, menu.getPage()); @@ -201,99 +212,24 @@ public class ExpiredMenu extends Holder { } } - String id = container.getOrDefault(Keys.auction_item.getNamespacedKey(), PersistentDataType.STRING, ""); - - if (id.isEmpty()) return; - - if (Methods.isInvFull(player)) { + if (Methods.isInvFull(player)) { // run this here obviously as well player.sendMessage(Messages.INVENTORY_FULL.getMessage(player)); return; } - final FileConfiguration data = Files.data.getConfiguration(); + final UUID uuid = player.getUniqueId(); - final ConfigurationSection section = data.getConfigurationSection("OutOfTime/Cancelled"); - - if (section == null) return; - - final ConfigurationSection auction = section.getConfigurationSection(id); + final ExpiredItem auction = this.userManager.getExpiredItemById(uuid, container.getOrDefault(Keys.auction_store_id.getNamespacedKey(), PersistentDataType.STRING, "")); if (auction == null) return; - final ItemStack yoink = Methods.fromBase64(auction.getString("Item")); + player.getInventory().addItem(auction.asItemStack()); - if (yoink != null) { - player.sendMessage(Messages.GOT_ITEM_BACK.getMessage(player)); + Files.data.save(); - player.getInventory().addItem(yoink); + menu.click(player); - data.set("OutOfTime/Cancelled." + id, null); - - Files.data.save(); - - menu.click(player); - - GuiManager.openPlayersExpiredList(player, 1); - } else { - menu.click(player); - - GuiManager.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1); - - player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(player)); - } - } - - private void getItems() { - final ConfigurationSection section = this.data.getConfigurationSection("OutOfTime/Cancelled"); - - 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", "expired_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 lore = new ArrayList<>(itemBuilder.getUpdatedLore()); - - lore.add(" "); - - for (final String line : this.config.getStringList("Settings.GUISettings.Cancelled/ExpiredLore")) { - 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()); - } + GuiManager.openPlayersExpiredList(player, menu.getPage()); } } \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/other/BuyingMenu.java b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/other/BuyingMenu.java index 9c974ef..15b223d 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/other/BuyingMenu.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/guis/types/other/BuyingMenu.java @@ -116,7 +116,7 @@ public class BuyingMenu extends Holder { String id = this.data.getString("Items." + this.id + ".Name", "None"); - for (String l : config.getStringList("Settings.GUISettings.SellingItemLore")) { + for (String l : this.config.getStringList("Settings.GUISettings.SellingItemLore")) { lore.add(l.replace("%Price%", price).replace("%price%", price) .replace("%Seller%", id) .replace("%seller%", id) @@ -126,8 +126,8 @@ public class BuyingMenu extends Holder { itemBuilder.setLore(lore); - itemBuilder.addInteger(this.data.getInt("Items." + this.id + ".StoreID"), Keys.auction_id.getNamespacedKey()); - itemBuilder.addString(this.id, Keys.auction_item.getNamespacedKey()); + itemBuilder.addInteger(this.data.getInt("Items." + this.id + ".StoreID"), Keys.auction_store_id.getNamespacedKey()); + itemBuilder.addString(this.id, Keys.auction_number.getNamespacedKey()); this.inventory.setItem(4, itemBuilder.build()); 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 cd3a783..647ea5a 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/commands/AuctionCommand.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/commands/AuctionCommand.java @@ -10,6 +10,7 @@ import com.badbones69.crazyauctions.api.enums.ShopType; import com.badbones69.crazyauctions.api.events.AuctionListEvent; import com.badbones69.crazyauctions.api.GuiManager; import com.badbones69.crazyauctions.tasks.InventoryManager; +import com.badbones69.crazyauctions.tasks.UserManager; import com.ryderbelserion.vital.paper.api.files.FileManager; import org.bukkit.Material; import org.bukkit.command.Command; @@ -31,6 +32,8 @@ public class AuctionCommand implements CommandExecutor { private final CrazyManager crazyManager = this.plugin.getCrazyManager(); + private final UserManager userManager = this.plugin.getUserManager(); + private final FileManager fileManager = this.plugin.getFileManager(); @Override @@ -89,7 +92,10 @@ public class AuctionCommand implements CommandExecutor { this.fileManager.reloadFiles().init(); // update it again! - this.plugin.getUserManager().updateAuctionsCache(); + this.userManager.updateAuctionsCache(); + + // we want to update this cache, after the cache above... because we will also calculate if items are expired! + this.userManager.updateExpiredCache(); //todo() close inventories by tracking viewers, so the cache can be updated than re-open their inventories //todo() we need to track the specific inventory they opened, and if it's for them or another player @@ -336,7 +342,7 @@ public class AuctionCommand implements CommandExecutor { ItemStack stack = item.clone(); stack.setAmount(amount); - this.plugin.getUserManager().addAuction(player, stack, price, args[0].equalsIgnoreCase("bid")); + this.userManager.addAuction(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 a413164..d14f038 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/tasks/UserManager.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/tasks/UserManager.java @@ -2,8 +2,11 @@ package com.badbones69.crazyauctions.tasks; import com.badbones69.crazyauctions.CrazyAuctions; import com.badbones69.crazyauctions.Methods; +import com.badbones69.crazyauctions.api.enums.Reasons; import com.badbones69.crazyauctions.api.enums.misc.Files; -import com.badbones69.crazyauctions.tasks.objects.Auction; +import com.badbones69.crazyauctions.api.events.AuctionCancelledEvent; +import com.badbones69.crazyauctions.tasks.objects.AuctionItem; +import com.badbones69.crazyauctions.tasks.objects.ExpiredItem; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; @@ -18,7 +21,9 @@ public class UserManager { private final CrazyAuctions plugin = CrazyAuctions.getPlugin(); - private final Map> auctions = new HashMap<>(); + private final Map> auctions = new HashMap<>(); + + private final Map> expired_items = new HashMap<>(); public void addAuction(final Player player, final ItemStack itemStack, final long price, final boolean isBiddable) { final FileConfiguration data = Files.data.getConfiguration(); @@ -51,6 +56,18 @@ public class UserManager { addItem(itemStack, uuid, price, isBiddable, config, section); } + public void removeAuction(final AuctionItem auction) { + if (!this.auctions.containsKey(auction.getUuid())) { + return; + } + + final List auctions = this.auctions.get(auction.getUuid()); + + auctions.remove(auction); + + this.auctions.put(auction.getUuid(), auctions); + } + public final void updateAuctionsCache() { this.auctions.clear(); @@ -70,19 +87,43 @@ public class UserManager { if (auction == null) continue; - addCache(key, auction); + addActiveAuction(key, auction); } } } - public final Map> getAuctions() { + public final void updateExpiredCache() { + this.expired_items.clear(); + + final FileConfiguration data = Files.data.getConfiguration(); + + final ConfigurationSection section = data.getConfigurationSection("expired_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; + + addExpiredAuction(key, auction); + } + } + } + + public final Map> getAuctions() { return this.auctions; } - public final Auction getAuctionById(final UUID uuid, final String store_id) { - Auction key = null; + public final AuctionItem getAuctionById(final UUID uuid, final String store_id) { + AuctionItem key = null; - for (final Auction auction : this.auctions.get(uuid)) { + for (final AuctionItem auction : this.auctions.get(uuid)) { if (!auction.getStoreID().equals(store_id)) continue; key = auction; @@ -93,6 +134,80 @@ public class UserManager { 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); + + final UUID uuid = player.getUniqueId(); + + final FileConfiguration data = Files.data.getConfiguration(); + + int number = 1; + + for (;data.contains("expired_auctions." + uuid + "." + number); number++); + + data.set("expired_auctions." + uuid + "." + number + ".name", auction.getName()); + + final ConfigurationSection section = data.getConfigurationSection("expired_auctions." + uuid + "." + number); + + if (section == null) return; + + section.set("item", auction.asBase64()); + section.set("store_id", auction.getStoreID()); + section.set("full_time", auction.getFullExpire()); + section.set("price", auction.getPrice()); + + data.set("active_auctions." + uuid+ "." + auction.getId(), null); + + if (!data.contains("active_auctions." + uuid + "." + auction.getId())) { + removeAuction(auction); + } + + Files.data.save(); + + final ExpiredItem expiredItem = new ExpiredItem( + auction.getUuid().toString(), + auction.getName(), + String.valueOf(number), + auction.asBase64(), + auction.getStoreID(), + auction.getFullExpire(), + auction.getPrice() + ); + + if (this.expired_items.containsKey(uuid)) { + final List items = this.expired_items.get(uuid); + + items.add(expiredItem); + + this.expired_items.put(uuid, items); + + return; + } + + this.expired_items.put(uuid, new ArrayList<>() {{ + add(expiredItem); + }}); + } + + public final ExpiredItem getExpiredItemById(final UUID uuid, final String store_id) { + ExpiredItem key = null; + + for (final ExpiredItem auction : this.expired_items.get(uuid)) { + if (!auction.getStoreID().equals(store_id)) continue; + + key = auction; + + break; + } + + return key; + } + + public final Map> getExpiredItems() { + return this.expired_items; + } + /** * Adds an item to the configuration section * @@ -119,13 +234,39 @@ public class UserManager { section.set("status.top_bidder.name", "None"); section.set("status.biddable", isBiddable); - addCache(uuid, section); + addActiveAuction(uuid, section); Files.data.save(); } - private void addCache(final String uuid, final ConfigurationSection section) { - final Auction new_auction = new Auction(uuid, section.getString("name"), + private void addExpiredAuction(final String uuid, final ConfigurationSection section) { + final ExpiredItem expired_item = new ExpiredItem( + uuid, + section.getString("name"), + section.getName(), + section.getString("item"), + section.getString("store_id"), + section.getLong("full_time"), + section.getLong("price") + ); + + final UUID fromString = UUID.fromString(uuid); + + if (this.expired_items.containsKey(fromString)) { + final List auctions = this.expired_items.get(fromString); + + auctions.add(expired_item); + + this.expired_items.put(fromString, auctions); + } else { + this.expired_items.put(fromString, new ArrayList<>() {{ + add(expired_item); + }}); + } + } + + private void addActiveAuction(final String uuid, final ConfigurationSection section) { + final AuctionItem new_auction = new AuctionItem(uuid, section.getString("name"), section.getName(), section.getString("item"), section.getString("store_id"), @@ -140,7 +281,7 @@ public class UserManager { final UUID fromString = UUID.fromString(uuid); if (this.auctions.containsKey(fromString)) { - final List auctions = this.auctions.get(fromString); + final List auctions = this.auctions.get(fromString); auctions.add(new_auction); diff --git a/paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/Auction.java b/paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/AuctionItem.java similarity index 89% rename from paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/Auction.java rename to paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/AuctionItem.java index 86a910d..e2cae13 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/Auction.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/AuctionItem.java @@ -13,7 +13,7 @@ import java.util.Locale; import java.util.UUID; @SuppressWarnings("UnusedAssignment") -public class Auction { +public class AuctionItem { private final UUID uuid; private final String name; @@ -30,7 +30,7 @@ public class Auction { private final String bidder_name; private final boolean isBiddable; - public Auction(final String uuid, final String name, final String id, final String item, final String store_id, final long price, final long time_till_expire, final long full_expire, final String bidder_uuid, final String bidder_name, final boolean biddable) { + public AuctionItem(final String uuid, final String name, final String id, final String item, final String store_id, final long price, final long time_till_expire, final long full_expire, final String bidder_uuid, final String bidder_name, final boolean biddable) { this.uuid = UUID.fromString(uuid); this.name = name; this.id = id; @@ -88,7 +88,15 @@ public class Auction { return this.isBiddable; } - public final ItemBuilder getItemBuilder(final ShopType shopType) { + public final ItemStack asItemStack() { + return this.itemStack; + } + + public final String asBase64() { + return Methods.toBase64(asItemStack()); + } + + public final ItemBuilder getActiveItem(final ShopType shopType) { final FileConfiguration configuration = Files.config.getConfiguration(); final ItemBuilder itemBuilder = ItemBuilder.convertItemStack(this.itemStack.clone()); @@ -135,7 +143,7 @@ public class Auction { return itemBuilder; } - public final ItemBuilder getItemBuilder() { + public final ItemBuilder getCurrentItem() { final FileConfiguration configuration = Files.config.getConfiguration(); final ItemBuilder itemBuilder = ItemBuilder.convertItemStack(this.itemStack.clone()); diff --git a/paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/ExpiredItem.java b/paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/ExpiredItem.java new file mode 100644 index 0000000..b3935bc --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/tasks/objects/ExpiredItem.java @@ -0,0 +1,95 @@ +package com.badbones69.crazyauctions.tasks.objects; + +import com.badbones69.crazyauctions.Methods; +import com.badbones69.crazyauctions.api.builders.ItemBuilder; +import com.badbones69.crazyauctions.api.enums.misc.Files; +import com.badbones69.crazyauctions.api.enums.misc.Keys; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.UUID; + +public class ExpiredItem { + + private final UUID uuid; + private final String name; + + private final String id; + + private final String store_id; + private final ItemStack itemStack; + private final long full_expire; + private final long price; + + public ExpiredItem(final String uuid, final String name, final String id, final String item, final String store_id, final long full_expire, final long price) { + this.uuid = UUID.fromString(uuid); + this.name = name; + this.id = id; + + this.itemStack = Methods.fromBase64(item); + + this.store_id = store_id; + this.full_expire = full_expire; + + this.price = price; + } + + public final UUID getUuid() { + return this.uuid; + } + + public final String getId() { + return this.id; + } + + public final String getName() { + return this.name; + } + + public final String getStoreID() { + return this.store_id; + } + + public final long getFullExpire() { + return this.full_expire; + } + + public final ItemStack asItemStack() { + return this.itemStack; + } + + public final long getPrice() { + return this.price; + } + + public final String asBase64() { + return Methods.toBase64(asItemStack()); + } + + public final ItemBuilder getExpiredItem() { + 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(getFullExpire()); + + final List lore = new ArrayList<>(itemBuilder.getUpdatedLore()); + + lore.add(" "); + + for (final String line : configuration.getStringList("Settings.GUISettings.Cancelled/ExpiredLore")) { + 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; + } +} \ No newline at end of file