From 210b02ff78ed0dff0845b2fea581b53023462a55 Mon Sep 17 00:00:00 2001 From: Taah Date: Fri, 25 Feb 2022 00:37:43 -0800 Subject: [PATCH] Add minimessage support to prefixes --- .../java/dev/plex/command/impl/TagCMD.java | 25 +++++++++++++++---- .../dev/plex/listener/impl/ChatListener.java | 3 ++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/dev/plex/command/impl/TagCMD.java b/src/main/java/dev/plex/command/impl/TagCMD.java index c454998..33a46dc 100644 --- a/src/main/java/dev/plex/command/impl/TagCMD.java +++ b/src/main/java/dev/plex/command/impl/TagCMD.java @@ -7,10 +7,11 @@ import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.source.RequiredCommandSource; import dev.plex.player.PlexPlayer; import dev.plex.rank.enums.Rank; -import dev.plex.util.PlexUtils; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.apache.commons.lang.StringUtils; -import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; @@ -46,12 +47,17 @@ public class TagCMD extends PlexCommand return usage("/tag set "); } String prefix = StringUtils.join(args, " ", 1, args.length); - if (ChatColor.stripColor(prefix).length() > plugin.config.getInt("chat.max-tag-length", 16)) + + Component convertedComponent = removeEvents(componentFromString(prefix)); + convertedComponent = removeEvents(MiniMessage.miniMessage().deserialize(LegacyComponentSerializer.legacySection().serialize(convertedComponent))); + + if (PlainTextComponentSerializer.plainText().serialize(convertedComponent).length() > plugin.config.getInt("chat.max-tag-length", 16)) { return messageComponent("maximumPrefixLength", plugin.config.getInt("chat.max-tag-length", 16)); } - player.setPrefix(prefix); - return messageComponent("prefixSetTo", PlexUtils.colorize(prefix)); + + player.setPrefix(MiniMessage.miniMessage().serialize(convertedComponent)); + return messageComponent("prefixSetTo", MiniMessage.miniMessage().serialize(convertedComponent)); } if (args[0].equalsIgnoreCase("clear")) @@ -76,4 +82,13 @@ public class TagCMD extends PlexCommand } return usage(); } + + private Component removeEvents(Component component) + { + if (component.clickEvent() != null) component = component.clickEvent(null); + if (component.hoverEvent() != null) component = component.hoverEvent(null); + return component; + } } + + diff --git a/src/main/java/dev/plex/listener/impl/ChatListener.java b/src/main/java/dev/plex/listener/impl/ChatListener.java index 1fd3b61..82588b6 100644 --- a/src/main/java/dev/plex/listener/impl/ChatListener.java +++ b/src/main/java/dev/plex/listener/impl/ChatListener.java @@ -10,6 +10,7 @@ import io.papermc.paper.event.player.AsyncChatEvent; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -71,7 +72,7 @@ public class ChatListener extends PlexListener { if (hasPrefix) { - return Component.empty().append(LegacyComponentSerializer.legacyAmpersand().deserialize(prefix)) + return Component.empty().append(MiniMessage.miniMessage().deserialize(prefix)) .append(Component.space()) .append(LegacyComponentSerializer.legacyAmpersand().deserialize("&" + plugin.config.getString("chat.name-color") + LegacyComponentSerializer.legacyAmpersand().serialize(sourceDisplayName))) .append(Component.space())