简体   繁体   中英

Minecraft Chat Message Replacement

I am making a permissions plugin, and want to replace the name of a player with their rank tag. For this, I have the following code:

public void playerChat(AsyncPlayerChatEvent e) {
    Player target = e.getPlayer();

    String message = e.getMessage().replaceAll(target.getName(), colorize(rFile.getString("players." + target)) + " " + target.getName());
    e.setMessage(message);
}

Whenever I send a message to chat, it appears like it would normally. What am I doing wrong here?

Additionally, I am using a config file (cFile) and a ranks.yml file (rFile).

First off, make sure you include the @EventHandler annotation.

@EventHandler
public void playerChat(AsyncPlayerChatEvent e) {
   [...]
}

Next, check if the listener is registered in your onEnable() method. getServer().getPluginManager().registerEvents(new YourListener(...), this);

(Replace the YourListener with this in case it's your main class)

Finally, as Luftbaum said, use AsyncPlayerChatEvent#setFormat within the event. Example Usage:

 e.setFormat(colorize(rFile.getString("players." + target)) + ": " + e.getMessage());

Edit:

In order to translate color codes such as '&3' to Bukkit's ChatColor format, you can use the ChatColor#translateAlternativeColorCodes method.

ChatColor.translateAlternateColorCodes('&', stringThatContainsCodes);

Use event.setFormat(playerRank + ": " + event.getMessage());

This basically formats the message to be the way you want. You can use ChatColor to do colors. Also make sure you have @EventHandler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM