2022-05-07 22:49:50 +00:00
|
|
|
package dev.plex.handler;
|
|
|
|
|
|
|
|
import dev.plex.Guilds;
|
|
|
|
import dev.plex.Plex;
|
|
|
|
import dev.plex.api.chat.IChatHandler;
|
|
|
|
import dev.plex.cache.PlayerCache;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
|
|
|
import dev.plex.util.PlexUtils;
|
|
|
|
import dev.plex.util.minimessage.SafeMiniMessage;
|
|
|
|
import io.papermc.paper.chat.ChatRenderer;
|
|
|
|
import io.papermc.paper.event.player.AsyncChatEvent;
|
|
|
|
import net.kyori.adventure.audience.Audience;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.TextReplacementConfig;
|
|
|
|
import net.kyori.adventure.text.event.ClickEvent;
|
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
|
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
|
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
|
|
|
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
|
|
public class ChatHandlerImpl implements IChatHandler
|
|
|
|
{
|
|
|
|
private final static TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig.builder().match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]").replacement((matchResult, builder) -> Component.empty().content(matchResult.group()).clickEvent(ClickEvent.openUrl(matchResult.group()))).build();
|
|
|
|
private final PlexChatRenderer renderer = new PlexChatRenderer();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doChat(AsyncChatEvent event)
|
|
|
|
{
|
|
|
|
event.renderer(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class PlexChatRenderer implements ChatRenderer
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer)
|
|
|
|
{
|
|
|
|
String text = PlexUtils.getTextFromComponent(message);
|
|
|
|
|
2022-05-08 05:54:22 +00:00
|
|
|
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(source.getUniqueId());
|
|
|
|
Component prefix = Plex.get().getRankManager().getPrefix(plexPlayer);
|
|
|
|
|
2022-05-07 22:49:50 +00:00
|
|
|
AtomicBoolean guildPrefix = new AtomicBoolean(false);
|
|
|
|
AtomicReference<Component> component = new AtomicReference<>(Component.empty());
|
2022-05-08 05:54:22 +00:00
|
|
|
Guilds.get().getGuildHolder().getGuild(source.getUniqueId()).ifPresent(guild ->
|
|
|
|
{
|
2022-05-07 22:49:50 +00:00
|
|
|
if (guild.getPrefix() != null)
|
|
|
|
{
|
2022-05-08 05:54:22 +00:00
|
|
|
component.set(component.get().append(SafeMiniMessage.mmDeserializeWithoutEvents(guild.getPrefix())));
|
2022-05-07 22:49:50 +00:00
|
|
|
guildPrefix.set(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-08 05:54:22 +00:00
|
|
|
if (prefix != null)
|
2022-05-07 22:49:50 +00:00
|
|
|
{
|
|
|
|
if (guildPrefix.get())
|
|
|
|
{
|
|
|
|
component.set(component.get().append(Component.space()));
|
|
|
|
}
|
|
|
|
component.set(component.get().append(prefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
return component.get().append(Component.space()).append(PlexUtils.mmDeserialize(Plex.get().config.getString("chat.name-color", "<white>") + MiniMessage.builder().tags(TagResolver.resolver(StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition())).build().serialize(sourceDisplayName))).append(Component.space()).append(Component.text("»").color(NamedTextColor.GRAY)).append(Component.space()).append(SafeMiniMessage.mmDeserializeWithoutEvents(text)).replaceText(URL_REPLACEMENT_CONFIG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|