Skip to content

Commit

Permalink
Update Improve Purpur AFK system
Browse files Browse the repository at this point in the history
More configurable afk away title & Remove player from afk map when quit
  • Loading branch information
Dreeam-qwq committed Jun 9, 2024
1 parent dda5812 commit dd62783
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
61 changes: 37 additions & 24 deletions patches/server/0073-Improve-Purpur-AFK-system.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Sat, 2 Mar 2024 18:22:15 -0500
Subject: [PATCH] Improve Purpur AFK system

TODO - Dreeam: Configurable afk title time

AFK command & command cooldown
AFK title message

Expand All @@ -21,7 +19,7 @@ index edbdcdac30a9c80849045c5225bb1d6bdd091e2d..f1ddbbf41a7232ad3c432dfef9a84209
org.purpurmc.purpur.command.DemoCommand.register(this.dispatcher); // Purpur
org.purpurmc.purpur.command.PingCommand.register(this.dispatcher); // Purpur
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index 6568b4541a81d52cc81729eb8590cf3723289bac..54e872d7279a56ce1c1ff0310ed302f0012e5d3e 100644
index 6568b4541a81d52cc81729eb8590cf3723289bac..f8b6d0c611effe446c20e4a9dd5fd3f082571c43 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -2277,6 +2277,8 @@ public class ServerPlayer extends Player {
Expand All @@ -33,25 +31,36 @@ index 6568b4541a81d52cc81729eb8590cf3723289bac..54e872d7279a56ce1c1ff0310ed302f0

@Override
public void setAfk(boolean afk) {
@@ -2314,6 +2316,9 @@ public class ServerPlayer extends Player {
@@ -2314,6 +2316,20 @@ public class ServerPlayer extends Player {
String prefix = (split.length > 0 ? split[0] : "").replace(org.purpurmc.purpur.PurpurConfig.afkTabListPrefix, "");
String suffix = (split.length > 1 ? split[1] : "").replace(org.purpurmc.purpur.PurpurConfig.afkTabListSuffix, "");
if (afk) {
+ net.kyori.adventure.title.Title tile = net.kyori.adventure.title.Title.title(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(org.purpurmc.purpur.PurpurConfig.afkTitleAway), net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(org.purpurmc.purpur.PurpurConfig.afkSubTitleAway), net.kyori.adventure.title.Title.Times.times(net.kyori.adventure.util.Ticks.duration(10), net.kyori.adventure.util.Ticks.duration(70), net.kyori.adventure.util.Ticks.duration(20)));
+ getBukkitEntity().showTitle(tile);
+ String[] rawTitle = org.purpurmc.purpur.PurpurConfig.afkTitleAway.split(":");
+ if (rawTitle.length == 5) {
+ String title = rawTitle[0];
+ String subTitle = rawTitle[1];
+ long fadeInTicks = Long.parseLong(rawTitle[2]);
+ long stayTicks = Long.parseLong(rawTitle[3]);
+ long fadeOutTicks = Long.parseLong(rawTitle[4]);
+ net.kyori.adventure.title.Title tile = net.kyori.adventure.title.Title.title(net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(title), net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(subTitle), net.kyori.adventure.title.Title.Times.times(net.kyori.adventure.util.Ticks.duration(fadeInTicks), net.kyori.adventure.util.Ticks.duration(stayTicks), net.kyori.adventure.util.Ticks.duration(fadeOutTicks)));
+ getBukkitEntity().showTitle(tile);
+ } else {
+ LOGGER.error("You put wrong format of afk-title-away in PurpurConfig, it should look like <gold><bold>AFK:<red>You are now AFK...:10:70:20");
+ LOGGER.error("'Title:Sub Title:Title Fade In Ticks:Title Stay Ticks:Title Fade Out Ticks', split with :");
+ }
+
getBukkitEntity().setPlayerListName(org.purpurmc.purpur.PurpurConfig.afkTabListPrefix + prefix + scoreboardName + suffix + org.purpurmc.purpur.PurpurConfig.afkTabListSuffix, true);
} else {
getBukkitEntity().setPlayerListName(prefix + scoreboardName + suffix);
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 70bf701ae9aea5b6f322db5aa2ae17009d68a807..85eba54ecdcb678eedcfdcc0a484ae2155f2bb15 100644
index 70bf701ae9aea5b6f322db5aa2ae17009d68a807..f734a6d9f69e5ba717e7564f5d89f2f863ef894d 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2264,8 +2264,33 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
@@ -2264,8 +2264,27 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
}
}

+ private final Map<UUID, Long> cooldown = new java.util.concurrent.ConcurrentHashMap<>(); // Purpur
+ public static final Map<UUID, Long> afkCooldown = new java.util.concurrent.ConcurrentHashMap<>(); // Purpur
+
@Override
public void handleChatCommand(ServerboundChatCommandPacket packet) {
Expand All @@ -62,35 +71,40 @@ index 70bf701ae9aea5b6f322db5aa2ae17009d68a807..85eba54ecdcb678eedcfdcc0a484ae21
+ if (org.purpurmc.purpur.PurpurConfig.afkCommandCooldown > 0) {
+ UUID uuid = player.getUUID();
+ long currentTime = System.nanoTime();
+ if (cooldown.containsKey(uuid) && (currentTime - cooldown.get(uuid)) / 1000000000 <= org.purpurmc.purpur.PurpurConfig.afkCommandCooldown) {
+ if (afkCooldown.containsKey(uuid) && (currentTime - afkCooldown.get(uuid)) / 1_000_000_000 <= 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 - (currentTime - cooldown.get(uuid)) / 1000000000))));
+ 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 - (currentTime - afkCooldown.get(uuid)) / 1_000_000_000))));
+ return;
+ } else {
+ cooldown.put(uuid, currentTime);
+ afkCooldown.put(uuid, currentTime);
+ }
+ // Dreeam - is this necessary?
+ /*
+ if (cooldown.size() > 200) {
+ cooldown = new java.util.concurrent.ConcurrentHashMap<>();
+ }
+ */
+ }
+ }
+ // 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/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index 25c7f47382f8a9949e2785c36f67dcf455d33f5a..6240ca2e2a9076398c5404679d363b019f493943 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -624,6 +624,7 @@ public abstract class PlayerList {
top.leavesmc.leaves.protocol.core.LeavesProtocolManager.handlePlayerLeave(entityplayer); // Leaves - protocol
// Paper end - Fix kick event leave message not being sent
org.purpurmc.purpur.task.BossBarTask.removeFromAll(entityplayer.getBukkitEntity()); // Purpur
+ net.minecraft.server.network.ServerGamePacketListenerImpl.afkCooldown.remove(entityplayer.getBukkitEntity().getUniqueId()); // Puprur
ServerLevel worldserver = entityplayer.serverLevel();

entityplayer.awardStat(Stats.LEAVE_GAME);
diff --git a/src/main/java/org/purpurmc/purpur/PurpurConfig.java b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
index e45c11bf2c42e6d57f803349d8ca6936ac98c995..e6d44744b2a560789cde22a3b4b87eef7874c701 100644
index e45c11bf2c42e6d57f803349d8ca6936ac98c995..03fc2ee4ed8d5b03b382c078fee12101f7940152 100644
--- a/src/main/java/org/purpurmc/purpur/PurpurConfig.java
+++ b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
@@ -176,9 +176,13 @@ public class PurpurConfig {
@@ -176,9 +176,12 @@ 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 String afkTitleAway = "<gold><bold>AFK:<red>You are now AFK...:10:70:20";
public static boolean afkBroadcastUseDisplayName = false;
public static String afkTabListPrefix = "[AFK] ";
public static String afkTabListSuffix = "";
Expand All @@ -99,12 +113,11 @@ index e45c11bf2c42e6d57f803349d8ca6936ac98c995..e6d44744b2a560789cde22a3b4b87eef
public static String creditsCommandOutput = "<green>%s has been shown the end credits";
public static String demoCommandOutput = "<green>%s has been shown the demo screen";
public static String pingCommandOutput = "<green>%s's ping is %sms";
@@ -195,9 +199,13 @@ public class PurpurConfig {
@@ -195,9 +198,12 @@ public class PurpurConfig {
cannotRideMob = getString("settings.messages.cannot-ride-mob", cannotRideMob);
afkBroadcastAway = getString("settings.messages.afk-broadcast-away", afkBroadcastAway);
afkBroadcastBack = getString("settings.messages.afk-broadcast-back", afkBroadcastBack);
+ afkTitleAway = getString("settings.messages.afk-title-away", afkTitleAway);
+ afkSubTitleAway = getString("settings.messages.afk-sub-title-away", afkSubTitleAway);
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)));
afkTabListSuffix = MiniMessage.miniMessage().serialize(MiniMessage.miniMessage().deserialize(getString("settings.messages.afk-tab-list-suffix", afkTabListSuffix)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ index a83fb872a1d186a3547c12f518e1bb0067fffaf7..fbe299dc0e2d0903075f64f199a498c3

// Paper start - Add setting for proxy online mode status
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index 54e872d7279a56ce1c1ff0310ed302f0012e5d3e..8cea11444cfb711c60ee6af84996c29100541cfa 100644
index f8b6d0c611effe446c20e4a9dd5fd3f082571c43..5c678ed49ef4fb04d4d40c60858748576fc5e9ab 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -2262,7 +2262,7 @@ public class ServerPlayer extends Player {
Expand Down Expand Up @@ -148,10 +148,10 @@ index 4a6bb425902b780ddfcc505e71d4ff70ff0babb9..dd6fa971c0584fe3b0c7814cfc11850d
if (packet == null || this.processedDisconnect) { // Spigot
return;
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index 25c7f47382f8a9949e2785c36f67dcf455d33f5a..b79c9c983dc960732927bbc13017e7eb028e485b 100644
index 6240ca2e2a9076398c5404679d363b019f493943..82072ff8ad54a98d0962d7d5448ae2b9285a1445 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -1533,7 +1533,7 @@ public abstract class PlayerList {
@@ -1534,7 +1534,7 @@ public abstract class PlayerList {
// Paper end
boolean flag = this.verifyChatTrusted(message);

Expand All @@ -160,7 +160,7 @@ index 25c7f47382f8a9949e2785c36f67dcf455d33f5a..b79c9c983dc960732927bbc13017e7eb
OutgoingChatMessage outgoingchatmessage = OutgoingChatMessage.create(message);
boolean flag1 = false;

@@ -1562,6 +1562,7 @@ public abstract class PlayerList {
@@ -1563,6 +1563,7 @@ public abstract class PlayerList {
}

public boolean verifyChatTrusted(PlayerChatMessage message) { // Paper - private -> public
Expand Down
4 changes: 2 additions & 2 deletions patches/server/0079-Block-log4j-rce-exploit-in-chat.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Block log4j rce exploit in chat


diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 85eba54ecdcb678eedcfdcc0a484ae2155f2bb15..0639def76c50fc9f68a058d781ef86383a01a128 100644
index f734a6d9f69e5ba717e7564f5d89f2f863ef894d..9370b70300df07f5050e01b6d3df867f5ba58a9e 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2234,6 +2234,9 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
Expand All @@ -18,7 +18,7 @@ index 85eba54ecdcb678eedcfdcc0a484ae2155f2bb15..0639def76c50fc9f68a058d781ef8638
if (ServerGamePacketListenerImpl.isChatMessageIllegal(packet.message())) {
this.disconnect(Component.translatable("multiplayer.disconnect.illegal_characters"), org.bukkit.event.player.PlayerKickEvent.Cause.ILLEGAL_CHARACTERS); // Paper - add cause
} else {
@@ -2409,6 +2412,15 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
@@ -2403,6 +2406,15 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
}
}

Expand Down

0 comments on commit dd62783

Please sign in to comment.