Skip to content

Commit

Permalink
shorten code even more
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 8, 2024
1 parent c537c3d commit 2cd208a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<UUID, AtomicLong> swapItemResumeTimes;
private final long swapDelayMillis;
private final ExpiringSet<UUID> 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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<UUID, AtomicLong> swapItemResumeTimes;
private final long swapDelayMillis;
private final ExpiringSet<UUID> 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
Expand All @@ -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());
}
}

0 comments on commit 2cd208a

Please sign in to comment.