kangarko / ChatControl-Red

Issue tracker and documentation for the next generation ChatControl Red, the most advanced chat management plugin.
48 stars 23 forks source link

1.20.4: Using variables such as [item] in ChatChannelEvent #2544

Closed Nextinka closed 7 months ago

Nextinka commented 7 months ago

Are you using MySQL?

Yes

Are you using BungeeCord/Velocity?

No

Question

Hello, I use ChatControlRed as a main plugin for chatting and I take advantage of the variables such as [item] and so on. The problem is that I also have a clan chat that works by hooking into your API, specifically using the ChatChannelEvent. It seems as it applies all the chat rules to it, such as swear filters, however I can't get to work the variables, typing [item] simply does not work - it just returns [item] in the chat. I'm not sure if this is an issue on my end or the event does not support variables at all.

Here is the code that handles the clan chat function:

`@EventHandler(priority = EventPriority.LOWEST)
public void onClanMessage(ChatChannelEvent event) {
    Player player = (Player) event.getSender();

    ClanUser clanUser = ClanPlugin.getInstance().getManager().getClanUser(player.getUniqueId());

    if (clanUser != null && clanUser.isInClan() && clanUser.isChatEnabled()) {
        event.setCancelled(true);

        String message = Config.clanChatFormat()
                .replace("{player}", player.getName())
                .replace("{pd}", player.getDisplayName())
                .replace("{clan}", clanUser.getCurrentClan().getName())
                .replace("{message}", event.getMessage());

        Pattern pattern = Pattern.compile("%(.*?)%");
        Matcher matcher = pattern.matcher(message);

        while (matcher.find()) {
            String placeholder = matcher.group(1);
            String placeholderValue = PlaceholderAPI.setPlaceholders(player, "%" + placeholder + "%");
            message = message.replace("%" + placeholder + "%", placeholderValue);
        }

        String finalMessage = message;
        clanUser.getCurrentClan().allAction(user -> {
            if (user.isOnline()) {
                sender.sendMessage(event.getMessage());
                user.bukkit().sendMessage(Util.bungeeColourise(finalMessage));
            }
        });

        String consoleOutput = Config.consoleLoggingFormat()
                .replace("{player}", player.getName())
                .replace("{pd}", player.getDisplayName())
                .replace("{clan}", clanUser.getCurrentClan().getName())
                .replace("{message}", event.getMessage());

        Bukkit.getConsoleSender().sendMessage(Util.bungeeColourise(consoleOutput));
    }
}`

I tried playing with the priority of the event, but whether it is the lowest or the highest, it still acts the same. I also used raw functions of the event, such as CommandSender, event.getMessage(), event.setMessage() and so on. I'm really clueless about this one, it's probably an easy fix and I can't figure it out. Is it because I am cancelling the event? If so, how do I make it without cancelling the event but making sure that the variables will work and I won't have duplicated messages (one in clan, one in public chat?) Why do the chat rules work in this case but variables won't? Any help will be greatly appreciated!

Using ChatControl-Red-10.23.5

kangarko commented 7 months ago

Hello,

Due to how chat components work, each message is split into an array and the [item] has a special mechanism to be replaced. As a result it only works internally in ChatControl. I will be looking forward for a rewrite in the next major version but at present there are no plans.

kangarko commented 7 months ago

I plan on dropping Spigot support and using Paper's new chat event for that, however Spigot usage needs to further drop not to alienate my user base.