Skip to content

New config.yml option: afk-timeout-command: #6169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ public interface ISettings extends IConf {

long getAutoAfkKick();

List<String> getAfkTimeoutCommands();

boolean getFreezeAfkPlayers();

boolean cancelAfkOnMove();
Expand Down
13 changes: 13 additions & 0 deletions Essentials/src/main/java/com/earth2me/essentials/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public class Settings implements net.ess3.api.ISettings {
private Tag secondaryColor = DEFAULT_SECONDARY_COLOR;
private Set<String> multiplierPerms;
private BigDecimal defaultMultiplier;
private List<String> afkTimeoutCommands = Collections.emptyList();

public Settings(final IEssentials ess) {
this.ess = ess;
Expand Down Expand Up @@ -938,6 +939,7 @@ public void reloadConfig() {
secondaryColor = _getSecondaryColor();
multiplierPerms = _getMultiplierPerms();
defaultMultiplier = _getDefaultMultiplier();
afkTimeoutCommands = _getAfkTimeoutCommands();

reloadCount.incrementAndGet();
}
Expand Down Expand Up @@ -1266,6 +1268,17 @@ public long getAutoAfkKick() {
return config.getLong("auto-afk-kick", -1);
}

private List<String> _getAfkTimeoutCommands() {
final List<String> commands = config.getList("afk-timeout-commands", String.class);
// commands.replaceAll(String::toLowerCase);
return commands;
}

@Override
public List<String> getAfkTimeoutCommands() {
return afkTimeoutCommands;
}

@Override
public boolean getFreezeAfkPlayers() {
return getFreezeAfkPlayers;
Expand Down
27 changes: 21 additions & 6 deletions Essentials/src/main/java/com/earth2me/essentials/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -825,18 +825,33 @@ public void checkActivity() {
}

final long autoafkkick = ess.getSettings().getAutoAfkKick();

// Checks if the player has been inactive for longer than the configured auto-afk-kick time.
if (autoafkkick > 0
&& lastActivity > 0 && (lastActivity + (autoafkkick * 1000)) < System.currentTimeMillis()
&& !isAuthorized("essentials.kick.exempt")
&& !isAuthorized("essentials.afk.kickexempt")) {
lastActivity = 0;
final double kickTime = autoafkkick / 60.0;

this.getBase().kickPlayer(AdventureUtil.miniToLegacy(playerTl("autoAfkKickReason", kickTime)));

for (final User user : ess.getOnlineUsers()) {
if (user.isAuthorized("essentials.kick.notify")) {
user.sendTl("playerKicked", Console.DISPLAY_NAME, getName(), user.playerTl("autoAfkKickReason", kickTime));

// If `afk-timeout-command` in config.yml is empty, use default Essentials kicking behaviour instead of executing a command.
if (ess.getSettings().getAfkTimeoutCommands().isEmpty()) {
this.getBase().kickPlayer(AdventureUtil.miniToLegacy(playerTl("autoAfkKickReason", kickTime)));

for (final User user : ess.getOnlineUsers()) {
if (user.isAuthorized("essentials.kick.notify")) {
user.sendTl("playerKicked", Console.DISPLAY_NAME, getName(), user.playerTl("autoAfkKickReason", kickTime));
}
}
} else {
// If `afk-timeout-command` in config.yml is populated, execute the command(s) instead of kicking the player.
for (final String command : ess.getSettings().getAfkTimeoutCommands()) {
if (command == null || command.isEmpty()){
continue;
}
// Replace placeholders in the command with actual values.
final String cmd = command.replace("{USERNAME}", getName()).replace("{KICKTIME}", String.valueOf(kickTime));
ess.getServer().dispatchCommand(ess.getServer().getConsoleSender(), cmd);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions Essentials/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@ auto-afk: 300
# Set to -1 for no timeout.
auto-afk-kick: -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we rename this option to auto-afk-timeout and explain that it will run the command below if set, otherwise kick the player?


# Define a set of commands the server runs when a player's AFK time elapses.
# Set to [] to use Essentials' default AFK timeout kick behavior.
#
# Available placeholders:
# {USERNAME} - The player's username.
# {KICKTIME} - The time, in minutes, the player has been AFK for.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we need this variable, wouldn't the time the player has been afk always be the same (the auto-afk-kick above).

afk-timeout-commands: []

# Set this to true if you want to freeze players when they are AFK.
# Other players or monsters won't be able to push them out of AFK mode.
# This will also enable temporary god mode for the AFK player.
Expand Down