2021-04-04 22:01:18 +00:00
|
|
|
package com.alttd.afkdectector;
|
|
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
import com.alttd.afkdectector.afkplayer.AFKPlayer;
|
2021-07-09 07:20:46 +00:00
|
|
|
import com.alttd.afkdectector.config.Config;
|
|
|
|
|
import com.alttd.afkdectector.config.Messages;
|
|
|
|
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
2022-08-31 00:36:47 +00:00
|
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
|
|
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
2021-04-04 22:01:18 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2021-07-09 07:20:46 +00:00
|
|
|
if (player.getLocation().distanceSquared(pastLocation) > Config.RADIUS * Config.RADIUS) {
|
2021-04-04 22:01:18 +00:00
|
|
|
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();
|
2021-07-09 07:20:46 +00:00
|
|
|
if(System.currentTimeMillis() - standingTime > Config.TOGGLETIME * 60 * 1000 && !afkplayer.isafk()) {
|
2021-04-04 22:01:18 +00:00
|
|
|
afkplayer.setafk(true);
|
|
|
|
|
player.setSleepingIgnored(true);
|
|
|
|
|
//player.setCanPickupItems(false);
|
|
|
|
|
plugin.AFKPlayers.addEntry(player.getName());
|
2022-08-31 00:36:47 +00:00
|
|
|
if (Config.AFKTOGGLEMESSAGES)
|
|
|
|
|
Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.AFKTOGGLEON.getMessage(), TagResolver.resolver(Placeholder.unparsed("player", player.getName()))), "afkdetector.notify");
|
2021-04-04 22:01:18 +00:00
|
|
|
}
|
|
|
|
|
if(System.currentTimeMillis() - standingTime > afkplayer.getafkTime() * 60 * 1000) {
|
|
|
|
|
MessageTimer currentTimer = plugin.messageTimers.get(uuid);
|
|
|
|
|
if(currentTimer == null) {
|
2022-08-31 00:36:47 +00:00
|
|
|
currentTimer = new MessageTimer(plugin, afkplayer, Config.MESSAGEREPEATS);
|
2021-04-04 22:01:18 +00:00
|
|
|
plugin.messageTimers.put(uuid, currentTimer);
|
2022-08-31 00:36:47 +00:00
|
|
|
currentTimer.init();
|
2021-04-04 22:01:18 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MessageTimer currentTimer = plugin.messageTimers.get(uuid);
|
|
|
|
|
if(currentTimer != null) {
|
|
|
|
|
plugin.messageTimers.remove(player.getUniqueId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|