Skip to content

Commit

Permalink
better accuracy for silent swap
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 10, 2024
1 parent 5611429 commit 256deb9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand All @@ -11,21 +10,27 @@
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.time.Duration;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class SilentSwapDelay extends AEFModule implements Listener {

private final ExpiringSet<UUID> swapItemCooldowns;
private final Map<UUID, Long> swapItemCooldowns;
private final long cooldownNanos;

public SilentSwapDelay() {
super("combat.silent-swap-delay");
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)"))));
this.swapItemCooldowns = new ConcurrentHashMap<>();
this.cooldownNanos = TimeUnit.MILLISECONDS.toNanos(
config.getLong(configPath + ".min-swap-delay-millis", 40L,"""
The delay in millis a player cant swap hotbar items after placing
a block, clicking a block (for example to place a crystal) or
damaging an entity. (50 ms = 1 tick)"""));
}

@Override
Expand All @@ -45,25 +50,37 @@ public void disable() {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot bar item selection changes
if (swapItemCooldowns.contains(event.getPlayer().getUniqueId())) {
if (!swapItemCooldowns.containsKey(event.getPlayer().getUniqueId())) return;

if (swapItemCooldowns.get(event.getPlayer().getUniqueId()) > System.nanoTime()) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if (event.getDamager().getType() == XEntityType.PLAYER.get()) {
swapItemCooldowns.add(event.getDamager().getUniqueId());
swapItemCooldowns.put(event.getDamager().getUniqueId(), System.nanoTime() + cooldownNanos);
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerInteract(PlayerInteractEvent event) {
swapItemCooldowns.add(event.getPlayer().getUniqueId());
swapItemCooldowns.put(event.getPlayer().getUniqueId(), System.nanoTime() + cooldownNanos);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
swapItemCooldowns.add(event.getPlayer().getUniqueId());
swapItemCooldowns.put(event.getPlayer().getUniqueId(), System.nanoTime() + cooldownNanos);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerQuit(PlayerQuitEvent event) {
swapItemCooldowns.remove(event.getPlayer().getUniqueId());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerKick(PlayerKickEvent event) {
swapItemCooldowns.remove(event.getPlayer().getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand All @@ -11,21 +10,27 @@
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.time.Duration;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class SilentSwapDelay extends AEFModule implements Listener {

private final ExpiringSet<UUID> swapItemCooldowns;
private final Map<UUID, Long> swapItemCooldowns;
private final long cooldownNanos;

public SilentSwapDelay() {
super("combat.silent-swap-delay");
this.swapItemCooldowns = new ExpiringSet<>(Duration.ofMillis(Math.max(1L,
this.swapItemCooldowns = new ConcurrentHashMap<>();
this.cooldownNanos = TimeUnit.MILLISECONDS.toNanos(
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)"))));
"a block, clicking a block (for example to place a crystal) or\n" +
"damaging an entity. (50 ms = 1 tick)"));
}

@Override
Expand All @@ -45,25 +50,37 @@ public void disable() {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot bar item selection changes
if (swapItemCooldowns.contains(event.getPlayer().getUniqueId())) {
if (!swapItemCooldowns.containsKey(event.getPlayer().getUniqueId())) return;

if (swapItemCooldowns.get(event.getPlayer().getUniqueId()) > System.nanoTime()) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if (event.getDamager().getType() == XEntityType.PLAYER.get()) {
swapItemCooldowns.add(event.getDamager().getUniqueId());
swapItemCooldowns.put(event.getDamager().getUniqueId(), System.nanoTime() + cooldownNanos);
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerInteract(PlayerInteractEvent event) {
swapItemCooldowns.add(event.getPlayer().getUniqueId());
swapItemCooldowns.put(event.getPlayer().getUniqueId(), System.nanoTime() + cooldownNanos);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
swapItemCooldowns.add(event.getPlayer().getUniqueId());
swapItemCooldowns.put(event.getPlayer().getUniqueId(), System.nanoTime() + cooldownNanos);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerQuit(PlayerQuitEvent event) {
swapItemCooldowns.remove(event.getPlayer().getUniqueId());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerKick(PlayerKickEvent event) {
swapItemCooldowns.remove(event.getPlayer().getUniqueId());
}
}

0 comments on commit 256deb9

Please sign in to comment.