4

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));
         }
     }
 }
Pillsy
  • 9,781
  • 1
  • 43
  • 70
NullReference
  • 311
  • 5
  • 20

3 Answers3

5

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
    }
  }
}
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • 1
    I have tried your solution but it gives me an error on check if message contains name. I have failed to get the name of the user that was mentioned. can someone help me. it must be something really easy im looking over. – NullReference Apr 11 '14 at 06:42
3

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);
}
MrLore
  • 3,759
  • 2
  • 28
  • 36
  • Hey, I have tried that however it will just send a extra message to the user. it won't change the message it will just send a seperated one. Thanks for responding. – NullReference Apr 10 '14 at 09:07
1

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

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Basti
  • 1,117
  • 12
  • 32
  • aah sorry i understand how to do that. What im trying to accomplish is. One player gets a message like "hi daniel" and for the player daniel it's "hi @daniel" Daniel should not receive 2 messages just a customized one. – NullReference Apr 10 '14 at 12:08
  • i searchd for my answer in google, i started today with programming minecraftplugs, but i am programming java for a while, so i am no newbi, just saying. What i would try in this case is to cancel the broadcast and send to every Player at the server a single message. would be a way. and you might should use something like @ in front of the name in the chat as a marker to make sure, that rlly daniel the player and not somebody other whose name is daniel was meant. i dont know if canceling the broadcast is possible, because i didnt tryed until yet – Basti Apr 10 '14 at 13:09