how can I modify a player's message only for some players?

Discussion in 'Plugin Development' started by Tokishu, Nov 28, 2023.

Thread Status:
Not open for further replies.
  1. Offline

    Tokishu

    Hello everyone, how can I modify a player's message only for some players? Let's say PlayerA sent a message, and I want PlayerB to receive the original message while PlayerC receives an edited version. I also need to ensure that this doesn't conflict with chat enhancement plugins, such as Chatty, for example. Here's how I'm currently sending messages, but it's not quite right:


    Code:
    for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
        if (onlinePlayer.hasPermission("nbw.filter")) {
            onlinePlayer.sendMessage(playerName + ": " + originalMessage);
        } else {
            onlinePlayer.sendMessage(playerName + ": " + event.getMessage());
        }
    }
     
  2. Online

    timtower Administrator Administrator Moderator

    @Tokishu This will result in double messages.
    Things like Chatty might be changing the packets send to the players instead of sending it manually like you are doing.
     
  3. Offline

    Tokishu

    The question is precisely about how to avoid this and how I should correctly modify user messages to avoid conflicts. Is it possible to simply increase the priority of the plugin so that Chatty processes the message after my plugin?
     
  4. Online

    timtower Administrator Administrator Moderator

    Considered checking the api of Chatty?
     
  5. Offline

    KarimAKL

    I'm not sure if this is possible with the AsyncPlayerChatEvent, as it is designed to send a single message to all recipients.
    You might, however, be able to let the third-party plugin know through an API, so that it doesn't cause any conflict. I believe this should work as long as the chat event is cancelled and you send the messages.

    Another option would be tinkering with the recipients and creating a new chat event with the modified string, but you should be careful not to recursively call the method. This way, both events should be handled separately by the third-party plugin.
     
  6. Offline

    Dogged

    Whenever a player sends a message, use the AsyncPlayerChatEvent to detect and cancel it. From there, take the message they were going to send and manually send it to Player B, but change it for Player C. This will not result in doubled messages as the original chat message will never be sent, since you cancel the event.
    Code would look something like this:
    Code:
    @EventHandler
    public void onPlayerChat (AsyncPlayerChatEvent e) {
        e.setCancelled(true);
        for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
        if (onlinePlayer.hasPermission("nbw.filter")) {
            onlinePlayer.sendMessage(playerName + ": " + originalMessage);
        } else {
            onlinePlayer.sendMessage(playerName + ": " + event.getMessage());
        }
    }
    
     
  7. Online

    timtower Administrator Administrator Moderator

    That will not work in combination with Chatty though
     
  8. Offline

    Dogged

    What is Chatty being used for? If it is not too complicated, maybe the things Chatty is being used for currently could be added to Tokishu's plugin.
     
  9. Online

    timtower Administrator Administrator Moderator

    Chat formatting probably, maybe message editing.
     
Thread Status:
Not open for further replies.

Share This Page