Add customizable chat format

This commit is contained in:
Telesphoreo 2024-11-27 19:11:37 -06:00
parent cc305f6bd6
commit 40e0bb0845
2 changed files with 32 additions and 37 deletions

View file

@ -10,9 +10,11 @@ import dev.plex.util.minimessage.SafeMiniMessage;
import dev.plex.util.redis.MessageUtil; import dev.plex.util.redis.MessageUtil;
import io.papermc.paper.chat.ChatRenderer; import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent; import io.papermc.paper.event.player.AsyncChatEvent;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig; import net.kyori.adventure.text.TextReplacementConfig;
@ -24,8 +26,7 @@ import org.bukkit.event.EventPriority;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@Toggleable("chat.enabled") @Toggleable("chat.enabled")
public class ChatListener extends PlexListener public class ChatListener extends PlexListener {
{
public static final TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig public static final TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig
.builder() .builder()
.match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]") .match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]")
@ -37,12 +38,12 @@ public class ChatListener extends PlexListener
public static BiConsumer<AsyncChatEvent, PlexPlayer> PRE_RENDERER = ChatListener::defaultChatProcessing; public static BiConsumer<AsyncChatEvent, PlexPlayer> PRE_RENDERER = ChatListener::defaultChatProcessing;
private final PlexChatRenderer renderer = new PlexChatRenderer(); private final PlexChatRenderer renderer = new PlexChatRenderer();
private static final Component format = SafeMiniMessage.mmDeserialize(plugin.config.getString("chat.format"));
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onChat(AsyncChatEvent event) public void onChat(AsyncChatEvent event) {
{
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(event.getPlayer().getUniqueId()); 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 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)); 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)))); 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); 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.hasPrefix = true;
renderer.prefix = prefix; renderer.prefix = prefix;
} } else {
else
{
renderer.hasPrefix = false; renderer.hasPrefix = false;
renderer.prefix = null; renderer.prefix = null;
} }
@ -67,42 +65,39 @@ public class ChatListener extends PlexListener
event.renderer(renderer); event.renderer(renderer);
} }
public static class PlexChatRenderer implements ChatRenderer public static class PlexChatRenderer implements ChatRenderer {
{
public boolean hasPrefix; public boolean hasPrefix;
public Component prefix; public Component prefix;
public Supplier<Component> before = null; public Supplier<Component> before = null;
@Override @Override
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer) public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer) {
{ Component component = format;
Component component = Component.empty();
if (before != null) if (before != null) {
{
component = component.append(before.get()); component = component.append(before.get());
} }
if (hasPrefix)
{ // Substitute the prefix from the config
component = component.append(prefix).append(Component.space()); if (hasPrefix) {
component = component.replaceText(TextReplacementConfig.builder().matchLiteral("{prefix}").replacement(prefix).build());
} }
return component
.append(Component.empty()) // Substitute the display name from the config
.append( component = component.replaceText(TextReplacementConfig.builder().matchLiteral("{name}")
source.name().equals(sourceDisplayName) ? .replacement(sourceDisplayName).build());
SafeMiniMessage.mmDeserialize(plugin.config.getString("chat.name-color") + SafeMiniMessage.mmSerialize(sourceDisplayName))
: SafeMiniMessage.mmDeserialize(plugin.config.getString("chat.name-color")).append(sourceDisplayName) // Substitute the message from the config
) component = component.replaceText(TextReplacementConfig.builder().matchLiteral("{message}").replacement(message).build());
.append(Component.space())
.append(Component.text("»").color(NamedTextColor.GRAY)) // Fix links not being clickable
.append(Component.space()) component = component.replaceText(URL_REPLACEMENT_CONFIG);
.append(message)
.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()); String text = PlexUtils.getTextFromComponent(event.message());
event.message(PlexUtils.stringToComponent(text)); event.message(PlexUtils.stringToComponent(text));
} }

View file

@ -25,8 +25,8 @@ chat:
# The maximum amount of characters a player can have for their tag # The maximum amount of characters a player can have for their tag
# This does not include color tags such as <red> or <rainbow> # This does not include color tags such as <red> or <rainbow>
max-tag-length: 64 max-tag-length: 64
# Color tag for name color # The chat format can be customized here if the Plex chat system is enabled
name-color: '<white>' format: "{prefix} <white>{name} <gray>» <reset>{message}"
# You can define colors for each group which will appear in the tab list # You can define colors for each group which will appear in the tab list
colors: colors: