From 3c41077bcc906b71169b0d58a511572672c620ff Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Sat, 6 Jul 2024 20:26:00 +0200 Subject: [PATCH] add config options for disabling broadcasting of afk messages --- CHANGELOG.md | 4 +-- gradle.properties | 2 +- .../dk/magnusjensen/simpleafk/AFKPlayer.java | 27 ++++++++++++------- .../simpleafk/config/ServerConfig.java | 12 +++++++++ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1006ebd..559ba2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,2 @@ -## 1.20.1 - 1.1.2 -- Fix version reange \ No newline at end of file +## 1.20.1 - 1.2.0 +- Add config options for enabled/disabling AFK messages being sent to all players on the server. \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index f757197..031a52d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/dk/magnusjensen/simpleafk/AFKPlayer.java b/src/main/java/dk/magnusjensen/simpleafk/AFKPlayer.java index 5c51c65..d5a049d 100644 --- a/src/main/java/dk/magnusjensen/simpleafk/AFKPlayer.java +++ b/src/main/java/dk/magnusjensen/simpleafk/AFKPlayer.java @@ -15,13 +15,12 @@ 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; @@ -29,7 +28,7 @@ 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; } @@ -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 && @@ -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) { @@ -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() { @@ -112,7 +121,7 @@ public long getSecondsSinceAfk() { } public long getSecondsSinceLastMove() { - return (System.currentTimeMillis() / 1000) - timesstampSinceLastMove; + return (System.currentTimeMillis() / 1000) - timestampSinceLastMove; } public boolean isAfk() { diff --git a/src/main/java/dk/magnusjensen/simpleafk/config/ServerConfig.java b/src/main/java/dk/magnusjensen/simpleafk/config/ServerConfig.java index c80802e..2b60f3e 100644 --- a/src/main/java/dk/magnusjensen/simpleafk/config/ServerConfig.java +++ b/src/main/java/dk/magnusjensen/simpleafk/config/ServerConfig.java @@ -29,9 +29,17 @@ public class ServerConfig private static final ForgeConfigSpec.ConfigValue 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 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 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 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 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")); @@ -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; @@ -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(); }