Files
Sdf1_login/txt/AFKManager.txt
T

138 lines
4.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package Sdf1_login;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
public class AFKManager {
private final Map<UUID, Long> lastAction = new HashMap<>();
private final Map<UUID, Integer> lastStage = new HashMap<>();
private final ConfigManager config;
private final JavaPlugin plugin;
public AFKManager(ConfigManager config, JavaPlugin plugin) {
this.config = config;
this.plugin = plugin;
}
public void startCheck() {
new BukkitRunnable() {
public void run() { tick(); }
}.runTaskTimer(plugin, 20L, 20L);
}
private void tick() {
if (!config.afkEnabled) return;
long now = System.currentTimeMillis();
for (Player p : Bukkit.getOnlinePlayers()) {
String name = p.getName();
if (!isLoggedIn(name)) continue;
if (config.getAfkWhitelist().contains(name)) continue;
UUID uuid = p.getUniqueId();
if (!lastAction.containsKey(uuid)) {
lastAction.put(uuid, now);
lastStage.put(uuid, 0);
continue;
}
long last = lastAction.get(uuid);
long idle = (now - last) / 1000;
int timeout = config.afkTimeout;
long remain = timeout - idle;
// 已超时:踢出
if (remain <= 0) {
p.kickPlayer("§c§l你已挂机 " + fmtSec(idle) + ",被踢出");
lastAction.remove(uuid);
lastStage.remove(uuid);
continue;
}
int stage = lastStage.getOrDefault(uuid, 0);
// === 阶段3:最后10秒 ===
// 刷屏倒计时 + 大标题 + 每秒音效
if (remain <= 10) {
if (stage < 3) {
lastStage.put(uuid, 3);
}
p.sendTitle("§e§l挂机了 " + fmtSec(idle),
"§c还剩 " + remain + " 秒踢出",
0, 22, 3);
playSound(p);
continue;
}
// === 阶段2:1分钟~10秒 ===
// 持续音效(每秒)+ 标题,聊天仅1次
if (remain <= 60) {
if (stage < 2) {
lastStage.put(uuid, 2);
p.sendMessage("§e§l⚠ 挂机警告 ⚠ §f你已挂机 §c" + fmtSec(idle) + "§f,还剩 §c" + fmtSec(remain) + "§f踢出");
}
p.sendTitle("§e§l挂机了 " + fmtSec(idle),
"§c还剩 " + fmtSec(remain) + " 踢出",
5, 25, 5);
playSound(p);
continue;
}
// === 阶段1:3分钟提醒 ===
// 仅1次:聊天 + 标题 + 3次音效循环
if (remain <= 180 && stage < 1) {
lastStage.put(uuid, 1);
p.sendMessage("§e§l⚠ 挂机警告 ⚠ §f你已挂机 §c" + fmtSec(idle) + "§f,还剩 §c" + fmtSec(remain) + "§f踢出");
p.sendTitle("§e§l挂机了 " + fmtSec(idle),
"§f还剩 " + fmtSec(remain) + " 踢出",
10, 60, 20);
for (int i = 0; i < 3; i++) {
final int delay = i * 10;
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (p.isOnline()) playSound(p);
}, delay);
}
}
// 超过3分钟:重置阶段
if (remain > 180) {
lastStage.remove(uuid);
}
}
}
private void playSound(Player p) {
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.0f, 0.79f);
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1.0f, 0.79f);
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0f, 0.79f);
}
public void recordAction(UUID uuid) {
lastAction.put(uuid, System.currentTimeMillis());
lastStage.put(uuid, 0);
}
public void remove(UUID uuid) {
lastAction.remove(uuid);
lastStage.remove(uuid);
}
private boolean isLoggedIn(String name) {
return ((Main) plugin).getLoggedIn().contains(name);
}
private String fmtSec(long sec) {
if (sec < 60) return sec + "秒";
long m = sec / 60;
long s = sec % 60;
return m + "分" + (s > 0 ? s + "秒" : "");
}
}