Solved Sending fake chat message from a player to a specific group of recipient

Discussion in 'Spigot Plugin Development' started by AlexLander123, Jun 12, 2018.

  1. I am aware that we could use Player.chat(String message) to send a message as a player. What I am looking for is how to send it only to the list of recipient provided. Maybe something like Player.chat(String message, List<Player> recipients). That way I can make the fake message appear only for certain players.

    Thanks in advance.
     
  2. To just show it
    Code (Java):
    // Assume players is a List<Player
    players.forEach(p -> p.sendMessage("Blah"));
    You might be able to trigger some event also to make it look like the player sent it and idk chat filters
     
  3. Yeah I thought of that but many plugin processes chat format, therefore it will ignore the chat plugins which make the fake message appear unnatural. An idea was to fire a AsyncChatEvent, pass the parameters into it and take the output but I am not certain whether it will work. Therefore, I came here to look for better alternatives.
     
    #3 AlexLander123, Jun 12, 2018
    Last edited: Jun 12, 2018
  4. You can create the event, dispatch it an if it wasn't cancelled you can send it
     
  5. ok so i copied some code 2 help u

    Code (Text):
       
    private void fakeMASSAGE_SEND(Player parameter1, String massage, Set<Player> recipients) {
            AsyncPlayerChatEvent event = new AsyncPlayerChatEvent(false, parameter1, massage, recipients);
            Bukkit.getPluginManager().callEvent(event);
    }
    just becareful of the ram
     
  6. Code (Java):
    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent e) {
        if (!e.isAsynchronous()) {
            e.getRecipients.clear();
            e.getRecipients().add(Player);
        }
    }
    If the plugin makes the player send the message, then it should be fired as synchronous (from the server), whereas a player sending it normally would be asynchronous (sent client-side to the server through packets). Therefore, you should be able to clear the recipients and add whichever players you wish to add as the recipients as shown above.

    EDIT://
    Here's also a screenshot to show an example of how the code runs differently depending on the message. The first message sent by me was executed by one of my commands, "/heal", using that Player#chat() method. As you can see, there is also a green message stating that the message was synchronously using that method. When I send my next message, a random "te", it doesn't send the message. Hope that helps!

    Code (Java):
    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent e) {
        if (!e.isAsynchronous())
            e.getPlayer().sendMessage(ChatColor.GREEN + "This message was sent synchronously!");
    }
     
    #6 Escad, Jun 13, 2018
    Last edited: Jun 13, 2018
    • Winner Winner x 1
  7. Thanks for the reply. Marking as solved.