AFKDetector/src/main/java/com/alttd/afkdectector/AFKCheckTimer.java

80 lines
2.9 KiB
Java
Raw Normal View History

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;
import net.kyori.adventure.text.minimessage.Template;
2021-04-04 22:01:18 +00:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
2021-04-04 22:01:18 +00:00
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());
2021-07-09 07:20:46 +00:00
Bukkit.broadcast(MiniMessage.get().parse(Messages.AFKTOGGLEON.getMessage(), Template.of("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) {
2021-07-09 07:20:46 +00:00
currentTimer = new MessageTimer(plugin, uuid, Config.MESSAGEREPEATS);
2021-04-04 22:01:18 +00:00
plugin.messageTimers.put(uuid, currentTimer);
2021-07-09 07:20:46 +00:00
new MessageTimer(plugin, uuid, Config.MESSAGEREPEATS).init();
2021-04-04 22:01:18 +00:00
}
} else {
MessageTimer currentTimer = plugin.messageTimers.get(uuid);
if(currentTimer != null) {
plugin.messageTimers.remove(player.getUniqueId());
}
}
}
}
}