Change single player post

I am writing a minecraft plugin that will notify someone when you provide their name in the chat. He will receive an individual message in which his name is underlined and returned in the message. It will also play music.

I have this, but it will send a message to everyone on 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));
         }
     }
 }
+4
source share
3 answers

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

For instance:

Player ply = (player who 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 name
ply.sendMessage(s); //send message to the player

So your event might 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 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
    }
  }
}
+5
source

Bukkit, , , e.setMessage("") on.sendMessage("").

: AsyncPlayerChatEvent Cancellable, , , , :

@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);
}
+3

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

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

Here is 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.")

This is all you need to do to send a color message to the player.

+1
source

Source: https://habr.com/ru/post/1535884/


All Articles