diff --git a/pom.xml b/pom.xml index 5405283..716b3df 100644 --- a/pom.xml +++ b/pom.xml @@ -136,7 +136,7 @@ ca.tweetzy tweetycore - 2.1.2 + 2.2.2 com.github.MilkBowl diff --git a/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java b/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java index 4a28cad..142c755 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java +++ b/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java @@ -22,6 +22,7 @@ import ca.tweetzy.core.database.DataMigrationManager; import ca.tweetzy.core.database.DatabaseConnector; import ca.tweetzy.core.database.MySQLConnector; import ca.tweetzy.core.gui.GuiManager; +import ca.tweetzy.core.locale.Locale; import ca.tweetzy.core.utils.Metrics; import co.aikar.taskchain.BukkitTaskChainFactory; import co.aikar.taskchain.TaskChain; diff --git a/src/main/java/ca/tweetzy/auctionhouse/api/events/AuctionEndEvent.java b/src/main/java/ca/tweetzy/auctionhouse/api/events/AuctionEndEvent.java index b5a8e28..c83e689 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/api/events/AuctionEndEvent.java +++ b/src/main/java/ca/tweetzy/auctionhouse/api/events/AuctionEndEvent.java @@ -29,14 +29,18 @@ public class AuctionEndEvent extends Event implements Cancellable { private AuctionItem auctionItem; private AuctionSaleType saleType; - public AuctionEndEvent(OfflinePlayer originalOwner, OfflinePlayer buyer, AuctionItem auctionItem, AuctionSaleType saleType) { - super(true); + public AuctionEndEvent(OfflinePlayer originalOwner, OfflinePlayer buyer, AuctionItem auctionItem, AuctionSaleType saleType, boolean async) { + super(async); this.originalOwner = originalOwner; this.buyer = buyer; this.auctionItem = auctionItem; this.saleType = saleType; } + public AuctionEndEvent(OfflinePlayer originalOwner, OfflinePlayer buyer, AuctionItem auctionItem, AuctionSaleType saleType) { + this(originalOwner, buyer, auctionItem, saleType, true); + } + public HandlerList getHandlers() { return handlers; } diff --git a/src/main/java/ca/tweetzy/auctionhouse/auction/AuctionItem.java b/src/main/java/ca/tweetzy/auctionhouse/auction/AuctionItem.java index 0a433bd..41d83ea 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/auction/AuctionItem.java +++ b/src/main/java/ca/tweetzy/auctionhouse/auction/AuctionItem.java @@ -103,7 +103,7 @@ public class AuctionItem implements Serializable { lore.addAll(Settings.AUCTION_PURCHASE_CONTROL_HEADER.getStringList().stream().map(TextUtils::formatText).collect(Collectors.toList())); lore.addAll(this.bidStartPrice <= 0 || this.bidIncPrice <= 0 ? Settings.AUCTION_PURCHASE_CONTROLS_BID_OFF.getStringList().stream().map(TextUtils::formatText).collect(Collectors.toList()) : Settings.AUCTION_PURCHASE_CONTROLS_BID_ON.getStringList().stream().map(TextUtils::formatText).collect(Collectors.toList())); - if ((ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11) && itemStack.getType() == XMaterial.SHULKER_BOX.parseMaterial()) || (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_14) && itemStack.getType() == XMaterial.SHULKER_BOX.parseMaterial() || itemStack.getType() == XMaterial.BARREL.parseMaterial())) { + if ((ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11) && itemStack.getType().name().contains("SHULKER_BOX")) || (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_14) && itemStack.getType() == XMaterial.SHULKER_BOX.parseMaterial() || itemStack.getType() == XMaterial.BARREL.parseMaterial())) { lore.addAll(Settings.AUCTION_PURCHASE_CONTROLS_INSPECTION.getStringList().stream().map(TextUtils::formatText).collect(Collectors.toList())); } diff --git a/src/main/java/ca/tweetzy/auctionhouse/guis/GUIAuctionHouse.java b/src/main/java/ca/tweetzy/auctionhouse/guis/GUIAuctionHouse.java index 2268c0a..7a05483 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/guis/GUIAuctionHouse.java +++ b/src/main/java/ca/tweetzy/auctionhouse/guis/GUIAuctionHouse.java @@ -119,6 +119,7 @@ public class GUIAuctionHouse extends Gui { cleanup(); e.manager.showGUI(e.player, new GUIConfirmPurchase(this.auctionPlayer, auctionItem, buyingQuantity)); + AuctionHouse.getInstance().getTransactionManager().addPrePurchase(e.player, auctionItem.getKey()); } private void handleBidItem(AuctionItem auctionItem, GuiClickEvent e, boolean buyNow) { diff --git a/src/main/java/ca/tweetzy/auctionhouse/guis/GUIConfirmPurchase.java b/src/main/java/ca/tweetzy/auctionhouse/guis/GUIConfirmPurchase.java index 99102fe..2752423 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/guis/GUIConfirmPurchase.java +++ b/src/main/java/ca/tweetzy/auctionhouse/guis/GUIConfirmPurchase.java @@ -61,6 +61,12 @@ public class GUIConfirmPurchase extends Gui { this.pricePerItem = this.auctionItem.getBasePrice() / this.maxStackSize; } + setOnClose(close -> { + AuctionHouse.getInstance().getTransactionManager().getPrePurchaseHolding().remove(close.player); + close.manager.showGUI(close.player, new GUIAuctionHouse(this.auctionPlayer)); + AuctionHouse.getInstance().getLogger().info("Removed " + close.player.getName() + " from confirmation pre purchase"); + }); + draw(); } @@ -69,7 +75,7 @@ public class GUIConfirmPurchase extends Gui { setItem(this.buyingSpecificQuantity ? 1 : 0, 4, AuctionAPI.getInstance().deserializeItem(this.auctionItem.getRawItem())); setItems(this.buyingSpecificQuantity ? 14 : 5, this.buyingSpecificQuantity ? 17 : 8, new TItemBuilder(Objects.requireNonNull(Settings.GUI_CONFIRM_BUY_NO_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_CONFIRM_BUY_NO_NAME.getString()).setLore(Settings.GUI_CONFIRM_BUY_NO_LORE.getStringList()).toItemStack()); - setActionForRange(this.buyingSpecificQuantity ? 14 : 5, this.buyingSpecificQuantity ? 17 : 8, ClickType.LEFT, e -> e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer))); + setActionForRange(this.buyingSpecificQuantity ? 14 : 5, this.buyingSpecificQuantity ? 17 : 8, ClickType.LEFT, e -> e.gui.close()); setActionForRange(this.buyingSpecificQuantity ? 9 : 0, this.buyingSpecificQuantity ? 12 : 3, ClickType.LEFT, e -> { // Re-select the item to ensure that it's available AuctionItem located = AuctionHouse.getInstance().getAuctionItemManager().getItem(this.auctionItem.getKey()); @@ -79,47 +85,45 @@ public class GUIConfirmPurchase extends Gui { if (!AuctionHouse.getInstance().getEconomy().has(e.player, this.buyingSpecificQuantity ? this.purchaseQuantity * this.pricePerItem : located.getBasePrice())) { AuctionHouse.getInstance().getLocale().getMessage("general.notenoughmoney").sendPrefixedMessage(e.player); SoundManager.getInstance().playSound(e.player, Settings.SOUNDS_NOT_ENOUGH_MONEY.getString(), 1.0F, 1.0F); - e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer)); + e.gui.close(); return; } - Bukkit.getServer().getScheduler().runTaskAsynchronously(AuctionHouse.getInstance(), () -> { - AuctionEndEvent auctionEndEvent = new AuctionEndEvent(Bukkit.getOfflinePlayer(this.auctionItem.getOwner()), e.player, this.auctionItem, AuctionSaleType.WITHOUT_BIDDING_SYSTEM); - Bukkit.getServer().getPluginManager().callEvent(auctionEndEvent); + AuctionEndEvent auctionEndEvent = new AuctionEndEvent(Bukkit.getOfflinePlayer(this.auctionItem.getOwner()), e.player, this.auctionItem, AuctionSaleType.WITHOUT_BIDDING_SYSTEM, false); + Bukkit.getServer().getPluginManager().callEvent(auctionEndEvent); + if (auctionEndEvent.isCancelled()) return; - if (auctionEndEvent.isCancelled()) return; + if (!Settings.ALLOW_PURCHASE_IF_INVENTORY_FULL.getBoolean() && e.player.getInventory().firstEmpty() == -1) { + AuctionHouse.getInstance().getLocale().getMessage("general.noroom").sendPrefixedMessage(e.player); + return; + } - if (!Settings.ALLOW_PURCHASE_IF_INVENTORY_FULL.getBoolean() && e.player.getInventory().firstEmpty() == -1) { - AuctionHouse.getInstance().getLocale().getMessage("general.noroom").sendPrefixedMessage(e.player); - return; - } - - if (this.buyingSpecificQuantity) { - ItemStack item = AuctionAPI.getInstance().deserializeItem(located.getRawItem()); -// Bukkit.broadcastMessage(String.format("Total Item Qty: %d\nTotal Purchase Qty: %d\nAmount of purchase: %d", item.getAmount(), this.purchaseQuantity, item.getAmount() - this.purchaseQuantity)); - - if (item.getAmount() - this.purchaseQuantity >= 1) { - item.setAmount(item.getAmount() - this.purchaseQuantity); - located.setRawItem(AuctionAPI.getInstance().serializeItem(item)); - located.setBasePrice(located.getBasePrice() - this.purchaseQuantity * this.pricePerItem); - item.setAmount(this.purchaseQuantity); - transferFunds(e.player, this.purchaseQuantity * this.pricePerItem); - } else { - transferFunds(e.player, located.getBasePrice()); - AuctionHouse.getInstance().getAuctionItemManager().removeItem(located.getKey()); - } - - givePlayerItem(e.player, item); - sendMessages(e, located, true, this.purchaseQuantity * this.pricePerItem); - e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer)); - return; + if (this.buyingSpecificQuantity) { + ItemStack item = AuctionAPI.getInstance().deserializeItem(located.getRawItem()); + + if (item.getAmount() - this.purchaseQuantity >= 1) { + item.setAmount(item.getAmount() - this.purchaseQuantity); + located.setRawItem(AuctionAPI.getInstance().serializeItem(item)); + located.setBasePrice(located.getBasePrice() - this.purchaseQuantity * this.pricePerItem); + item.setAmount(this.purchaseQuantity); + transferFunds(e.player, this.purchaseQuantity * this.pricePerItem); + } else { + transferFunds(e.player, located.getBasePrice()); + AuctionHouse.getInstance().getAuctionItemManager().removeItem(located.getKey()); } + givePlayerItem(e.player, item); + sendMessages(e, located, true, this.purchaseQuantity * this.pricePerItem); + } else { transferFunds(e.player, located.getBasePrice()); AuctionHouse.getInstance().getAuctionItemManager().removeItem(located.getKey()); givePlayerItem(e.player, AuctionAPI.getInstance().deserializeItem(located.getRawItem())); sendMessages(e, located, false, 0); - e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer)); + } + + AuctionHouse.getInstance().getTransactionManager().getPrePurchasePlayers(auctionItem.getKey()).forEach(player -> { + AuctionHouse.getInstance().getTransactionManager().removeAllRelatedPlayers(auctionItem.getKey()); + player.closeInventory(); }); }); diff --git a/src/main/java/ca/tweetzy/auctionhouse/guis/transaction/GUITransactionList.java b/src/main/java/ca/tweetzy/auctionhouse/guis/transaction/GUITransactionList.java index 069b1dc..638af28 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/guis/transaction/GUITransactionList.java +++ b/src/main/java/ca/tweetzy/auctionhouse/guis/transaction/GUITransactionList.java @@ -44,43 +44,42 @@ public class GUITransactionList extends Gui { private void draw() { reset(); - AuctionHouse.newChain().async(() -> { - pages = (int) Math.max(1, Math.ceil(this.transactions.size() / (double) 45)); - setPrevPage(5, 3, new TItemBuilder(Objects.requireNonNull(Settings.GUI_BACK_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_BACK_BTN_NAME.getString()).setLore(Settings.GUI_BACK_BTN_LORE.getStringList()).toItemStack()); - setButton(5, 4, new TItemBuilder(Objects.requireNonNull(Settings.GUI_REFRESH_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_REFRESH_BTN_NAME.getString()).setLore(Settings.GUI_REFRESH_BTN_LORE.getStringList()).toItemStack(), e -> e.manager.showGUI(e.player, new GUITransactionList(this.auctionPlayer))); - setNextPage(5, 5, new TItemBuilder(Objects.requireNonNull(Settings.GUI_NEXT_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_NEXT_BTN_NAME.getString()).setLore(Settings.GUI_NEXT_BTN_LORE.getStringList()).toItemStack()); - setOnPage(e -> { - draw(); - SoundManager.getInstance().playSound(this.auctionPlayer.getPlayer(), Settings.SOUNDS_NAVIGATE_GUI_PAGES.getString(), 1.0F, 1.0F); - }); - // Other Buttons - setButton(5, 0, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_CLOSE_BTN_ITEM.getString(), Settings.GUI_CLOSE_BTN_NAME.getString(), Settings.GUI_CLOSE_BTN_LORE.getStringList(), null), e -> e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer))); - setButton(5, 1, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_TRANSACTIONS_ITEM_TOGGLE_OWN_ITEM.getString(), Settings.GUI_TRANSACTIONS_ITEM_TOGGLE_OWN_NAME.getString(), Settings.GUI_TRANSACTIONS_ITEM_TOGGLE_OWN_LORE.getStringList(), null), e -> { - this.showOwnOnly = !this.showOwnOnly; - draw(); - }); + pages = (int) Math.max(1, Math.ceil(this.transactions.size() / (double) 45)); + setPrevPage(5, 3, new TItemBuilder(Objects.requireNonNull(Settings.GUI_BACK_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_BACK_BTN_NAME.getString()).setLore(Settings.GUI_BACK_BTN_LORE.getStringList()).toItemStack()); + setButton(5, 4, new TItemBuilder(Objects.requireNonNull(Settings.GUI_REFRESH_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_REFRESH_BTN_NAME.getString()).setLore(Settings.GUI_REFRESH_BTN_LORE.getStringList()).toItemStack(), e -> e.manager.showGUI(e.player, new GUITransactionList(this.auctionPlayer))); + setNextPage(5, 5, new TItemBuilder(Objects.requireNonNull(Settings.GUI_NEXT_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_NEXT_BTN_NAME.getString()).setLore(Settings.GUI_NEXT_BTN_LORE.getStringList()).toItemStack()); + setOnPage(e -> { + draw(); + SoundManager.getInstance().playSound(this.auctionPlayer.getPlayer(), Settings.SOUNDS_NAVIGATE_GUI_PAGES.getString(), 1.0F, 1.0F); + }); - if (this.showOwnOnly) { - highlightItem(5, 1); - } else { - removeHighlight(5, 1); - } + // Other Buttons + setButton(5, 0, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_CLOSE_BTN_ITEM.getString(), Settings.GUI_CLOSE_BTN_NAME.getString(), Settings.GUI_CLOSE_BTN_LORE.getStringList(), null), e -> e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer))); + setButton(5, 1, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_TRANSACTIONS_ITEM_TOGGLE_OWN_ITEM.getString(), Settings.GUI_TRANSACTIONS_ITEM_TOGGLE_OWN_NAME.getString(), Settings.GUI_TRANSACTIONS_ITEM_TOGGLE_OWN_LORE.getStringList(), null), e -> { + this.showOwnOnly = !this.showOwnOnly; + draw(); + }); - int slot = 0; - List data = this.transactions.stream().sorted(Comparator.comparingLong(Transaction::getTransactionTime).reversed()).skip((page - 1) * 45L).limit(45).collect(Collectors.toList()); - if (this.showOwnOnly) - data = data.stream().filter(transaction -> transaction.getSeller().equals(this.auctionPlayer.getPlayer().getUniqueId()) || transaction.getBuyer().equals(this.auctionPlayer.getPlayer().getUniqueId())).collect(Collectors.toList()); + if (this.showOwnOnly) { + highlightItem(5, 1); + } else { + removeHighlight(5, 1); + } - for (Transaction transaction : data) { - setButton(slot++, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_TRANSACTIONS_ITEM_TRANSACTION_ITEM.getString(), Settings.GUI_TRANSACTIONS_ITEM_TRANSACTION_NAME.getString(), Settings.GUI_TRANSACTIONS_ITEM_TRANSACTION_LORE.getStringList(), new HashMap() {{ - put("%transaction_id%", transaction.getId().toString()); - put("%seller%", Bukkit.getOfflinePlayer(transaction.getSeller()).getName()); - put("%buyer%", Bukkit.getOfflinePlayer(transaction.getBuyer()).getName()); - put("%date%", AuctionAPI.getInstance().convertMillisToDate(transaction.getTransactionTime())); - put("%item_name%", AuctionAPI.getInstance().getItemName(AuctionAPI.getInstance().deserializeItem(transaction.getAuctionItem().getRawItem()))); - }}), e -> e.manager.showGUI(e.player, new GUITransactionView(this.auctionPlayer, transaction))); - } - }).execute(); + int slot = 0; + List data = this.transactions.stream().sorted(Comparator.comparingLong(Transaction::getTransactionTime).reversed()).skip((page - 1) * 45L).limit(45).collect(Collectors.toList()); + if (this.showOwnOnly) + data = data.stream().filter(transaction -> transaction.getSeller().equals(this.auctionPlayer.getPlayer().getUniqueId()) || transaction.getBuyer().equals(this.auctionPlayer.getPlayer().getUniqueId())).collect(Collectors.toList()); + + for (Transaction transaction : data) { + setButton(slot++, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_TRANSACTIONS_ITEM_TRANSACTION_ITEM.getString(), Settings.GUI_TRANSACTIONS_ITEM_TRANSACTION_NAME.getString(), Settings.GUI_TRANSACTIONS_ITEM_TRANSACTION_LORE.getStringList(), new HashMap() {{ + put("%transaction_id%", transaction.getId().toString()); + put("%seller%", Bukkit.getOfflinePlayer(transaction.getSeller()).getName()); + put("%buyer%", Bukkit.getOfflinePlayer(transaction.getBuyer()).getName()); + put("%date%", AuctionAPI.getInstance().convertMillisToDate(transaction.getTransactionTime())); + put("%item_name%", AuctionAPI.getInstance().getItemName(AuctionAPI.getInstance().deserializeItem(transaction.getAuctionItem().getRawItem()))); + }}), e -> e.manager.showGUI(e.player, new GUITransactionView(this.auctionPlayer, transaction))); + } } } diff --git a/src/main/java/ca/tweetzy/auctionhouse/helpers/PlayerHelper.java b/src/main/java/ca/tweetzy/auctionhouse/helpers/PlayerHelper.java index 997b78b..3866cbd 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/helpers/PlayerHelper.java +++ b/src/main/java/ca/tweetzy/auctionhouse/helpers/PlayerHelper.java @@ -1,6 +1,8 @@ package ca.tweetzy.auctionhouse.helpers; +import ca.tweetzy.auctionhouse.AuctionHouse; import ca.tweetzy.core.compatibility.ServerVersion; +import ca.tweetzy.core.utils.TextUtils; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; diff --git a/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionItemManager.java b/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionItemManager.java index b9dd91d..af4cfdb 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionItemManager.java +++ b/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionItemManager.java @@ -21,7 +21,6 @@ import java.util.stream.Collectors; * Usage of any code found within this class is prohibited unless given explicit permission otherwise */ - public class AuctionItemManager { private final ArrayList auctionItems = new ArrayList<>(); @@ -40,7 +39,7 @@ public class AuctionItemManager { } public List getAuctionItems() { - return Collections.unmodifiableList(auctionItems); + return this.auctionItems; } public List getFilteredItems(AuctionItemCategory category) { diff --git a/src/main/java/ca/tweetzy/auctionhouse/managers/TransactionManager.java b/src/main/java/ca/tweetzy/auctionhouse/managers/TransactionManager.java index a57dca0..83a15e5 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/managers/TransactionManager.java +++ b/src/main/java/ca/tweetzy/auctionhouse/managers/TransactionManager.java @@ -6,11 +6,9 @@ import ca.tweetzy.auctionhouse.settings.Settings; import ca.tweetzy.auctionhouse.transaction.Transaction; import ca.tweetzy.core.utils.TextUtils; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; /** @@ -22,6 +20,7 @@ import java.util.stream.Collectors; public class TransactionManager { private final ArrayList transactions = new ArrayList<>(); + private final HashMap prePurchaseHolding = new HashMap<>(); public void addTransaction(Transaction transaction) { if (transaction == null) return; @@ -40,6 +39,22 @@ public class TransactionManager { return Collections.unmodifiableList(this.transactions); } + public void addPrePurchase(Player player, UUID uuid) { + this.prePurchaseHolding.put(player, uuid); + } + + public void removeAllRelatedPlayers(UUID uuid) { + this.prePurchaseHolding.keySet().removeIf(p -> this.prePurchaseHolding.get(p).equals(uuid)); + } + + public HashMap getPrePurchaseHolding() { + return prePurchaseHolding; + } + + public List getPrePurchasePlayers(UUID uuid) { + return this.prePurchaseHolding.keySet().stream().filter(p -> this.prePurchaseHolding.get(p).equals(uuid)).collect(Collectors.toList()); + } + public void loadTransactions(boolean useDatabase) { if (useDatabase) { AuctionHouse.getInstance().getDataManager().getTransactions(all -> all.forEach(this::addTransaction));