package org.twipnetwork.colorfulwords;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

public class ChatListener implements Listener {
    private final ColorfulWords plugin;

    public ChatListener(ColorfulWords plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event) {
        String colorHex = plugin.getPlayerColor(event.getPlayer().getUniqueId());
        if (colorHex != null) {
            TextColor color = TextColor.fromHexString(colorHex);
            String coloredMessage = Component.text(event.getMessage()).color(color).toString();
            event.setFormat("%1$s: " + coloredMessage);
        }
    }
}
