9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0130-Improve-Purpur-AFK-system.patch
Dreeam 3af60cbe46 Updated Upstream (Paper)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@de410d13 Fix reobf mappings regression in GameRules.Type (#12437)
PaperMC/Paper@33e8928f Add support for bonus chest configuration in WorldCreator (#12344)
PaperMC/Paper@723b511f Clone exit location passed to teleport event (#12354)
PaperMC/Paper@ed322043 Clone blockpos in InsideBlockEffectApplier record
PaperMC/Paper@6b4ad082 Add PlayerRespawnEvent#isMissingRespawnBlock (#12422)
PaperMC/Paper@c0bd5688 Add logic for Human canUseEquipmentSlot (#12433)
2025-04-14 21:30:21 -04:00

198 lines
13 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Sat, 2 Mar 2024 18:22:15 -0500
Subject: [PATCH] Improve Purpur AFK system
AFK command & command cooldown
AFK title message
diff --git a/net/minecraft/commands/Commands.java b/net/minecraft/commands/Commands.java
index bcecf48b43eef377354e32695d4258ea8020f73d..aba02a7e3139030050c3c61aabf7708c6c088a68 100644
--- a/net/minecraft/commands/Commands.java
+++ b/net/minecraft/commands/Commands.java
@@ -247,6 +247,7 @@ public class Commands {
StopCommand.register(this.dispatcher);
TransferCommand.register(this.dispatcher);
WhitelistCommand.register(this.dispatcher);
+ if (org.dreeam.leaf.config.modules.gameplay.AfkCommand.enabled) org.purpurmc.purpur.command.AFKCommand.register(this.dispatcher); // Leaf - Improve Purpur AFK system
org.purpurmc.purpur.command.CreditsCommand.register(this.dispatcher); // Purpur - Add credits command
org.purpurmc.purpur.command.DemoCommand.register(this.dispatcher); // Purpur - Add demo command
org.purpurmc.purpur.command.PingCommand.register(this.dispatcher); // Purpur - Add ping command
diff --git a/net/minecraft/server/level/ServerPlayer.java b/net/minecraft/server/level/ServerPlayer.java
index 7746550258a1c13c042b946bdfe6c1410dcee4ad..2801bf484ea1d06dd8924b920e5b7e3640d0b528 100644
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
@@ -2387,6 +2387,10 @@ public class ServerPlayer extends Player implements ca.spottedleaf.moonrise.patc
// Purpur start - AFK API
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) {
@@ -2424,6 +2428,18 @@ public class ServerPlayer extends Player implements ca.spottedleaf.moonrise.patc
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(":");
+ 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);
+ // 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/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 956a19d137243baddcc44927b3ae77697ca0bfdf..41251d29a881d31283cb647a7d74fa402c1b422f 100644
--- a/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2329,6 +2329,7 @@ public class ServerGamePacketListenerImpl
@Override
public void handleChatCommand(ServerboundChatCommandPacket packet) {
+ final boolean isAfkCommandCooldown = this.performAfkCooldown(packet.command()); // Leaf - Improve Purpur AFK system
this.tryHandleChat(packet.command(), () -> {
// CraftBukkit start - SPIGOT-7346: Prevent disconnected players from executing commands
if (this.player.hasDisconnected()) {
@@ -2337,7 +2338,7 @@ public class ServerGamePacketListenerImpl
// CraftBukkit end
this.performUnsignedChatCommand(packet.command());
this.detectRateSpam("/" + packet.command()); // Spigot
- }, true); // CraftBukkit - sync commands
+ }, true, isAfkCommandCooldown); // CraftBukkit - sync commands // Leaf - Improve Purpur AFK system
}
private void performUnsignedChatCommand(String command) {
@@ -2370,6 +2371,7 @@ public class ServerGamePacketListenerImpl
public void handleSignedChatCommand(ServerboundChatCommandSignedPacket packet) {
Optional<LastSeenMessages> optional = this.unpackAndApplyLastSeen(packet.lastSeenMessages());
if (!optional.isEmpty()) {
+ final boolean isAfkCommandCooldown = this.performAfkCooldown(packet.command()); // Leaf - Improve Purpur AFK system
this.tryHandleChat(packet.command(), () -> {
// CraftBukkit start - SPIGOT-7346: Prevent disconnected players from executing commands
if (this.player.hasDisconnected()) {
@@ -2378,7 +2380,7 @@ public class ServerGamePacketListenerImpl
// CraftBukkit end
this.performSignedChatCommand(packet, optional.get());
this.detectRateSpam("/" + packet.command()); // Spigot
- }, true); // CraftBukkit - sync commands
+ }, true, isAfkCommandCooldown); // CraftBukkit - sync commands // Leaf - Improve Purpur AFK system
}
}
@@ -2485,12 +2487,17 @@ public class ServerGamePacketListenerImpl
return dispatcher.parse(command, this.player.createCommandSourceStack());
}
- private void tryHandleChat(String message, Runnable handler, boolean sync) { // CraftBukkit
+ // Leaf start - Improve Purpur AFK system
+ private void tryHandleChat(String message, Runnable handler, boolean sync) {
+ tryHandleChat(message, handler, sync, false);
+ }
+ private void tryHandleChat(String message, Runnable handler, boolean sync, boolean afkCommandCooldown) { // CraftBukkit
+ // Leaf end - Improve Purpur AFK system
if (isChatMessageIllegal(message)) {
this.disconnectAsync(Component.translatable("multiplayer.disconnect.illegal_characters"), org.bukkit.event.player.PlayerKickEvent.Cause.ILLEGAL_CHARACTERS); // Paper - add proper async disconnect
} else if (this.player.isRemoved() || this.player.getChatVisibility() == ChatVisiblity.HIDDEN) { // CraftBukkit - dead men tell no tales
this.send(new ClientboundSystemChatPacket(Component.translatable("chat.disabled.options").withStyle(ChatFormatting.RED), false));
- } else if (player.didPlayerJoinEvent) { // Gale - EMC - do not process chat/commands before player has joined
+ } else if (player.didPlayerJoinEvent && !afkCommandCooldown) { // Gale - EMC - do not process chat/commands before player has joined // Leaf - Improve Purpur AFK system - don't process afk command if in cooldown
this.player.resetLastActionTime();
// CraftBukkit start
if (sync) {
@@ -2502,6 +2509,40 @@ public class ServerGamePacketListenerImpl
}
}
+ // Leaf start - Improve Purpur AFK system
+ public static final Map<java.util.UUID, Long> afkCooldown = new it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap<>();
+ private boolean performAfkCooldown(String command) {
+ if (!org.dreeam.leaf.config.modules.gameplay.AfkCommand.enabled || !"afk".equals(command)) {
+ return false;
+ }
+
+ this.player.commandAfkStatus = this.player.isAfk();
+ this.player.isCommandAfk = true;
+
+ if (org.purpurmc.purpur.PurpurConfig.afkCommandCooldown <= 0) {
+ return false;
+ }
+
+ java.util.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 true;
+ } else {
+ afkCooldown.put(uuid, currentTime);
+ return false;
+ }
+ }
+ // Leaf end - Improve Purpur AFK system
+
private Optional<LastSeenMessages> unpackAndApplyLastSeen(LastSeenMessages.Update update) {
synchronized (this.lastSeenMessages) {
Optional var10000;
diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java
index 82117c8619b184017bb4448bf2e30f817abd368a..bbdb8e00ac0fdcc4f1b94faf8e2cd13597e4e2a0 100644
--- a/net/minecraft/server/players/PlayerList.java
+++ b/net/minecraft/server/players/PlayerList.java
@@ -524,6 +524,7 @@ public abstract class PlayerList {
public @Nullable net.kyori.adventure.text.Component remove(ServerPlayer player, net.kyori.adventure.text.Component leaveMessage) {
// Paper end - Fix kick event leave message not being sent
org.purpurmc.purpur.task.BossBarTask.removeFromAll(player.getBukkitEntity()); // Purpur - Implement TPSBar
+ net.minecraft.server.network.ServerGamePacketListenerImpl.afkCooldown.remove(player.getBukkitEntity().getUniqueId()); // Leaf - Improve Purpur AFK system
ServerLevel serverLevel = player.serverLevel();
player.awardStat(Stats.LEAVE_GAME);
// CraftBukkit start - Quitting must be before we do final save of data, in case plugins need to modify it
diff --git a/org/purpurmc/purpur/PurpurConfig.java b/org/purpurmc/purpur/PurpurConfig.java
index 23e959e348a1875d63b5862411dae162625a1aac..78e90d4e173c52c0dadc6c8e00326829a7c57575 100644
--- a/org/purpurmc/purpur/PurpurConfig.java
+++ b/org/purpurmc/purpur/PurpurConfig.java
@@ -175,6 +175,11 @@ 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";
+ // Leaf start - Improve Purpur AFK system
+ public static String afkTitleAway = "<gold><bold>AFK:<red>You are now AFK...:10:70:20";
+ 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 boolean afkBroadcastUseDisplayName = false;
public static String afkTabListPrefix = "[AFK] ";
public static String afkTabListSuffix = "";
@@ -194,6 +199,15 @@ 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);
+ // Leaf start - Improve Purpur AFK system
+ afkTitleAway = getString("settings.messages.afk-title-away", afkTitleAway);
+ if (afkTitleAway.split(":").length != 5) {
+ Bukkit.getLogger().log(Level.SEVERE, "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");
+ Bukkit.getLogger().log(Level.SEVERE, "'Title:Sub Title:Title Fade In Ticks:Title Stay Ticks:Title Fade Out Ticks', split with :");
+ }
+ 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
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)));