Skip to content

Commit

Permalink
Update, cleanup Purpur afk improvement patch & Fix cooldown bypass is…
Browse files Browse the repository at this point in the history
…sue for signed chat command
  • Loading branch information
Dreeam-qwq committed Sep 21, 2024
1 parent b67c6bf commit d233f6b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 35 deletions.
129 changes: 96 additions & 33 deletions patches/server/0065-Improve-Purpur-AFK-system.patch
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,37 @@ AFK command & command cooldown
AFK title message

diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
index b6713361cd104786f52bd880418b8653e4126fda..94bb40c9932b0b7dd9fb8af680b63d139ae18e3a 100644
index b6713361cd104786f52bd880418b8653e4126fda..11e94766b487ee8e112813cae0561b84d16f4f8a 100644
--- a/src/main/java/net/minecraft/commands/Commands.java
+++ b/src/main/java/net/minecraft/commands/Commands.java
@@ -246,6 +246,7 @@ public class Commands {
StopCommand.register(this.dispatcher);
TransferCommand.register(this.dispatcher);
WhitelistCommand.register(this.dispatcher);
+ org.purpurmc.purpur.command.AFKCommand.register(this.dispatcher); // Purpur
+ org.purpurmc.purpur.command.AFKCommand.register(this.dispatcher); // Leaf - Improve Purpur AFK system
org.purpurmc.purpur.command.CreditsCommand.register(this.dispatcher); // Purpur
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 7c6bda95b8b08cc70182f19cf0b991f78d28c235..9fb0372511710cb27ffb18f9879e3ff3a0384427 100644
index 7c6bda95b8b08cc70182f19cf0b991f78d28c235..79df52237b7a5822e0cbaff015ccd193a8195980 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -2414,6 +2414,8 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player imple
@@ -2414,6 +2414,10 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player imple

// Purpur Start
private boolean isAfk = false;
+ // Leaf sart - Improve Purpur AFK system
+ public boolean isCommandAfk = false;
+ public boolean commandAfkStatus = false;
+ // Leaf end - Improve Purpur AFK system

@Override
public void setAfk(boolean afk) {
@@ -2451,6 +2453,20 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player imple
@@ -2451,6 +2455,22 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player imple
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) {
+ // Leaf start - Improve Purpur AFK system
+ String[] rawTitle = org.purpurmc.purpur.PurpurConfig.afkTitleAway.split(":");
+ if (rawTitle.length == 5) {
+ String title = rawTitle[0];
Expand All @@ -48,91 +51,148 @@ index 7c6bda95b8b08cc70182f19cf0b991f78d28c235..9fb0372511710cb27ffb18f9879e3ff3
+ 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 :");
+ }
+ // Leaf end - Improve Purpur AFK system
+
getBukkitEntity().setPlayerListName(org.purpurmc.purpur.PurpurConfig.afkTabListPrefix + prefix + scoreboardName + suffix + org.purpurmc.purpur.PurpurConfig.afkTabListSuffix, true);
} else {
getBukkitEntity().setPlayerListName(prefix + scoreboardName + suffix, true);
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 33b2234970e3b78ccd4b8da184bbdd62b50d3afa..81b0f8c986131335533f6c81f9150b8c6019f8d7 100644
index 33b2234970e3b78ccd4b8da184bbdd62b50d3afa..4cf8e0a2817795cc3c4ed30a0bf1199dd0e40c83 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2276,8 +2276,28 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
@@ -2276,6 +2276,8 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
}
}

+ public static final Map<UUID, Long> afkCooldown = new java.util.concurrent.ConcurrentHashMap<>(); // Purpur
+ public static final Map<UUID, Long> afkCooldown = new java.util.concurrent.ConcurrentHashMap<>(); // Leaf - Improve Purpur AFK system
+
@Override
public void handleChatCommand(ServerboundChatCommandPacket packet) {
+ // Purpur start
+ if (this.server.getPlayerIdleTimeout() > 0 && packet.command().equals("afk")) {
+ player.commandAfkStatus = player.isAfk();
+ player.isCommandAfk = true;
this.tryHandleChat(packet.command(), () -> {
@@ -2296,6 +2298,32 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
ServerGamePacketListenerImpl.LOGGER.info(this.player.getScoreboardName() + " issued server command: " + command1);
}

+ // Leaf start - Improve Purpur AFK system
+ if (command.equals("afk")) {
+ this.player.commandAfkStatus = this.player.isAfk();
+ this.player.isCommandAfk = true;
+
+ if (org.purpurmc.purpur.PurpurConfig.afkCommandCooldown > 0) {
+ UUID uuid = player.getUUID();
+ UUID uuid = this.player.getUUID();
+ Long cooldown = afkCooldown.get(uuid);
+ long currentTime = System.nanoTime();
+ if (afkCooldown.containsKey(uuid) && (currentTime - afkCooldown.get(uuid)) / 1_000_000_000 <= org.purpurmc.purpur.PurpurConfig.afkCommandCooldown) {
+
+ if (cooldown != null && (currentTime - cooldown) / 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 - afkCooldown.get(uuid)) / 1_000_000_000))));
+
+ if (msg != null && !msg.isEmpty()) {
+ net.kyori.adventure.text.Component message = net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(msg.replace("%time%", String.valueOf(org.purpurmc.purpur.PurpurConfig.afkCommandCooldown - (currentTime - cooldown) / 1_000_000_000)));
+ this.player.sendMessage(message);
+ }
+
+ return;
+ } else {
+ afkCooldown.put(uuid, currentTime);
+ }
+ }
+ }
+ // Purpur end
this.tryHandleChat(packet.command(), () -> {
// CraftBukkit start - SPIGOT-7346: Prevent disconnected players from executing commands
if (this.player.hasDisconnected()) {
+ // Leaf end - Improve Purpur AFK system
+
PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(this.getCraftPlayer(), command1, new LazyPlayerSet(this.server));
this.cserver.getPluginManager().callEvent(event);

@@ -2333,11 +2361,37 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl

private void performSignedChatCommand(ServerboundChatCommandSignedPacket packet, LastSeenMessages lastSeenMessages) {
// CraftBukkit start
- String command = "/" + packet.command();
+ String command = "/" + packet.command(); // Leaf start - Improve Purpur AFK system - diff on change
if (org.spigotmc.SpigotConfig.logCommands) { // Paper - Add missing SpigotConfig logCommands check
ServerGamePacketListenerImpl.LOGGER.info(this.player.getScoreboardName() + " issued server command: " + command);
} // Paper - Add missing SpigotConfig logCommands check

+ // Leaf start - Improve Purpur AFK system
+ if (command.equals("/afk")) {
+ this.player.commandAfkStatus = this.player.isAfk();
+ this.player.isCommandAfk = true;
+
+ if (org.purpurmc.purpur.PurpurConfig.afkCommandCooldown > 0) {
+ UUID uuid = this.player.getUUID();
+ Long cooldown = afkCooldown.get(uuid);
+ long currentTime = System.nanoTime();
+
+ if (cooldown != null && (currentTime - cooldown) / 1_000_000_000 <= org.purpurmc.purpur.PurpurConfig.afkCommandCooldown) {
+ String msg = org.purpurmc.purpur.PurpurConfig.afkCooldown;
+
+ if (msg != null && !msg.isEmpty()) {
+ net.kyori.adventure.text.Component message = net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(msg.replace("%time%", String.valueOf(org.purpurmc.purpur.PurpurConfig.afkCommandCooldown - (currentTime - cooldown) / 1_000_000_000)));
+ this.player.sendMessage(message);
+ }
+
+ return;
+ } else {
+ afkCooldown.put(uuid, currentTime);
+ }
+ }
+ }
+ // Leaf end - Improve Purpur AFK system
+
PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(this.getCraftPlayer(), command, new LazyPlayerSet(this.server));
this.cserver.getPluginManager().callEvent(event);

diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index 5cffe034571ef1ddf868d22fc38b1a8c2a1500f1..714e0bf56ee866b4ba00953074bec9642cc5f08e 100644
index f073b87e6b214e08672980f8f229cd436a742def..5d81f06c981128e4199555c1378345dccf4682b2 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -759,6 +759,7 @@ public abstract class PlayerList {
org.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
+ net.minecraft.server.network.ServerGamePacketListenerImpl.afkCooldown.remove(entityplayer.getBukkitEntity().getUniqueId()); // Leaf - Improve Purpur AFK system
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 69832f436254d975a77e251e6ae5de2e81f92750..0715ff8e271400ae59fd533883cc0e7dcee83357 100644
index 69832f436254d975a77e251e6ae5de2e81f92750..0a6aa10b112cf24a490ac4350f04a65e03fbb692 100644
--- a/src/main/java/org/purpurmc/purpur/PurpurConfig.java
+++ b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
@@ -178,9 +178,12 @@ public class PurpurConfig {
@@ -178,9 +178,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:<red>You are now AFK...:10:70:20";
+ public static String afkTitleAway = "<gold><bold>AFK:<red>You are now AFK...:10:70:20"; // Leaf - Improve Purpur AFK system
public static boolean afkBroadcastUseDisplayName = false;
public static String afkTabListPrefix = "[AFK] ";
public static String afkTabListSuffix = "";
+ // Leaf start - Improve Purpur AFK system
+ public static int afkCommandCooldown = 0;
+ public static String afkCooldown = "<gray>You need to wait %time%s to use /afk.";
+ // Leaf end - Improve Purpur AFK system
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";
@@ -197,9 +200,12 @@ public class PurpurConfig {
@@ -197,9 +202,14 @@ 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);
+ afkTitleAway = getString("settings.messages.afk-title-away", afkTitleAway); // Leaf - Improve Purpur AFK system
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)));
+ // Leaf start - Improve Purpur AFK system
+ afkCommandCooldown = getInt("settings.messages.afk-command-cooldown", afkCommandCooldown);
+ afkCooldown = MiniMessage.miniMessage().serialize(MiniMessage.miniMessage().deserialize(getString("settings.messages.afk-command-cooldown-msg", afkCooldown)));
+ // Leaf end - Improve Purpur AFK system
creditsCommandOutput = getString("settings.messages.credits-command-output", creditsCommandOutput);
demoCommandOutput = getString("settings.messages.demo-command-output", demoCommandOutput);
pingCommandOutput = getString("settings.messages.ping-command-output", pingCommandOutput);
diff --git a/src/main/java/org/purpurmc/purpur/command/AFKCommand.java b/src/main/java/org/purpurmc/purpur/command/AFKCommand.java
new file mode 100644
index 0000000000000000000000000000000000000000..8dca966c25f8e3f5622ef8f5862fdb43eae3813f
index 0000000000000000000000000000000000000000..1b8eee9cce3ae069be7c97bd0840338e9287ce4e
--- /dev/null
+++ b/src/main/java/org/purpurmc/purpur/command/AFKCommand.java
@@ -0,0 +1,34 @@
@@ -0,0 +1,37 @@
+package org.purpurmc.purpur.command;
+
+import com.mojang.brigadier.CommandDispatcher;
Expand All @@ -158,12 +218,15 @@ index 0000000000000000000000000000000000000000..8dca966c25f8e3f5622ef8f5862fdb43
+
+ private static int execute(CommandSourceStack sender, Collection<ServerPlayer> targets) {
+ for (ServerPlayer player : targets) {
+ boolean afk = player.isCommandAfk ? !player.commandAfkStatus : !player.isAfk();
+ if (afk) {
+ player.setAfk(true);
+ }
+ boolean afk = player.isCommandAfk
+ ? !player.commandAfkStatus
+ : !player.isAfk();
+
+ if (afk) player.setAfk(true);
+
+ player.isCommandAfk = false;
+ }
+
+ return targets.size();
+ }
+}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ index 573c380e123473e35c0b72c44b32c8d6ba8e61c6..feacc41ecf7f4028e0a1cce5d2012ced
}

diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 68cc3c4efd7e10a2ddc0576491e9cb5b02fcabe7..5ee48b2347b4d588206d4c4aabd47a3918046973 100644
index df734f214ecfe798f1b9098e4a068d43ade5680c..b7b204d13663d8da9f0a05a3fcd7eb01860d0412 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2921,7 +2921,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
@@ -2955,7 +2955,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
entity.refreshEntityData(ServerGamePacketListenerImpl.this.player);
// SPIGOT-7136 - Allays
if (entity instanceof Allay || entity instanceof net.minecraft.world.entity.animal.horse.AbstractHorse) { // Paper - Fix horse armor desync
Expand Down

0 comments on commit d233f6b

Please sign in to comment.