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/0044-Improve-Purpur-AFK-system.patch
Dreeam 01fa6ac227 Add Leaf Commands (WIP)
* Added Leaf Commands base
* Added WIP /leaf reload
* Added /leaf version
* Change /gale permission to OP as default
2025-02-22 03:15:42 -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 ee7bdfd8f9da8d5989c9cc25f8cbcc94640361c5..fb18f69cb26132fc8c53b185454c6aadb8a5f7e5 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);
+ 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 1e834d51cc57255fd6075c25b05e2b3816bd501b..4af5f13eb9a0c76d037074fa0fd5c1b2061dc59b 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 3d6efee3469302f37e60a9013f6a3adf970a580a..03f3e1de0c5df76a4d14c5dc62647ecfc2b17dce 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)));