Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
         }
     }
 }
like image 793
NullReference Avatar asked Jan 11 '23 20:01

NullReference


2 Answers

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
    }
  }
}
like image 76
Jojodmo Avatar answered Jan 18 '23 05:01

Jojodmo


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);
}
like image 20
MrLore Avatar answered Jan 18 '23 05:01

MrLore