From a3ed1058e65c9f3265869150656ba04362c97fd4 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 27 Jan 2022 22:20:38 +0100 Subject: [PATCH] Improves the double click prevention by accounting for heavy concurrent usage and server lag --- README.md | 1 + .../listener/PlayerEventListener.java | 24 +++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1e0b6a6..ce8d504 100644 --- a/README.md +++ b/README.md @@ -401,6 +401,7 @@ portalInfoServer=Server: %server% - Adds a config option to set the exit velocity of any players exiting a stargate - Adjusts vehicle teleportation a bit to prevent passengers' exit rotation from being wrong +- Improves the checking for buggy double-clicks on non-button blocks #### \[Version 0.9.3.1] EpicKnarvik97 fork diff --git a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java index b182690..8261673 100644 --- a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java @@ -34,14 +34,16 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; +import java.util.HashMap; +import java.util.Map; + /** * This listener listens to any player-related events related to stargates */ @SuppressWarnings("unused") public class PlayerEventListener implements Listener { - private static long eventTime; - private static PlayerInteractEvent previousEvent; + private static final Map previousEventTimes = new HashMap<>(); /** * This event handler handles detection of any player teleporting through a bungee gate @@ -295,7 +297,7 @@ public class PlayerEventListener implements Listener { } //Prevent a double click caused by a Spigot bug - if (clickIsBug(event, block)) { + if (clickIsBug(event.getPlayer(), block)) { return; } @@ -366,19 +368,17 @@ public class PlayerEventListener implements Listener { * immediately, or causing portal information printing twice. This fix should detect the bug without breaking * clicking once the bug is fixed.

* - * @param event

The event causing the right click

- * @param block

The block to check

+ * @param player

The player performing the right-click

+ * @param block

The block to check

* @return

True if the click is a bug and should be cancelled

*/ - private boolean clickIsBug(PlayerInteractEvent event, Block block) { - if (previousEvent != null && - event.getPlayer() == previousEvent.getPlayer() && eventTime + 15 > System.currentTimeMillis()) { - previousEvent = null; - eventTime = 0; + private boolean clickIsBug(Player player, Block block) { + Long previousEventTime = previousEventTimes.get(player); + if (previousEventTime != null && previousEventTime + 50 > System.currentTimeMillis()) { + previousEventTimes.put(player, null); return true; } - previousEvent = event; - eventTime = System.currentTimeMillis(); + previousEventTimes.put(player, System.currentTimeMillis()); return false; }