package com.alttd.afkdectector; import java.util.UUID; import com.alttd.afkdectector.afkplayer.AFKPlayer; import com.alttd.afkdectector.config.Config; import com.alttd.afkdectector.config.Messages; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; public class AFKCheckTimer extends BukkitRunnable{ private AFKDetector plugin; public AFKCheckTimer(AFKDetector plugin) { this.plugin = plugin; } public void init() { runTaskTimer(plugin, 0, 20); } @Override public void run() { for (UUID uuid : plugin.players.keySet()) { Player player = Bukkit.getPlayer(uuid); AFKPlayer afkplayer = plugin.players.get(uuid); if(player == null || player.hasPermission("afkdetector.bypass")) { continue; } Location pastLocation = afkplayer.getplayerToSphereCenter(); if(pastLocation == null || !player.getLocation().getWorld().getName().equals(pastLocation.getWorld().getName())) { pastLocation = player.getLocation(); afkplayer.setplayerToSphereCenter(pastLocation); } if (player.getLocation().distanceSquared(pastLocation) > Config.RADIUS * Config.RADIUS) { if(player.getLocation().getYaw() != pastLocation.getYaw() && player.getLocation().getPitch() != pastLocation.getPitch()) { afkplayer.setplayerToSphereCenter(pastLocation); afkplayer.setstandingTime(System.currentTimeMillis()); player.setSleepingIgnored(false); //player.setCanPickupItems(true); plugin.AFKPlayers.removeEntry(player.getName()); afkplayer.ResetAFK(); MessageTimer currentTimer = plugin.messageTimers.get(player.getUniqueId()); if (currentTimer != null) { plugin.messageTimers.remove(player.getUniqueId()); } continue; } } long standingTime = afkplayer.getstandingTime(); if(System.currentTimeMillis() - standingTime > Config.TOGGLETIME * 60 * 1000 && !afkplayer.isafk()) { afkplayer.setafk(true); player.setSleepingIgnored(true); //player.setCanPickupItems(false); plugin.AFKPlayers.addEntry(player.getName()); if (Config.AFKTOGGLEMESSAGES) { TagResolver placeholders = TagResolver.resolver( Placeholder.parsed("player", player.getName()) ); Component component = AFKDetector.miniMessage.deserialize(Messages.AFKTOGGLEON.getMessage(), placeholders); Bukkit.broadcast(component, "afkdetector.notify"); } } if(System.currentTimeMillis() - standingTime > afkplayer.getafkTime() * 60 * 1000) { MessageTimer currentTimer = plugin.messageTimers.get(uuid); if(currentTimer == null) { currentTimer = new MessageTimer(plugin, afkplayer, Config.MESSAGEREPEATS); plugin.messageTimers.put(uuid, currentTimer); currentTimer.init(); } } else { MessageTimer currentTimer = plugin.messageTimers.get(uuid); if(currentTimer != null) { plugin.messageTimers.remove(player.getUniqueId()); } } } } }