Skip to content

Commit

Permalink
add config options for disabling broadcasting of afk messages
Browse files Browse the repository at this point in the history
  • Loading branch information
MagnusHJensen committed Jul 6, 2024
1 parent 768fd32 commit 3c41077
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
## 1.20.1 - 1.1.2
- Fix version reange
## 1.20.1 - 1.2.0
- Add config options for enabled/disabling AFK messages being sent to all players on the server.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod_name=Simple AFK
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=LGPL-3.0
# The mod version. See https://semver.org/
mod_version=1.1.2
mod_version=1.2.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/dk/magnusjensen/simpleafk/AFKPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.server.permission.PermissionAPI;

public class AFKPlayer {
private final ServerPlayer player;
private boolean isAfk;
private long timestampSinceAfk;
private long timesstampSinceLastMove;
private long timestampSinceLastMove;
private BlockPos lastPosition;


public AFKPlayer(ServerPlayer player) {
this.player = player;
this.isAfk = false;
this.timestampSinceAfk = System.currentTimeMillis() / 1000;
this.timesstampSinceLastMove = System.currentTimeMillis() / 1000;
this.timestampSinceLastMove = System.currentTimeMillis() / 1000;
this.lastPosition = null;
}

Expand All @@ -51,7 +50,7 @@ public void tick(ServerPlayer player) {
} else if (player.level().getGameTime() % 20 == 0) {
long timestampInSeconds = System.currentTimeMillis() / 1000;
// Check if the player is not marked as AFK, and if the player has not moved for the amount of seconds specified in the config.
if (!isAfk && timestampInSeconds - timesstampSinceLastMove >= ServerConfig.secondsBeforeAfk) {
if (!isAfk && timestampInSeconds - timestampSinceLastMove >= ServerConfig.secondsBeforeAfk) {
setAfkStatus();
} else if (
ServerConfig.secondsBeforeKick > 0 &&
Expand All @@ -78,17 +77,27 @@ private void setAfkStatus() {
move(player.blockPosition());
this.player.refreshDisplayName();
this.player.refreshTabListName();
Utilities.broadcastSystemMessage(Utilities.formatMessageWithPlayerName(ServerConfig.isNowAfkMessage, player.getDisplayName().getString()));

if (ServerConfig.isNowAfkMessageEnabled) {
Utilities.broadcastSystemMessage(Utilities.formatMessageWithPlayerName(ServerConfig.isNowAfkMessage, player.getDisplayName().getString()));
} else {
player.sendSystemMessage(Utilities.formatMessageWithPlayerName(ServerConfig.isNowAfkMessage, player.getDisplayName().getString()), false);
}
}

private void removeAfkStatus() {
this.isAfk = false;
this.timestampSinceAfk = System.currentTimeMillis() / 1000;
this.timesstampSinceLastMove = System.currentTimeMillis() / 1000;
this.timestampSinceLastMove = System.currentTimeMillis() / 1000;
move(player.blockPosition());
this.player.refreshDisplayName();
this.player.refreshTabListName();
Utilities.broadcastSystemMessage(Utilities.formatMessageWithPlayerName(ServerConfig.isNoLongerAfkMessage, player.getDisplayName().getString()));

if (ServerConfig.isNoLongerAfkMessageEnabled) {
Utilities.broadcastSystemMessage(Utilities.formatMessageWithPlayerName(ServerConfig.isNoLongerAfkMessage, player.getDisplayName().getString()));
} else {
player.sendSystemMessage(Utilities.formatMessageWithPlayerName(ServerConfig.isNoLongerAfkMessage, player.getDisplayName().getString()), false);
}
}

private boolean hasPlayerMoved(BlockPos currentPos) {
Expand All @@ -97,7 +106,7 @@ private boolean hasPlayerMoved(BlockPos currentPos) {

private void move(BlockPos pos) {
this.lastPosition = pos;
this.timesstampSinceLastMove = System.currentTimeMillis() / 1000;
this.timestampSinceLastMove = System.currentTimeMillis() / 1000;
}

public ServerPlayer getPlayer() {
Expand All @@ -112,7 +121,7 @@ public long getSecondsSinceAfk() {
}

public long getSecondsSinceLastMove() {
return (System.currentTimeMillis() / 1000) - timesstampSinceLastMove;
return (System.currentTimeMillis() / 1000) - timestampSinceLastMove;
}

public boolean isAfk() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/dk/magnusjensen/simpleafk/config/ServerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ public class ServerConfig
private static final ForgeConfigSpec.ConfigValue<String> IS_NOW_AFK_MESSAGE = BUILDER
.comment("How the message should be displayed when a player is marked as AFK", "Can be edited with the regular minecraft chat codes which can be found here https://minecraft.fandom.com/wiki/Formatting_codes#Color_codes", "$player will be replaced with the actual player name", "$player is required to appear in the text")
.define("isNowAfkMessage", "§e§o$player is now AFK", (obj) -> obj instanceof String && obj.toString().contains("$player"));

private static final ForgeConfigSpec.ConfigValue<Boolean> IS_NOW_AFK_MESSAGE_ENABLED = BUILDER
.comment("Whether or not the player is now AFK message should be sent to all players.", "Note: It will always be sent to the player that has gone AFK.")
.define("isNowAfkMessageEnabled", true);
private static final ForgeConfigSpec.ConfigValue<String> IS_NO_LONGER_AFK_MESSAGE = BUILDER
.comment("How the message should be displayed when a player is no longer marked as AFK", "Can be edited with the regular minecraft chat codes which can be found here https://minecraft.fandom.com/wiki/Formatting_codes#Color_codes", "$player will be replaced with the actual player name", "$player is required to appear in the text")
.define("isNoLongerAfkMessage", "§e§o$player is no longer AFK", (obj) -> obj instanceof String && obj.toString().contains("$player"));

private static final ForgeConfigSpec.ConfigValue<Boolean> IS_NO_LONGER_AFK_MESSAGE_ENABLED = BUILDER
.comment("Whether or not the player is no longer AFK message should be sent to all players.", "Note: It will always be sent to the player that is no longer AFK.")
.define("isNoLongerAfkMessageEnabled", true);
private static final ForgeConfigSpec.ConfigValue<String> PLAYER_NAME_FORMAT = BUILDER
.comment("How the player name should appear when a player is AFK (includes tab list and nametag above player)", "Can be edited with the regular minecraft chat codes which can be found here https://minecraft.fandom.com/wiki/Formatting_codes#Color_codes", "$player will be replaced with the actual player name", "$player is required to appear in the text")
.define("playerNameFormat", "§7[AFK] §r$player", (obj) -> obj instanceof String && obj.toString().contains("$player"));
Expand All @@ -43,7 +51,9 @@ public class ServerConfig
public static int secondsBeforeAfk;
public static int secondsBeforeKick;
public static String isNowAfkMessage;
public static Boolean isNowAfkMessageEnabled;
public static String isNoLongerAfkMessage;
public static Boolean isNoLongerAfkMessageEnabled;
public static String playerNameFormat;
public static String afkKickMessage;

Expand All @@ -54,7 +64,9 @@ static void onLoad(final ModConfigEvent event)
secondsBeforeAfk = SECONDS_BEFORE_AFK.get();
secondsBeforeKick = SECONDS_BEFORE_KICK.get();
isNowAfkMessage = IS_NOW_AFK_MESSAGE.get();
isNowAfkMessageEnabled = IS_NOW_AFK_MESSAGE_ENABLED.get();
isNoLongerAfkMessage = IS_NO_LONGER_AFK_MESSAGE.get();
isNoLongerAfkMessageEnabled = IS_NO_LONGER_AFK_MESSAGE_ENABLED.get();
playerNameFormat = PLAYER_NAME_FORMAT.get();
afkKickMessage = AFK_KICK_MESSAGE.get();
}
Expand Down

0 comments on commit 3c41077

Please sign in to comment.