Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 8, 2024
1 parent e5433fc commit c537c3d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,13 +20,13 @@

public class SilentSwapDelay extends AEFModule implements Listener {

private final Map<UUID, PlayerData> playerDataMap;
private final Map<UUID, AtomicLong> 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)");
Expand All @@ -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);
}
}
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,13 +20,13 @@

public class SilentSwapDelay extends AEFModule implements Listener {

private final Map<UUID, PlayerData> playerDataMap;
private final Map<UUID, AtomicLong> 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)");
Expand All @@ -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);
}
}
Expand All @@ -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());
}
}

0 comments on commit c537c3d

Please sign in to comment.