mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
198 lines
13 KiB
Diff
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 eb600398e4802bb47231bbc0c55fb24ce24a6efb..54cc28bb1693be2077cb30d1dc85f9ae24a6c954 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 f3fb661e2cbf8119264d113a06bcb7bb5150be91..034a946cd545d775188a4375a87c90d253ebdb08 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 0bf9ead58e256dccd3c1e89bd08ebae9d630b49e..3c14d32ec9caf3dba9d99afe86a3ca053de70958 100644
|
|
--- a/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
+++ b/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
@@ -2327,6 +2327,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()) {
|
|
@@ -2335,7 +2336,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) {
|
|
@@ -2368,6 +2369,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()) {
|
|
@@ -2376,7 +2378,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
|
|
}
|
|
}
|
|
|
|
@@ -2483,12 +2485,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) {
|
|
@@ -2500,6 +2507,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 33962beef40e9dcaf09d096735c7966b146665ab..5d8b445d5b2762b2266c3e12b3423b9c2044ac7d 100644
|
|
--- a/net/minecraft/server/players/PlayerList.java
|
|
+++ b/net/minecraft/server/players/PlayerList.java
|
|
@@ -673,6 +673,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 e0dceff32b47b334ddcb76271e3cf3ea0d27e5f1..ba206b753afe26a5b4c6a2fd1fc438d9a581c3dc 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)));
|