NullReference
NullReference

Reputation: 311

Modify message for a single player

I'm writing a minecraft plugin that will notify someone when you mention his name in chat. He will receive a customized message where in the message his name is underlined and recoulerd. It will also play a music note.

I have this, but it will sent the message to everyone in the server:

@Override
public void onEnable()
{
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
}

@Override
public void onDisable()
{

}


 @EventHandler
 public void onChat(AsyncPlayerChatEvent e)
 {
     for(Player on:Bukkit.getServer().getOnlinePlayers())
     {
         if(on.equals(e.getPlayer()))continue;


         if(e.getMessage().contains(on.getName()))
         {
             e.setMessage(e.getMessage().replaceAll(on.getName(), ChatColor.GREEN + "@" + ChatColor.UNDERLINE + on.getName()));
             on.playNote(on.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A));
         }
     }
 }

Upvotes: 4

Views: 3896

Answers (3)

Jojodmo
Jojodmo

Reputation: 23616

You could simply cancel the event, and then send a message to all players, sending one player a different, custom message.

For example:

Player ply = (player who's name is mentioned)
String message = "<message>"

on.playNote(on.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A)); //send the player the music
String s = message.replaceAll(ply.getName(), ChatColor.GOLD + ChatColor.UNDERLINE + ply.getName()); //change format of player's name
ply.sendMessage(s); //send message to the player

So, your event could look like this:

@EventHandler
public void onChat(AsyncPlayerChatEvent e){
  String message = e.getMessage(); //get the message

  e.setCancelled(true); //cancel the event, so no message is sent (yet)

  for(Player on : Bukkit.getOnlinePlayers()){ //loop threw all online players
    if(message.contains(on.getName())){ //check if the message contains the player's name
      String newMessage = message.replaceAll(on.getName(), ChatColor.BLUE + ChatColor.UNDERLINE + on.getName() + ChatColor.WHITE); //format the message
      //change ChatColor.BLUE + ChatColor.UNDERLINE to the formatting you want
      on.sendMessage(newMessage); //send the player the message
      on.playNote(on.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A)); //send the player the music
    }
    else{
      on.sendMessage(message); //else send the player the regular message
    }
  }
}

Upvotes: 5

Basti
Basti

Reputation: 1186

To send a message to a single player, you can use:

playerobject.sendMessage(ChatColor.YOURCOLOR+"MESSAGETEXT");

Here's an example:

Player pl = *where you get your player from*;
pl.sendMessage(CharColor.RED + "Hello, this text will be displayed on the Screen of 1 Player in red.")

Thats all you need to do to send a colored message to a player

Upvotes: 1

MrLore
MrLore

Reputation: 3780

I haven't used Bukkit before, but as mentioned in this tutorial, instead of e.setMessage(""), try and use on.sendMessage("").

Edit: I've had a look at the source and AsyncPlayerChatEvent implements Cancellable, so how about you try sending the message to each player individually and then cancelling the event at the end, so you don't duplicate the message being sent, like this:

@EventHandler
public void onChat(AsyncPlayerChatEvent e)
{
    for(Player on:Bukkit.getServer().getOnlinePlayers())
    {
        if(on.equals(e.getPlayer()))continue;

        if(e.getMessage().contains(on.getName()))
        {
            on.sendMessage(e.getMessage().replaceAll(on.getName(), ChatColor.GREEN + "@" + ChatColor.UNDERLINE + on.getName()));
            on.playNote(on.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A));
        }
        else
        {
            on.sendMessage(e.getMessage());
        }
    }
    e.setCancelled(true);
}

Upvotes: 3

Related Questions