Solved Editing a message for just one player

Discussion in 'Spigot Plugin Development' started by GCNT, Jun 11, 2018.

  1. Hey spigot

    I'm trying to edit a message to only one player, but i can only imagine the code for the whole server.
    Can someone help me?

    Code should be (in words):
    Code (words (Unknown Language)):

    if (e.getMessage().contains(A PLAYER NAME) {
      for (A PLAYER NAME) {
        e.setMessage(e.getMessage().replace(A PLAYER NAME, ChatColor.AQUA + "" + ChatColor.UNDERLINED + A PLAYER NAME));
      }
    }
     
    Whole server code:
    Code (Java):

    @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {  
            for (Player p2 : Bukkit.getOnlinePlayers()) {
                if (e.getMessage().contains(p2.getName())) {
                    e.setMessage(e.getMessage().replaceAll(p2.getName(), ChatColor.AQUA + "" + ChatColor.UNDERLINE + p2.getName()));
                }
            }
        }
     
    Hope you'll understand my words :)
     
  2. You could cancel the event and send the message to each player individually, hence you can send a different message to every player.
     
    • Agree Agree x 3
  3. Or, you can listen to the chat packets to modify those stuffs sent in packets directly by other plugins.
     
  4. I've done it this game and made sure that the name is not the sender's name. It will now only be aqua underlined for the named player.