From f97007e428e06b58b80142b4683cc0d5e5d2a6de Mon Sep 17 00:00:00 2001 From: Kiran Hart Date: Fri, 17 Feb 2023 07:36:06 -0500 Subject: [PATCH] add processing state, prevent player from dropping item or switching hotbar slots while listing is in process Took 32 seconds --- .../auctionhouse/commands/CommandSell.java | 16 ++++++++++-- .../listeners/PlayerListeners.java | 26 ++++++++++++++++--- .../managers/AuctionPlayerManager.java | 21 +++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/main/java/ca/tweetzy/auctionhouse/commands/CommandSell.java b/src/main/java/ca/tweetzy/auctionhouse/commands/CommandSell.java index 06105ff..1284205 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/commands/CommandSell.java +++ b/src/main/java/ca/tweetzy/auctionhouse/commands/CommandSell.java @@ -73,6 +73,8 @@ public final class CommandSell extends AbstractCommand { } AuctionPlayer auctionPlayer = instance.getAuctionPlayerManager().getPlayer(player.getUniqueId()); + if (!Bukkit.getOfflinePlayer(player.getUniqueId()).isOnline()) + return ReturnType.FAILURE; ItemStack originalItem = PlayerHelper.getHeldItem(player).clone(); ItemStack itemToSell = PlayerHelper.getHeldItem(player).clone(); @@ -338,11 +340,13 @@ public final class CommandSell extends AbstractCommand { auctionedItem.setInfinite(isInfinite); auctionedItem.setAllowPartialBuy(partialBuy); - if (Settings.ASK_FOR_LISTING_CONFIRMATION.getBoolean()) { - player.getInventory().setItemInHand(CompMaterial.AIR.parseItem()); + AuctionHouse.getInstance().getAuctionPlayerManager().addToSellProcess(player); + if (Settings.ASK_FOR_LISTING_CONFIRMATION.getBoolean()) { instance.getGuiManager().showGUI(player, new GUIListingConfirm(player, auctionedItem, result -> { if (!result) { + AuctionHouse.getInstance().getAuctionPlayerManager().processSell(player); + player.closeInventory(); PlayerUtils.giveItem(player, auctionedItem.getItem()); auctionPlayer.setItemBeingListed(null); @@ -357,6 +361,8 @@ public final class CommandSell extends AbstractCommand { player.getInventory().setItemInHand(CompMaterial.AIR.parseItem()); AuctionCreator.create(auctionPlayer, auctionedItem, (auction, listingResult) -> { + AuctionHouse.getInstance().getAuctionPlayerManager().processSell(player); + if (listingResult != ListingResult.SUCCESS) { PlayerUtils.giveItem(player, auction.getItem()); auctionPlayer.setItemBeingListed(null); @@ -365,6 +371,8 @@ public final class CommandSell extends AbstractCommand { if (Settings.OPEN_MAIN_AUCTION_HOUSE_AFTER_MENU_LIST.getBoolean()) instance.getGuiManager().showGUI(player, new GUIAuctionHouse(auctionPlayer)); + else + AuctionHouse.newChain().sync(player::closeInventory).execute(); }); }, Settings.INTERNAL_CREATE_DELAY.getInt()); @@ -378,6 +386,8 @@ public final class CommandSell extends AbstractCommand { player.getInventory().setItemInHand(CompMaterial.AIR.parseItem()); AuctionCreator.create(auctionPlayer, auctionedItem, (auction, listingResult) -> { + AuctionHouse.getInstance().getAuctionPlayerManager().processSell(player); + if (listingResult != ListingResult.SUCCESS) { PlayerUtils.giveItem(player, auction.getItem()); auctionPlayer.setItemBeingListed(null); @@ -386,6 +396,8 @@ public final class CommandSell extends AbstractCommand { if (Settings.OPEN_MAIN_AUCTION_HOUSE_AFTER_MENU_LIST.getBoolean()) instance.getGuiManager().showGUI(player, new GUIAuctionHouse(auctionPlayer)); + else + AuctionHouse.newChain().sync(player::closeInventory).execute(); }); }, Settings.INTERNAL_CREATE_DELAY.getInt()); diff --git a/src/main/java/ca/tweetzy/auctionhouse/listeners/PlayerListeners.java b/src/main/java/ca/tweetzy/auctionhouse/listeners/PlayerListeners.java index b3acc9e..0202d8e 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/listeners/PlayerListeners.java +++ b/src/main/java/ca/tweetzy/auctionhouse/listeners/PlayerListeners.java @@ -44,9 +44,7 @@ import org.bukkit.event.block.Action; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.inventory.PrepareAnvilEvent; import org.bukkit.event.inventory.PrepareItemCraftEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.*; import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; @@ -65,6 +63,9 @@ public class PlayerListeners implements Listener { public void onPlayerDeath(PlayerDeathEvent event) { final Player player = event.getEntity(); final AuctionPlayer auctionPlayer = AuctionHouse.getInstance().getAuctionPlayerManager().getPlayer(player.getUniqueId()); + + AuctionHouse.getInstance().getAuctionPlayerManager().processSell(player); + if (auctionPlayer != null) { // task id cancel Bukkit.getServer().getScheduler().cancelTask(auctionPlayer.getAssignedTaskId()); @@ -81,7 +82,6 @@ public class PlayerListeners implements Listener { final AuctionHouse instance = AuctionHouse.getInstance(); Titles.sendTitle(player, 1, 1, 1, " ", " "); - instance.getAuctionPlayerManager().addPlayer(player); Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(AuctionHouse.getInstance(), () -> { @@ -97,6 +97,8 @@ public class PlayerListeners implements Listener { final Player player = e.getPlayer(); final AuctionHouse instance = AuctionHouse.getInstance(); + AuctionHouse.getInstance().getAuctionPlayerManager().processSell(player); + if (instance.getAuctionPlayerManager().getPlayer(player.getUniqueId()) != null && instance.getAuctionPlayerManager().getPlayer(player.getUniqueId()).getItemBeingListed() != null) { player.getInventory().addItem(instance.getAuctionPlayerManager().getPlayer(player.getUniqueId()).getItemBeingListed()); @@ -170,6 +172,22 @@ public class PlayerListeners implements Listener { PlayerUtils.giveItem(player, items); } + @EventHandler + public void onItemDropDuringSell(final PlayerDropItemEvent event) { + final Player player = event.getPlayer(); + + if (AuctionHouse.getInstance().getAuctionPlayerManager().isInSellProcess(player)) + event.setCancelled(true); + } + + @EventHandler + public void onHotbarSwapDuringSell(final PlayerItemHeldEvent event) { + final Player player = event.getPlayer(); + + if (AuctionHouse.getInstance().getAuctionPlayerManager().isInSellProcess(player)) + event.setCancelled(true); + } + @EventHandler public void onInventoryClick(PrepareAnvilEvent event) { ItemStack stack = event.getResult(); diff --git a/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionPlayerManager.java b/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionPlayerManager.java index a8699f1..c7af3d2 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionPlayerManager.java +++ b/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionPlayerManager.java @@ -24,6 +24,7 @@ import ca.tweetzy.auctionhouse.auction.enums.AuctionItemCategory; import ca.tweetzy.auctionhouse.auction.enums.AuctionSaleType; import ca.tweetzy.auctionhouse.settings.Settings; import lombok.Getter; +import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -48,6 +49,9 @@ public class AuctionPlayerManager { private final HashSet usingSellGUI = new HashSet<>(); private final HashMap cooldowns = new HashMap<>(); + @Getter + private final HashSet processingSell = new HashSet<>(); + public void addPlayer(AuctionPlayer auctionPlayer) { if (auctionPlayer == null) return; this.auctionPlayers.put(auctionPlayer.getUuid(), auctionPlayer); @@ -126,6 +130,23 @@ public class AuctionPlayerManager { return this.cooldowns; } + public void addToSellProcess(@NonNull final Player player) { + if (this.processingSell.contains(player.getUniqueId())) + return; + this.processingSell.add(player.getUniqueId()); + } + + public boolean isInSellProcess(@NonNull final Player player) { + return this.processingSell.contains(player.getUniqueId()); + } + + public void processSell(@NonNull final Player player) { + if (!this.processingSell.contains(player.getUniqueId())) + return; + this.processingSell.remove(player.getUniqueId()); + } + + public void loadPlayers() { this.auctionPlayers.clear();