Skip to content

Commit

Permalink
Add auto-vanish feature
Browse files Browse the repository at this point in the history
Signed-off-by: applenick <applenick@users.noreply.github.com>
  • Loading branch information
applenick committed Mar 19, 2023
1 parent cd2891a commit 402097b
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>tc.oc.occ</groupId>
<artifactId>Idly</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<name>Idly</name>
<description>Prevent players from going afk in PGM</description>

Expand All @@ -16,6 +16,13 @@
<project.author>applenick</project.author>
</properties>

<distributionManagement>
<repository>
<id>pgm-repo-snapshots</id>
<url>https://repo.pgm.fyi/snapshots</url>
</repository>
</distributionManagement>

<repositories>
<repository>
<id>jitpack.io</id>
Expand Down Expand Up @@ -44,12 +51,12 @@
<dependency>
<groupId>tc.oc.pgm</groupId>
<artifactId>core</artifactId>
<version>0.13-SNAPSHOT</version>
<version>0.16-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>tc.oc.pgm</groupId>
<artifactId>util</artifactId>
<version>0.13-SNAPSHOT</version>
<version>0.16-SNAPSHOT</version>
</dependency>

<!-- Command Framework -->
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/tc/oc/occ/idly/Idly.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Idly extends JavaPlugin {
private BukkitCommandManager commands;
private IdlyConfig config;
private IdlyManager manager;
private IdlyAutoVanishManager vanishManager;
private IdlyAPI api;

@Override
Expand All @@ -27,6 +28,7 @@ public void onEnable() {
this.adventure = BukkitAudiences.create(this);
this.api = new BaseIdlyAPI();
this.manager = new IdlyManager(this);
this.vanishManager = new IdlyAutoVanishManager(config, manager);

this.commands.registerDependency(IdlyConfig.class, config);
this.commands.registerCommand(new IdlyCommand());
Expand All @@ -46,11 +48,19 @@ public IdlyAPI getAPI() {
return api;
}

public IdlyManager getIdlyManager() {
return manager;
}

public IdlyConfig getIdlyConfig() {
return config;
}

public Audience getViewer(CommandSender sender) {
return adventure.sender(sender);
}

public static void log(String message, Object... args) {
plugin.getLogger().info(String.format(message, args));
}
}
90 changes: 90 additions & 0 deletions src/main/java/tc/oc/occ/idly/IdlyAutoVanishManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package tc.oc.occ.idly;

import static net.kyori.adventure.key.Key.key;
import static net.kyori.adventure.sound.Sound.sound;
import static net.kyori.adventure.text.Component.text;
import static tc.oc.occ.idly.Idly.log;

import java.util.List;
import java.util.stream.Collectors;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import tc.oc.pgm.api.integration.Integration;
import tc.oc.pgm.util.Audience;

public class IdlyAutoVanishManager {

private final int PRE_AFK_DELAY = 5; // SECONDS
private final Sound WARN_SOUND = sound(key("note.pling"), Sound.Source.MASTER, 1f, 0.5f);

private final IdlyConfig config;
private final IdlyManager manager;

public IdlyAutoVanishManager(IdlyConfig config, IdlyManager manager) {
this.config = config;
this.manager = manager;

Idly.get()
.getServer()
.getScheduler()
.scheduleSyncRepeatingTask(
Idly.get(), this::check, 0L, 20L * config.getAutoVanishRefresh());
}

public void check() {
if (Bukkit.getOnlinePlayers().isEmpty()) return;

logMessage("=== Checking for AFK players to auto-vanish ===");
logMessage("- Delay: %d seconds", config.getAutoVanishDelay());
logMessage("- Refresh: %d seconds", config.getAutoVanishRefresh());

List<Player> staff =
Bukkit.getOnlinePlayers().stream()
.filter(p -> p.hasPermission(config.getAutoVanishPermission()))
.filter(p -> !Integration.isVanished(p) && Integration.getNick(p) == null)
.filter(p -> manager.isIdle(p, config.getAutoVanishDelay()))
.collect(Collectors.toList());

logMessage("- Found %d AFK player(s) to vanish!", staff.size());

for (Player player : staff) {
final Audience viewer = Audience.get(player);
viewer.sendMessage(
text()
.append(text("You will be auto-vanished in "))
.append(text(PRE_AFK_DELAY + " seconds", NamedTextColor.YELLOW))
.color(NamedTextColor.RED));
viewer.sendMessage(
text()
.append(text("If you're not AFK, move around or chat to cancel!"))
.color(NamedTextColor.RED)
.build());
viewer.playSound(WARN_SOUND);

Idly.get()
.getServer()
.getScheduler()
.runTaskLater(
Idly.get(),
() -> {
if (manager.isIdle(player, config.getAutoVanishDelay())) {
Bukkit.dispatchCommand(player, "vanish");
logMessage(
"- [AUTO-VANISH] %s has been auto-vanished for being AFK", player.getName());
} else {
logMessage(
"- [FALSE-ALARM] %s is not AFK (recent activity found)", player.getName());
}
},
20L * PRE_AFK_DELAY);
}
}

private void logMessage(String message, Object... args) {
if (config.isVerbose()) {
log(message, args);
}
}
}
36 changes: 36 additions & 0 deletions src/main/java/tc/oc/occ/idly/IdlyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class IdlyConfig {

private boolean enabled;
private boolean verbose;
private boolean kickMode;
private int participantDelay;
private int observerDelay;
Expand All @@ -17,7 +18,12 @@ public class IdlyConfig {
private boolean preciseMovement;
private boolean movementCheck;
private boolean chatCheck;
private boolean cmdCheck;
private String kickMessage;
private boolean autoVanish;
private String autoVanishPermission;
private int autoVanishDelay;
private int autoVanishRefresh;

public IdlyConfig(Configuration config) {
reload(config);
Expand All @@ -27,6 +33,10 @@ public boolean isEnabled() {
return enabled;
}

public boolean isVerbose() {
return verbose;
}

public boolean isKickMode() {
return kickMode;
}
Expand Down Expand Up @@ -67,12 +77,33 @@ public boolean isChatCheck() {
return chatCheck;
}

public boolean isCommandCheck() {
return cmdCheck;
}

public String getKickMessage() {
return ChatColor.translateAlternateColorCodes('&', kickMessage);
}

public boolean isAutoVanish() {
return autoVanish;
}

public String getAutoVanishPermission() {
return autoVanishPermission;
}

public int getAutoVanishDelay() {
return autoVanishDelay;
}

public int getAutoVanishRefresh() {
return autoVanishRefresh;
}

public void reload(Configuration config) {
this.enabled = config.getBoolean("enabled");
this.verbose = config.getBoolean("verbose");
this.kickMode = config.getBoolean("kick-mode");
this.participantDelay = config.getInt("participant-delay");
this.observerDelay = config.getInt("observer-delay");
Expand All @@ -83,7 +114,12 @@ public void reload(Configuration config) {
this.preciseMovement = config.getBoolean("precise-movement");
this.movementCheck = config.getBoolean("checks.movement");
this.chatCheck = config.getBoolean("checks.chat");
this.cmdCheck = config.getBoolean("checks.command");
this.kickMessage = config.getString("kick-message");
this.autoVanish = config.getBoolean("auto-vanish.enabled");
this.autoVanishPermission = config.getString("auto-vanish.permission-node");
this.autoVanishDelay = config.getInt("auto-vanish.delay");
this.autoVanishRefresh = config.getInt("auto-vanish.refresh");

this.participantDelay = convertSettingTime(participantDelay);
this.observerDelay = convertSettingTime(observerDelay);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/tc/oc/occ/idly/IdlyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import tc.oc.pgm.api.match.event.MatchStartEvent;
Expand Down Expand Up @@ -44,6 +45,13 @@ public void onPlayerChatEvent(AsyncPlayerChatEvent event) {
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
if (config.isCommandCheck()) {
this.manager.logMovement(event.getPlayer());
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMoveEvent(PlayerMoveEvent event) {
if (config.isMovementCheck()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/tc/oc/occ/idly/IdlyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public void logMovement(Player player) {
playerInactivityTicks.put(player, 0);
}

public boolean isIdle(Player player, int timeoutSeconds) {
Integer inactivityTicks = playerInactivityTicks.get(player);
if (inactivityTicks == null) {
return false;
}

int maxInactivityTicks = timeoutSeconds * TICKS_PER_SECOND;
return inactivityTicks > maxInactivityTicks;
}

private void checkPlayers() {
if (!config.isEnabled()) return;

Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Whether idle players are kicked or not
enabled: true

# Whether debug log messages are displayed to console
verbose: true

# What to do when player is marked as inactive:
# true = Kick from server
# false = Move to observers
Expand Down Expand Up @@ -32,7 +35,20 @@ precise-movement: true
checks:
movement: true
chat: true
command: true

# Message sent to player when kicked from the server
kick-message: "&c&lYou were kicked for being idle too long!"

# Allow players to be auto-vanished if idle after a configurable period of time
auto-vanish:
enabled: true
# Permission node required to auto-vanish when idle
permission-node: "idly.auto-vanish"
# Delay - how long a player must be idle for to be considered for auto-vanish
delay: 600 # Seconds
# Refresh - how often the auto-vanish task will run
refresh: 60 # Seconds



0 comments on commit 402097b

Please sign in to comment.