From 2cd208a2afa99d29a97955b183432be4f0968a4e Mon Sep 17 00:00:00 2001 From: xGinko Date: Thu, 8 Aug 2024 19:26:49 +0200 Subject: [PATCH] shorten code even more --- .../aef/modules/combat/SilentSwapDelay.java | 45 ++++++------------- .../aef/modules/combat/SilentSwapDelay.java | 45 ++++++------------- 2 files changed, 28 insertions(+), 62 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 4ce6282d..8ead2819 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 @@ -2,6 +2,7 @@ import com.cryptomorin.xseries.XEntityType; import me.xginko.aef.modules.AEFModule; +import me.xginko.aef.utils.models.ExpiringSet; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; @@ -10,26 +11,21 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemHeldEvent; -import org.bukkit.event.player.PlayerKickEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import java.util.Map; +import java.time.Duration; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicLong; public class SilentSwapDelay extends AEFModule implements Listener { - private final Map swapItemResumeTimes; - private final long swapDelayMillis; + private final ExpiringSet swapItemCooldowns; public SilentSwapDelay() { super("combat.crystal-aura.silent-swap-delay"); - 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)"); + this.swapItemCooldowns = new ExpiringSet<>(Duration.ofMillis(Math.max(1L, + 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)")))); } @Override @@ -49,38 +45,25 @@ public void disable() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot bar item selection changes - if (swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()).get() > System.currentTimeMillis()) { + if (swapItemCooldowns.contains(event.getPlayer().getUniqueId())) { event.setCancelled(true); } } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onEntityDamageByEntity(EntityDamageByEntityEvent event) { - if (event.getDamager().getType() != XEntityType.PLAYER.get()) return; - - swapItemResumeTimes.computeIfAbsent(event.getDamager().getUniqueId(), k -> new AtomicLong()) - .set(System.currentTimeMillis() + swapDelayMillis); + if (event.getDamager().getType() == XEntityType.PLAYER.get()) { + swapItemCooldowns.add(event.getDamager().getUniqueId()); + } } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onPlayerInteract(PlayerInteractEvent event) { - swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()) - .set(System.currentTimeMillis() + swapDelayMillis); + swapItemCooldowns.add(event.getPlayer().getUniqueId()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onBlockPlace(BlockPlaceEvent event) { - swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()) - .set(System.currentTimeMillis() + swapDelayMillis); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - private void onPlayerQuit(PlayerQuitEvent event) { - swapItemResumeTimes.remove(event.getPlayer().getUniqueId()); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - private void onPlayerKick(PlayerKickEvent event) { - swapItemResumeTimes.remove(event.getPlayer().getUniqueId()); + swapItemCooldowns.add(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 4ce6282d..8ead2819 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 @@ -2,6 +2,7 @@ import com.cryptomorin.xseries.XEntityType; import me.xginko.aef.modules.AEFModule; +import me.xginko.aef.utils.models.ExpiringSet; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; @@ -10,26 +11,21 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemHeldEvent; -import org.bukkit.event.player.PlayerKickEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import java.util.Map; +import java.time.Duration; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicLong; public class SilentSwapDelay extends AEFModule implements Listener { - private final Map swapItemResumeTimes; - private final long swapDelayMillis; + private final ExpiringSet swapItemCooldowns; public SilentSwapDelay() { super("combat.crystal-aura.silent-swap-delay"); - 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)"); + this.swapItemCooldowns = new ExpiringSet<>(Duration.ofMillis(Math.max(1L, + 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)")))); } @Override @@ -49,38 +45,25 @@ public void disable() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot bar item selection changes - if (swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()).get() > System.currentTimeMillis()) { + if (swapItemCooldowns.contains(event.getPlayer().getUniqueId())) { event.setCancelled(true); } } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onEntityDamageByEntity(EntityDamageByEntityEvent event) { - if (event.getDamager().getType() != XEntityType.PLAYER.get()) return; - - swapItemResumeTimes.computeIfAbsent(event.getDamager().getUniqueId(), k -> new AtomicLong()) - .set(System.currentTimeMillis() + swapDelayMillis); + if (event.getDamager().getType() == XEntityType.PLAYER.get()) { + swapItemCooldowns.add(event.getDamager().getUniqueId()); + } } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onPlayerInteract(PlayerInteractEvent event) { - swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()) - .set(System.currentTimeMillis() + swapDelayMillis); + swapItemCooldowns.add(event.getPlayer().getUniqueId()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onBlockPlace(BlockPlaceEvent event) { - swapItemResumeTimes.computeIfAbsent(event.getPlayer().getUniqueId(), k -> new AtomicLong()) - .set(System.currentTimeMillis() + swapDelayMillis); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - private void onPlayerQuit(PlayerQuitEvent event) { - swapItemResumeTimes.remove(event.getPlayer().getUniqueId()); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - private void onPlayerKick(PlayerKickEvent event) { - swapItemResumeTimes.remove(event.getPlayer().getUniqueId()); + swapItemCooldowns.add(event.getPlayer().getUniqueId()); } } \ No newline at end of file