修改单个玩家的消息

时间:2014-04-10 08:03:29

标签: java minecraft bukkit

我正在编写一个Minecraft插件,当你在聊天中提到他的名字时会通知某人。他将收到一条定制的消息,在消息中,他的名字加下划线并重新收录。它还会播放音乐笔记。

我有这个,但它会将消息发送给服务器中的每个人:

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

3 个答案:

答案 0 :(得分:5)

您可以简单地取消该活动,然后向所有玩家发送消息,向一个玩家发送不同的自定义消息。

例如:

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

因此,您的活动可能如下所示:

@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
    }
  }
}

答案 1 :(得分:3)

我以前没有使用Bukkit,但正如this tutorial中所提到的,而不是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);
}

答案 2 :(得分:1)

要向单个玩家发送消息,您可以使用:

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

以下是一个例子:

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

您需要做的就是向播放器发送彩色信息