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 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<AsyncChatEvent, PlexPlayer> 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<Component> 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));
}

View file

@ -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 <red> or <rainbow>
max-tag-length: 64
# Color tag for name color
name-color: '<white>'
# The chat format can be customized here if the Plex chat system is enabled
format: "{prefix} <white>{name} <gray>» <reset>{message}"
# You can define colors for each group which will appear in the tab list
colors: