commit
d7797a496d
|
|
@ -10,6 +10,8 @@ import org.bukkit.Bukkit;
|
|||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.util.NumberConversions;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
|
@ -47,15 +49,35 @@ public class AFKCheckTimer extends BukkitRunnable {
|
|||
}
|
||||
|
||||
long standingTime = afkPlayer.getStandingTime();
|
||||
if (!afkPlayer.isAFK() && System.currentTimeMillis() - standingTime > TimeUnit.MINUTES.toMillis(Config.WARNING_TIME)) {
|
||||
afkPlayer.warnPlayer(player);
|
||||
}
|
||||
if (!afkPlayer.isAFK() && System.currentTimeMillis() - standingTime > TimeUnit.MINUTES.toMillis(Config.TOGGLE_TIME)) {
|
||||
setPlayerAFK(afkPlayer, player);
|
||||
}
|
||||
if (afkPlayer.isAFK()) {
|
||||
afkPlayer.updateBossBarProgress(afkPlayer.getAfkTimeMinutes());
|
||||
}
|
||||
runMessageTimerCheck(afkPlayer, uuid, standingTime);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean playerMovedOutOfSphere(Player player, Location pastLocation) {
|
||||
return player.getLocation().distanceSquared(pastLocation) > Config.RADIUS * Config.RADIUS;
|
||||
return TwoDDistanceSquared(player.getLocation(), pastLocation) > Config.RADIUS * Config.RADIUS;
|
||||
// return player.getLocation().distanceSquared(pastLocation) > Config.RADIUS * Config.RADIUS;
|
||||
}
|
||||
|
||||
private double TwoDDistanceSquared(@NotNull Location o1, @NotNull Location o2) {
|
||||
if (o2.getWorld() != null && o1.getWorld() != null) {
|
||||
if (o2.getWorld() != o1.getWorld()) {
|
||||
String var10002 = o1.getWorld().getName();
|
||||
throw new IllegalArgumentException("Cannot measure distance between " + var10002 + " and " + o2.getWorld().getName());
|
||||
} else {
|
||||
return NumberConversions.square(o1.getX() - o2.getX()) + NumberConversions.square(o1.getZ() - o2.getZ());
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Cannot measure distance to a null world");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean playerHeadMoved(Location playerLocation, Location pastLocation) {
|
||||
|
|
@ -75,7 +97,7 @@ public class AFKCheckTimer extends BukkitRunnable {
|
|||
}
|
||||
|
||||
private void setPlayerAFK(AFKPlayer afkPlayer, Player player) {
|
||||
afkPlayer.setAFK(true);
|
||||
afkPlayer.setAFK(player);
|
||||
player.setSleepingIgnored(true);
|
||||
//player.setCanPickupItems(false);
|
||||
if (Config.AFK_TOGGLE_MESSAGES) {
|
||||
|
|
@ -88,7 +110,7 @@ public class AFKCheckTimer extends BukkitRunnable {
|
|||
}
|
||||
|
||||
private void runMessageTimerCheck(AFKPlayer afkPlayer, UUID uuid, long standingTime) {
|
||||
if (System.currentTimeMillis() - standingTime > TimeUnit.MINUTES.toMillis(afkPlayer.getAfkTime())) {
|
||||
if (System.currentTimeMillis() - standingTime > TimeUnit.MINUTES.toMillis(afkPlayer.getAfkTimeMinutes())) {
|
||||
MessageTimer currentTimer = plugin.messageTimers.get(uuid);
|
||||
if (currentTimer == null) {
|
||||
currentTimer = new MessageTimer(plugin, afkPlayer, Config.MESSAGE_REPEATS);
|
||||
|
|
|
|||
|
|
@ -155,7 +155,7 @@ public class AFKDetector extends JavaPlugin implements Listener {
|
|||
}
|
||||
|
||||
Player player = event.getPlayer();
|
||||
getPlayer(player).ResetAFK();
|
||||
getPlayer(player).resetAFK(player);
|
||||
messageHistory.addEntry(uuid, message);
|
||||
}
|
||||
|
||||
|
|
@ -175,26 +175,10 @@ public class AFKDetector extends JavaPlugin implements Listener {
|
|||
}
|
||||
|
||||
Player player = event.getPlayer();
|
||||
getPlayer(player).ResetAFK();
|
||||
getPlayer(player).resetAFK(player);
|
||||
commandHistory.addEntry(uuid, command);
|
||||
}
|
||||
|
||||
/* @EventHandler
|
||||
public void onPlayerMove(PlayerMoveEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (player != null) {
|
||||
if (!player.hasPermission("afkdetect.bypass")) {
|
||||
if(!player.isInsideVehicle()) {
|
||||
float yawDif = Math.abs(event.getFrom().getYaw() - event.getTo().getYaw());
|
||||
float pitchDif = Math.abs(event.getFrom().getPitch() - event.getTo().getPitch());
|
||||
if (yawDif != 0.0F || pitchDif != 0.0F) {
|
||||
//getPlayer(player).ResetAFK();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
public static AFKDetector getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
|
@ -203,7 +187,7 @@ public class AFKDetector extends JavaPlugin implements Listener {
|
|||
Config.reload();
|
||||
MessagesConfig.reload();
|
||||
if (sender != null) {
|
||||
sender.sendMessage("Configuration reloaded");
|
||||
sender.sendMiniMessage("<green>Configuration reloaded", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,13 +4,19 @@ import com.alttd.afkdectector.AFKDetector;
|
|||
import com.alttd.afkdectector.config.Config;
|
||||
import com.alttd.afkdectector.config.Messages;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.boss.BarColor;
|
||||
import org.bukkit.boss.BarStyle;
|
||||
import org.bukkit.boss.BossBar;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class AFKPlayer {
|
||||
|
||||
|
|
@ -19,6 +25,10 @@ public class AFKPlayer {
|
|||
private long standingTime;
|
||||
private final int afkTime;
|
||||
private boolean isAFK;
|
||||
private boolean isWarned;
|
||||
private final BossBar bossBar;
|
||||
private final String afkSoon;
|
||||
private final String afkKickSoon;
|
||||
|
||||
public AFKPlayer(Player player, AFKDetector plugin) {
|
||||
this.player = player;
|
||||
|
|
@ -26,6 +36,10 @@ public class AFKPlayer {
|
|||
this.standingTime = System.currentTimeMillis();
|
||||
this.afkTime = plugin.getAllowedAFKTime(player);
|
||||
this.isAFK = false;
|
||||
this.bossBar = Bukkit.createBossBar("Time until AFK", BarColor.PURPLE, BarStyle.SOLID);
|
||||
MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
this.afkSoon = LegacyComponentSerializer.builder().build().serialize(miniMessage.deserialize(Messages.AFK_SOON_BOSS_BAR.getMessage()));
|
||||
this.afkKickSoon = LegacyComponentSerializer.builder().build().serialize(miniMessage.deserialize(Messages.AFK_KICK_SOON_BOSS_BAR.getMessage()));
|
||||
}
|
||||
|
||||
public String getPlayerName() {
|
||||
|
|
@ -52,7 +66,7 @@ public class AFKPlayer {
|
|||
standingTime = Time;
|
||||
}
|
||||
|
||||
public int getAfkTime() {
|
||||
public int getAfkTimeMinutes() {
|
||||
if (isInSpawn())
|
||||
return this.afkTime + Config.EXTRA_MIN_IN_SPAWN;
|
||||
return this.afkTime;
|
||||
|
|
@ -82,8 +96,45 @@ public class AFKPlayer {
|
|||
Bukkit.broadcast(component, "afkdetector.notify");
|
||||
}
|
||||
standingTime = System.currentTimeMillis();
|
||||
player.setAfk(false);
|
||||
playerToSphereCenter = player.getLocation();
|
||||
isAFK = false;
|
||||
unWarnPlayer();
|
||||
}
|
||||
|
||||
public void setAFK(Player player) {
|
||||
bossBar.setTitle(afkKickSoon);
|
||||
bossBar.setColor(BarColor.RED);
|
||||
player.setAfk(true);
|
||||
isAFK = true;
|
||||
playerToSphereCenter = (player == null) ? null : player.getLocation();
|
||||
setAFK(false);
|
||||
}
|
||||
|
||||
public boolean isAFK() {
|
||||
return isAFK;
|
||||
}
|
||||
|
||||
private void unWarnPlayer() {
|
||||
if (!isWarned)
|
||||
return;
|
||||
bossBar.removeAll();
|
||||
isWarned = false;
|
||||
}
|
||||
|
||||
public void warnPlayer(Player player) {
|
||||
updateBossBarProgress(Config.TOGGLE_TIME);
|
||||
if (isWarned)
|
||||
return;
|
||||
bossBar.setColor(BarColor.PURPLE);
|
||||
bossBar.setTitle(afkSoon);
|
||||
bossBar.addPlayer(player);
|
||||
isWarned = true;
|
||||
}
|
||||
|
||||
public void updateBossBarProgress(int totalAllowedMinutes) {
|
||||
double afkSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - getStandingTime());
|
||||
double toggleSeconds = TimeUnit.MINUTES.toSeconds(totalAllowedMinutes);
|
||||
bossBar.setProgress(Math.min(afkSeconds / toggleSeconds, 1));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,9 +35,10 @@ public class Config extends AbstractConfiguration {
|
|||
|
||||
public static boolean SLEEP_IGNORE = false;
|
||||
public static int RADIUS = 4;
|
||||
public static int TOGGLE_TIME = 4;
|
||||
public static int DEFAULT_AFK_TIME = 5;
|
||||
public static int MAX_AFK_TIME = 5;
|
||||
public static int WARNING_TIME = 5;
|
||||
public static int TOGGLE_TIME = 7;
|
||||
public static int DEFAULT_AFK_TIME = 30;
|
||||
public static int MAX_AFK_TIME = 60;
|
||||
public static boolean SERVER_FULL = false;
|
||||
public static int PLAYER_LIMIT = Math.round(Bukkit.getMaxPlayers() * 90 / 100);
|
||||
public static int COMMAND_COOL_DOWN = 60;
|
||||
|
|
@ -48,6 +49,7 @@ public class Config extends AbstractConfiguration {
|
|||
private static void playerSettings() {
|
||||
SLEEP_IGNORE = config.getBoolean("player.sleep", SLEEP_IGNORE);
|
||||
RADIUS = config.getInt("player.radius", RADIUS);
|
||||
WARNING_TIME = config.getInt("player.warning-time", WARNING_TIME);
|
||||
TOGGLE_TIME = config.getInt("player.toggle-time", TOGGLE_TIME);
|
||||
DEFAULT_AFK_TIME = config.getInt("player.afk-time", 5);
|
||||
MAX_AFK_TIME = config.getInt("player.maxafk-time", 5);
|
||||
|
|
|
|||
|
|
@ -20,7 +20,10 @@ public enum Messages {
|
|||
AFK_CHECK_SUBTITLE("afkcheck-subtitle", "Please respond to the dm from staff!"),
|
||||
AFK_CHECK_MESSAGE("afkcheck-message", "Hey, since you're near a farm and not moving. I'm making sure you aren't afk. Please respond to me if you're not AFK."),
|
||||
AFK_KICK_STAFF_MESSAGE("afkkick-staff-messsge", "<gold><player> got afk kicked after being afk for <afk_time> minutes."),
|
||||
SUSPICIOUS_KICK_COUNT("afkkick-suspicious-message", "<gold><player> has had <count> suspicious AFK kicks since last reboot.");
|
||||
SUSPICIOUS_KICK_COUNT("afkkick-suspicious-message", "<gold><player> has had <count> suspicious AFK kicks since last reboot."),
|
||||
AFK_SOON_BOSS_BAR("afk-soon-boss-bar", "<dark_aqua>Time until AFK.</dark_aqua>"),
|
||||
AFK_KICK_SOON_BOSS_BAR("afk-kick-soon-boss-bar", "<dark_aqua>Time until <red>AFK kick</red>.</dark_aqua>"),
|
||||
AFK_NOW_BOSS_BAR("afk-now-boss-bar", "<red>AFK</red>");
|
||||
|
||||
private final String key;
|
||||
private String message;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user