9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-06 15:51:31 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0043-Improve-Purpur-AFK-system.patch
Dreeam 387597f347 Updated Upstream (Leaves)
Upstream has released updates that appear to apply and compile correctly

Leaves Changes:
LeavesMC/Leaves@88819fe8 Add mc-old hopper suck-in behavior (#395)
LeavesMC/Leaves@7394e8dd Fix papermc repo
LeavesMC/Leaves@85c7bf11 Remove cache-world-generator-sea-level (#392)
LeavesMC/Leaves@00798036 init 1.21.4, and boom!
LeavesMC/Leaves@91fc24da build change, but weight not work
LeavesMC/Leaves@4ccdf459 just work
LeavesMC/Leaves@05ee2e36 Build changes, and delete timings
LeavesMC/Leaves@fcc859dc Fix API patches (#406)
LeavesMC/Leaves@6a1259df 0006/0129
LeavesMC/Leaves@3e3b05df 0009/0129
LeavesMC/Leaves@c3255c4f 0011/0129
LeavesMC/Leaves@6284c7b6 0018/0129
LeavesMC/Leaves@7abdc88c 0030/0129
LeavesMC/Leaves@4d119ff9 0035/0129
LeavesMC/Leaves@60baed99 0043/0129
LeavesMC/Leaves@dc319d5b 0048/0129
LeavesMC/Leaves@73a505d5 0049/0129
LeavesMC/Leaves@016b29dd 0057/0129
LeavesMC/Leaves@c9cf5af8 0065/0129
LeavesMC/Leaves@330b79ff 0086/0129 (#408)
LeavesMC/Leaves@06c1d946 0087/0129
LeavesMC/Leaves@bf4bc284 0091/0129
LeavesMC/Leaves@102a3b70 0097/0129
LeavesMC/Leaves@53b43fed 0101/0129
LeavesMC/Leaves@892f3925 102/129
LeavesMC/Leaves@08c3043a 0107/0129
LeavesMC/Leaves@48764d8e 0112/0129
LeavesMC/Leaves@8380feff 0118/0129
LeavesMC/Leaves@e51603db 0129/0129, 100% patched
LeavesMC/Leaves@ef851152 fix some
LeavesMC/Leaves@9b7c6e88 server work
LeavesMC/Leaves@272b7dcb Protocol... (#409)
LeavesMC/Leaves@7be1bc97 Make jade better
LeavesMC/Leaves@5350f6ea Make action work
LeavesMC/Leaves@f07c26c8 fix action jar
2025-02-06 23:28:14 -05:00

184 lines
12 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 967c01f9e40c2c73f266de281425c918472cf66e..d8464f77c54f547161356b78e76429923937b4a7 100644
--- a/net/minecraft/commands/Commands.java
+++ b/net/minecraft/commands/Commands.java
@@ -242,6 +242,7 @@ public class Commands {
StopCommand.register(this.dispatcher);
TransferCommand.register(this.dispatcher);
WhitelistCommand.register(this.dispatcher);
+ 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 dfb4524d80f642eff1b146dd2fbfa07f21d844c6..2c67693cc3781eb5aee10b4dfb9617cbe107e922 100644
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
@@ -2460,6 +2460,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) {
@@ -2497,6 +2501,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 5cd5234e9f2b8a17b6f9fdaa62e8fc0a9aa6331b..44f2182b5d78b0e51e8f5253fa9c89ea687b72df 100644
--- a/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2260,6 +2260,8 @@ public class ServerGamePacketListenerImpl
}
}
+ public static final Map<java.util.UUID, Long> afkCooldown = new it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap<>(); // Leaf - Improve Purpur AFK system
+
@Override
public void handleChatCommand(ServerboundChatCommandPacket packet) {
this.tryHandleChat(packet.command(), () -> {
@@ -2280,6 +2282,32 @@ public class ServerGamePacketListenerImpl
LOGGER.info("{} issued server command: {}", this.player.getScoreboardName(), prefixedCommand);
}
+ // 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) {
+ 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;
+ } else {
+ afkCooldown.put(uuid, currentTime);
+ }
+ }
+ }
+ // Leaf end - Improve Purpur AFK system
+
PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(this.getCraftPlayer(), prefixedCommand, new LazyPlayerSet(this.server));
this.cserver.getPluginManager().callEvent(event);
@@ -2317,11 +2345,37 @@ public class ServerGamePacketListenerImpl
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
LOGGER.info("{} issued server command: {}", this.player.getScoreboardName(), 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) {
+ 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;
+ } 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);
command = event.getMessage().substring(1);
diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java
index 29275d0fd6b3d320d5e444fb32aa4004f4370720..2d8c20ddd675173e9ea5734cfde6687ac042e01d 100644
--- a/net/minecraft/server/players/PlayerList.java
+++ b/net/minecraft/server/players/PlayerList.java
@@ -668,6 +668,7 @@ public abstract class PlayerList {
org.leavesmc.leaves.protocol.core.LeavesProtocolManager.handlePlayerLeave(player); // Leaves - protocol
// 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 b8c8806789bd0060cd3faee5815bbf25c8715a9b..c726406ac67980f0403cc524d96f08916218667a 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)));