Skip to content

Commit

Permalink
Add AFK command cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Mar 4, 2024
1 parent 14f174a commit 04ae20f
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions patches/server/0072-Improve-Purpur-AFK-system.patch
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Date: Sat, 2 Mar 2024 18:22:15 -0500
Subject: [PATCH] Improve Purpur AFK system

Add afk command
Add afk command cooldown
Add custom afk tab name

diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
Expand Down Expand Up @@ -55,39 +56,57 @@ index 9950580f3d0387b142576bfdb6a8f74365c12878..4270b24734affdeff0cd49ab494b7fad
}

diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 42885c7f374f619fe63d9fe22a4b7f88d68008c3..c7fbf58f0aa653bf4b89fa0787f5ad4ca83e51d5 100644
index 42885c7f374f619fe63d9fe22a4b7f88d68008c3..08407b2eaacb581085f6c22820304fd179c42af4 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2266,6 +2266,12 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
@@ -2264,8 +2264,26 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
}
}

+ private final Map<ServerPlayer, Long> cooldown = new java.util.concurrent.ConcurrentHashMap<>(); // Purpur
+
@Override
public void handleChatCommand(ServerboundChatCommandPacket packet) {
+ // Purpur start
+ if (packet.command().equals("afk")) {
+ player.commandAfkStatus = player.isAfk();
+ player.isCommandAfk = true;
+ long currentTime = System.nanoTime();
+ if (org.purpurmc.purpur.PurpurConfig.afkCommandCooldown > 0) {
+ if (cooldown.containsKey(player) && (currentTime - cooldown.get(player)) / 1000000000 <= org.purpurmc.purpur.PurpurConfig.afkCommandCooldown) {
+ String msg = org.purpurmc.purpur.PurpurConfig.afkCooldown;
+ if (msg != null && !msg.isEmpty()) player.sendMessage(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(msg.replace("%time%", String.valueOf(org.purpurmc.purpur.PurpurConfig.afkCommandCooldown - cooldown.get(player)))));
+ return;
+ } else {
+ cooldown.put(player, currentTime);
+ }
+ }
+ }
+ // Purpur end
if (ServerGamePacketListenerImpl.isChatMessageIllegal(packet.command())) {
this.disconnect(Component.translatable("multiplayer.disconnect.illegal_characters"), org.bukkit.event.player.PlayerKickEvent.Cause.ILLEGAL_CHARACTERS); // Paper
} else {
diff --git a/src/main/java/org/purpurmc/purpur/PurpurConfig.java b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
index e45c11bf2c42e6d57f803349d8ca6936ac98c995..61dc8b798f1246c19c7e45e9ae9c54366286cfce 100644
index e45c11bf2c42e6d57f803349d8ca6936ac98c995..9536b0c27f5422161d6bd9fa6916735fa31602f0 100644
--- a/src/main/java/org/purpurmc/purpur/PurpurConfig.java
+++ b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
@@ -176,8 +176,11 @@ public class PurpurConfig {
@@ -176,9 +176,14 @@ public class PurpurConfig {
public static String cannotRideMob = "<red>You cannot mount that mob";
public static String afkBroadcastAway = "<yellow><italic>%s is now AFK";
public static String afkBroadcastBack = "<yellow><italic>%s is no longer AFK";
+ public static String afkTitleAway = "<gold><bold>AFK";
+ public static String afkSubTitleAway = "<red>You are now AFK...";
public static boolean afkBroadcastUseDisplayName = false;
public static String afkTabListPrefix = "[AFK] ";
- public static String afkTabListSuffix = "";
+ public static String afkTabListCustomName = "%player_name% <red><bold>[AFK]<reset>";
public static String afkTabListSuffix = "";
+ public static String afkTabListSuffix = "You need to wait %time%s to use /afk.";
+ public static long afkCommandCooldown = 3;
+ public static String afkCooldown = "";
public static String creditsCommandOutput = "<green>%s has been shown the end credits";
public static String demoCommandOutput = "<green>%s has been shown the demo screen";
@@ -197,6 +200,7 @@ public class PurpurConfig {
public static String pingCommandOutput = "<green>%s's ping is %sms";
@@ -197,6 +202,7 @@ public class PurpurConfig {
afkBroadcastBack = getString("settings.messages.afk-broadcast-back", afkBroadcastBack);
afkBroadcastUseDisplayName = getBoolean("settings.messages.afk-broadcast-use-display-name", afkBroadcastUseDisplayName);
afkTabListPrefix = MiniMessage.miniMessage().serialize(MiniMessage.miniMessage().deserialize(getString("settings.messages.afk-tab-list-prefix", afkTabListPrefix)));
Expand Down

0 comments on commit 04ae20f

Please sign in to comment.