package com.entryrise.afkguard.listeners; import java.util.Map; import java.util.WeakHashMap; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerToggleFlightEvent; import org.bukkit.event.player.PlayerToggleSprintEvent; import com.entryrise.afkguard.Main; import com.entryrise.afkguard.antiafk.AFKManager; public class AFKListener implements Listener { private Map oldlocations = new WeakHashMap<>(); public AFKListener() { Bukkit.getScheduler().runTaskTimer(Main.p, () -> { for (Player p : Bukkit.getOnlinePlayers()) { oldlocations.compute(p, (k, oldloc) -> { Location newloc = p.getLocation(); if (oldloc == null) { return newloc; } double multiplier = Main.config .getDouble("settings.afk-detection.counter.actions.location-movement"); double value = 0; if (!p.isInsideVehicle()) { if (oldloc.getWorld() == newloc.getWorld()) { value += multiplier * oldloc.distanceSquared(newloc); } else { value += Main.config.getDouble("settings.afk-detection.counter.actions.world-movement"); } } else { value += Main.config.getDouble("settings.afk-detection.counter.actions.riding-movement"); } value += (Math.abs(newloc.getYaw() - oldloc.getYaw()) + Math.abs((newloc.getPitch() - oldloc.getPitch())) * Main.config.getDouble("settings.afk-detection.counter.actions.mouse-movement")); AFKManager.incrementPlayer(p, value); return newloc; }); } }, 100, 100); } /* * The plugin uses monitor as it's the intended priority for monitoring tasks. * * Since afkguard doesn't do anything directly in the event, it is the ideal * choice for this usecase. */ @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onChat(AsyncPlayerChatEvent e) { AFKManager.incrementPlayer(e.getPlayer(), Main.config.getDouble("settings.afk-detection.counter.actions.chat")); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent e) { AFKManager.incrementPlayer(e.getPlayer(), Main.config.getDouble("settings.afk-detection.counter.actions.block-break")); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockPlace(BlockPlaceEvent e) { AFKManager.incrementPlayer(e.getPlayer(), Main.config.getDouble("settings.afk-detection.counter.actions.block-place")); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onToggleSprint(PlayerToggleSprintEvent e) { AFKManager.incrementPlayer(e.getPlayer(), Main.config.getDouble("settings.afk-detection.counter.actions.toggle-sprint")); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onToggleSprint(PlayerToggleFlightEvent e) { AFKManager.incrementPlayer(e.getPlayer(), Main.config.getDouble("settings.afk-detection.counter.actions.toggle-flight")); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockPlace(PlayerFishEvent e) { AFKManager.incrementPlayer(e.getPlayer(), Main.config.getDouble("settings.afk-detection.counter.actions.fishing")); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onItemPickup(EntityPickupItemEvent e) { Player p = e.getEntity() instanceof Player ? (Player) e.getEntity() : null; if (p == null) { return; } AFKManager.incrementPlayer(p, Main.config.getDouble("settings.afk-detection.counter.actions.pickup-item")); } }