From 40e0bb0845c8df934ce760c1f7960ae98eb2f858 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Wed, 27 Nov 2024 19:11:37 -0600 Subject: [PATCH] Add customizable chat format --- .../dev/plex/listener/impl/ChatListener.java | 65 +++++++++---------- server/src/main/resources/config.yml | 4 +- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/server/src/main/java/dev/plex/listener/impl/ChatListener.java b/server/src/main/java/dev/plex/listener/impl/ChatListener.java index eb87efb..4de754f 100644 --- a/server/src/main/java/dev/plex/listener/impl/ChatListener.java +++ b/server/src/main/java/dev/plex/listener/impl/ChatListener.java @@ -10,9 +10,11 @@ import dev.plex.util.minimessage.SafeMiniMessage; import dev.plex.util.redis.MessageUtil; import io.papermc.paper.chat.ChatRenderer; import io.papermc.paper.event.player.AsyncChatEvent; + import java.util.UUID; import java.util.function.BiConsumer; import java.util.function.Supplier; + import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextReplacementConfig; @@ -24,8 +26,7 @@ import org.bukkit.event.EventPriority; import org.jetbrains.annotations.NotNull; @Toggleable("chat.enabled") -public class ChatListener extends PlexListener -{ +public class ChatListener extends PlexListener { public static final TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig .builder() .match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]") @@ -37,12 +38,12 @@ public class ChatListener extends PlexListener public static BiConsumer PRE_RENDERER = ChatListener::defaultChatProcessing; private final PlexChatRenderer renderer = new PlexChatRenderer(); + private static final Component format = SafeMiniMessage.mmDeserialize(plugin.config.getString("chat.format")); + @EventHandler(priority = EventPriority.LOWEST) - public void onChat(AsyncChatEvent event) - { + public void onChat(AsyncChatEvent event) { PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(event.getPlayer().getUniqueId()); - if (plexPlayer.isStaffChat()) - { + if (plexPlayer.isStaffChat()) { String prefix = PlexUtils.mmSerialize(VaultHook.getPrefix(event.getPlayer())); // Don't use PlexPlayer#getPrefix because that returns their custom set prefix and not their group's MessageUtil.sendStaffChat(event.getPlayer(), event.message(), PlexUtils.adminChat(event.getPlayer().getName(), prefix, SafeMiniMessage.mmSerialize(event.message())).toArray(UUID[]::new)); plugin.getServer().getConsoleSender().sendMessage(PlexUtils.messageComponent("adminChatFormat", event.getPlayer().getName(), prefix, SafeMiniMessage.mmSerialize(event.message().replaceText(URL_REPLACEMENT_CONFIG)))); @@ -51,13 +52,10 @@ public class ChatListener extends PlexListener } Component prefix = PlayerMeta.getPrefix(plexPlayer); - if (prefix != null && !prefix.equals(Component.empty()) && !prefix.equals(Component.space())) - { + if (prefix != null && !prefix.equals(Component.empty()) && !prefix.equals(Component.space())) { renderer.hasPrefix = true; renderer.prefix = prefix; - } - else - { + } else { renderer.hasPrefix = false; renderer.prefix = null; } @@ -67,42 +65,39 @@ public class ChatListener extends PlexListener event.renderer(renderer); } - public static class PlexChatRenderer implements ChatRenderer - { + public static class PlexChatRenderer implements ChatRenderer { public boolean hasPrefix; public Component prefix; public Supplier before = null; @Override - public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer) - { - Component component = Component.empty(); + public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer) { + Component component = format; - if (before != null) - { + if (before != null) { component = component.append(before.get()); } - if (hasPrefix) - { - component = component.append(prefix).append(Component.space()); + + // Substitute the prefix from the config + if (hasPrefix) { + component = component.replaceText(TextReplacementConfig.builder().matchLiteral("{prefix}").replacement(prefix).build()); } - return component - .append(Component.empty()) - .append( - source.name().equals(sourceDisplayName) ? - SafeMiniMessage.mmDeserialize(plugin.config.getString("chat.name-color") + SafeMiniMessage.mmSerialize(sourceDisplayName)) - : SafeMiniMessage.mmDeserialize(plugin.config.getString("chat.name-color")).append(sourceDisplayName) - ) - .append(Component.space()) - .append(Component.text("»").color(NamedTextColor.GRAY)) - .append(Component.space()) - .append(message) - .replaceText(URL_REPLACEMENT_CONFIG); + + // Substitute the display name from the config + component = component.replaceText(TextReplacementConfig.builder().matchLiteral("{name}") + .replacement(sourceDisplayName).build()); + + // Substitute the message from the config + component = component.replaceText(TextReplacementConfig.builder().matchLiteral("{message}").replacement(message).build()); + + // Fix links not being clickable + component = component.replaceText(URL_REPLACEMENT_CONFIG); + + return component; } } - private static void defaultChatProcessing(AsyncChatEvent event, PlexPlayer plexPlayer) - { + private static void defaultChatProcessing(AsyncChatEvent event, PlexPlayer plexPlayer) { String text = PlexUtils.getTextFromComponent(event.message()); event.message(PlexUtils.stringToComponent(text)); } diff --git a/server/src/main/resources/config.yml b/server/src/main/resources/config.yml index b30ddbf..164e282 100644 --- a/server/src/main/resources/config.yml +++ b/server/src/main/resources/config.yml @@ -25,8 +25,8 @@ chat: # The maximum amount of characters a player can have for their tag # This does not include color tags such as or max-tag-length: 64 - # Color tag for name color - name-color: '' + # The chat format can be customized here if the Plex chat system is enabled + format: "{prefix} {name} » {message}" # You can define colors for each group which will appear in the tab list colors: