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/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java index 7b27575ee14ea30c104be2a231d378f69f7bd347..91eea05a68d0ace8678fc5071e67cedb18a4386b 100644 --- a/src/main/java/net/minecraft/commands/Commands.java +++ b/src/main/java/net/minecraft/commands/Commands.java @@ -248,6 +248,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 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..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,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 +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]; + 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 AFK: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 39428db6c83527c085f4dcb012b4da297840bca6..81834489ee488be58b70dae0336645e4147d8dae 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -2271,6 +2271,8 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl } } + public static final Map afkCooldown = new java.util.concurrent.ConcurrentHashMap<>(); // Leaf - Improve Purpur AFK system + @Override public void handleChatCommand(ServerboundChatCommandPacket packet) { this.tryHandleChat(packet.command(), () -> { @@ -2291,6 +2293,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 = 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(), command1, new LazyPlayerSet(this.server)); this.cserver.getPluginManager().callEvent(event); @@ -2328,11 +2356,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); command = event.getMessage().substring(1); diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java index bc7dd0ceea8bb304fb4370084bf19f29c1df0828..3ac1446ed791e06e2bbf226b925890ac4d1a16de 100644 --- a/src/main/java/net/minecraft/server/players/PlayerList.java +++ b/src/main/java/net/minecraft/server/players/PlayerList.java @@ -756,6 +756,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()); // 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 654e838d9a77e0dd8fe9fb1d4804785ad0f463c8..5b9b9830eadf133e5ee822d6d6a0d649e08a53ed 100644 --- a/src/main/java/org/purpurmc/purpur/PurpurConfig.java +++ b/src/main/java/org/purpurmc/purpur/PurpurConfig.java @@ -178,9 +178,14 @@ public class PurpurConfig { public static String cannotRideMob = "You cannot mount that mob"; public static String afkBroadcastAway = "%s is now AFK"; public static String afkBroadcastBack = "%s is no longer AFK"; + public static String afkTitleAway = "AFK: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 = "You need to wait %time%s to use /afk."; + // Leaf end - Improve Purpur AFK system public static String creditsCommandOutput = "%s has been shown the end credits"; public static String demoCommandOutput = "%s has been shown the demo screen"; public static String pingCommandOutput = "%s's ping is %sms"; @@ -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); // 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..1b8eee9cce3ae069be7c97bd0840338e9287ce4e --- /dev/null +++ b/src/main/java/org/purpurmc/purpur/command/AFKCommand.java @@ -0,0 +1,37 @@ +package org.purpurmc.purpur.command; + +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.commands.arguments.EntityArgument; +import net.minecraft.server.level.ServerPlayer; + +import java.util.Collection; +import java.util.Collections; + +public class AFKCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(Commands.literal("afk") + .requires((listener) -> listener.hasPermission(2, "bukkit.command.afk")) + .executes((context) -> execute(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException()))) + .then(Commands.argument("targets", EntityArgument.players()) + .requires(listener -> listener.hasPermission(2, "bukkit.command.afk.other")) + .executes((context) -> execute(context.getSource(), EntityArgument.getPlayers(context, "targets"))) + ) + ); + } + + private static int execute(CommandSourceStack sender, Collection targets) { + for (ServerPlayer player : targets) { + boolean afk = player.isCommandAfk + ? !player.commandAfkStatus + : !player.isAfk(); + + if (afk) player.setAfk(true); + + player.isCommandAfk = false; + } + + return targets.size(); + } +}