add processing state, prevent player from dropping item or switching hotbar slots while listing is in process

Took 32 seconds
This commit is contained in:
Kiran Hart 2023-02-17 07:36:06 -05:00
parent 4cd6bc7ea5
commit f97007e428
No known key found for this signature in database
GPG Key ID: 5F36C7BC79D3EBC3
3 changed files with 57 additions and 6 deletions

View File

@ -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());

View File

@ -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();

View File

@ -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<UUID> usingSellGUI = new HashSet<>();
private final HashMap<UUID, Long> cooldowns = new HashMap<>();
@Getter
private final HashSet<UUID> 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();