From c537c3d443758c0f8199f7973a25ccabe24dd395 Mon Sep 17 00:00:00 2001 From: xGinko Date: Thu, 8 Aug 2024 19:11:21 +0200 Subject: [PATCH] simplify code --- .../aef/modules/combat/SilentSwapDelay.java | 54 +++++-------------- .../aef/modules/combat/SilentSwapDelay.java | 54 +++++-------------- 2 files changed, 26 insertions(+), 82 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/combat/SilentSwapDelay.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/combat/SilentSwapDelay.java index 419fb121..4ce6282d 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/combat/SilentSwapDelay.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/combat/SilentSwapDelay.java @@ -6,7 +6,6 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; @@ -21,13 +20,13 @@ public class SilentSwapDelay extends AEFModule implements Listener { - private final Map playerDataMap; + private final Map swapItemResumeTimes; private final long swapDelayMillis; public SilentSwapDelay() { super("combat.crystal-aura.silent-swap-delay"); - this.playerDataMap = new ConcurrentHashMap<>(); - this.swapDelayMillis = config.getLong(configPath + ".min-swap-delay-millis", 50L, + this.swapItemResumeTimes = new ConcurrentHashMap<>(); + this.swapDelayMillis = config.getLong(configPath + ".min-swap-delay-millis", 40L, "The delay in millis a player cant swap hotbar items after placing\n" + "a block, clicking a block (for example to place a crystal) or\n" + "damaging an entity. (50 ms = 1 tick)"); @@ -50,19 +49,7 @@ public void disable() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot bar item selection changes - PlayerData playerData = playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new); - - if (playerData.lastAttackEntityTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) { - event.setCancelled(true); - return; - } - - if (playerData.lastInteractBlockTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) { - event.setCancelled(true); - return; - } - - if (playerData.lastPlaceBlockTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) { + if (swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()).get() > System.currentTimeMillis()) { event.setCancelled(true); } } @@ -71,44 +58,29 @@ private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot b private void onEntityDamageByEntity(EntityDamageByEntityEvent event) { if (event.getDamager().getType() != XEntityType.PLAYER.get()) return; - playerDataMap.computeIfAbsent(event.getDamager().getUniqueId(), PlayerData::new) - .lastAttackEntityTimeMillis.set(System.currentTimeMillis()); + swapItemResumeTimes.computeIfAbsent(event.getDamager().getUniqueId(), k -> new AtomicLong()) + .set(System.currentTimeMillis() + swapDelayMillis); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onPlayerInteract(PlayerInteractEvent event) { - if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; - - playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new) - .lastInteractBlockTimeMillis.set(System.currentTimeMillis()); + swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()) + .set(System.currentTimeMillis() + swapDelayMillis); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onBlockPlace(BlockPlaceEvent event) { - playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new) - .lastPlaceBlockTimeMillis.set(System.currentTimeMillis()); + swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()) + .set(System.currentTimeMillis() + swapDelayMillis); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onPlayerQuit(PlayerQuitEvent event) { - playerDataMap.remove(event.getPlayer().getUniqueId()); + swapItemResumeTimes.remove(event.getPlayer().getUniqueId()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - private void onPlayerKicked(PlayerKickEvent event) { - playerDataMap.remove(event.getPlayer().getUniqueId()); - } - - private static class PlayerData { - - public final UUID uuid; - public final AtomicLong lastInteractBlockTimeMillis, lastAttackEntityTimeMillis, lastPlaceBlockTimeMillis; - - public PlayerData(UUID uuid) { - this.uuid = uuid; - this.lastInteractBlockTimeMillis = new AtomicLong(); - this.lastAttackEntityTimeMillis = new AtomicLong(); - this.lastPlaceBlockTimeMillis = new AtomicLong(); - } + private void onPlayerKick(PlayerKickEvent event) { + swapItemResumeTimes.remove(event.getPlayer().getUniqueId()); } } \ No newline at end of file diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/combat/SilentSwapDelay.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/combat/SilentSwapDelay.java index 419fb121..4ce6282d 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/combat/SilentSwapDelay.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/combat/SilentSwapDelay.java @@ -6,7 +6,6 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; @@ -21,13 +20,13 @@ public class SilentSwapDelay extends AEFModule implements Listener { - private final Map playerDataMap; + private final Map swapItemResumeTimes; private final long swapDelayMillis; public SilentSwapDelay() { super("combat.crystal-aura.silent-swap-delay"); - this.playerDataMap = new ConcurrentHashMap<>(); - this.swapDelayMillis = config.getLong(configPath + ".min-swap-delay-millis", 50L, + this.swapItemResumeTimes = new ConcurrentHashMap<>(); + this.swapDelayMillis = config.getLong(configPath + ".min-swap-delay-millis", 40L, "The delay in millis a player cant swap hotbar items after placing\n" + "a block, clicking a block (for example to place a crystal) or\n" + "damaging an entity. (50 ms = 1 tick)"); @@ -50,19 +49,7 @@ public void disable() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot bar item selection changes - PlayerData playerData = playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new); - - if (playerData.lastAttackEntityTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) { - event.setCancelled(true); - return; - } - - if (playerData.lastInteractBlockTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) { - event.setCancelled(true); - return; - } - - if (playerData.lastPlaceBlockTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) { + if (swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()).get() > System.currentTimeMillis()) { event.setCancelled(true); } } @@ -71,44 +58,29 @@ private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot b private void onEntityDamageByEntity(EntityDamageByEntityEvent event) { if (event.getDamager().getType() != XEntityType.PLAYER.get()) return; - playerDataMap.computeIfAbsent(event.getDamager().getUniqueId(), PlayerData::new) - .lastAttackEntityTimeMillis.set(System.currentTimeMillis()); + swapItemResumeTimes.computeIfAbsent(event.getDamager().getUniqueId(), k -> new AtomicLong()) + .set(System.currentTimeMillis() + swapDelayMillis); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onPlayerInteract(PlayerInteractEvent event) { - if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; - - playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new) - .lastInteractBlockTimeMillis.set(System.currentTimeMillis()); + swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()) + .set(System.currentTimeMillis() + swapDelayMillis); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onBlockPlace(BlockPlaceEvent event) { - playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new) - .lastPlaceBlockTimeMillis.set(System.currentTimeMillis()); + swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()) + .set(System.currentTimeMillis() + swapDelayMillis); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onPlayerQuit(PlayerQuitEvent event) { - playerDataMap.remove(event.getPlayer().getUniqueId()); + swapItemResumeTimes.remove(event.getPlayer().getUniqueId()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - private void onPlayerKicked(PlayerKickEvent event) { - playerDataMap.remove(event.getPlayer().getUniqueId()); - } - - private static class PlayerData { - - public final UUID uuid; - public final AtomicLong lastInteractBlockTimeMillis, lastAttackEntityTimeMillis, lastPlaceBlockTimeMillis; - - public PlayerData(UUID uuid) { - this.uuid = uuid; - this.lastInteractBlockTimeMillis = new AtomicLong(); - this.lastAttackEntityTimeMillis = new AtomicLong(); - this.lastPlaceBlockTimeMillis = new AtomicLong(); - } + private void onPlayerKick(PlayerKickEvent event) { + swapItemResumeTimes.remove(event.getPlayer().getUniqueId()); } } \ No newline at end of file