Code:java //WRONG USAGE public class MyChatListener implements Listener{@EventHandler(priority = EventPriority.LOW)public void onPlayerChat(AsyncPlayerChatEvent event) { String eventMessage = "write this before every message" + event.getMessage();Player eventPlayer = event.getPlayer();String format = "<group-prefix><player><group-suffix>:<message>";//replacing your valuesformat.replace("<player>", player.getDisplayName());format.replace("<group-prefix>", MyPermissionManager.getPlayerGroupPrefix(player)); //something like thatformat.replace("<group-suffix>", MyPermissionManager.getPlayerGroupSuffix(player)); //something like thatformat.replace("<message>", eventMessage);e.setFormat(format);}} It is working if you do it like this, but if you now type in a "%", "$" or "\" symbol, all your chat formatting is gone and you have plain text. Some developers are trying to replace those chars but this isn't the right way to do chat formatting with bukkit. Code:java AsyncPlayerChatEvent.setFormat() is using Code:java String.format() internally This causes that you should use the AsyncPlayerChatEvent like this(which is the way it is! and was! made for): Code:java //CORRECT USAGE public class MyChatListener implements Listener{@EventHandler(priority = EventPriority.LOW)public void onPlayerChat(AsyncPlayerChatEvent event) { String eventMessage = event.getMessage();Player eventPlayer = event.getPlayer(); String format = "<group-prefix><player><group-suffix>:<message>";//replacing your valuesformat.replace("<player>", "%1$s"); //the player name will be automatically replaced by player.getDisplayName() you could write "%s" too but if you do it like that, you can place the message before the player's nameformat.replace("<group-prefix>", MyPermissionManager.getPlayerGroupPrefix(player)); //something like thatformat.replace("<group-suffix>", MyPermissionManager.getPlayerGroupSuffix(player)); //something like thatformat.replace("<message>", "%2$s");e.setFormat(format);e.setMessage("we are writing something before each message" + eventMessage); //do not change the message with setFormat(), use setMessage() instead!}}} I will make better examples soon. But the sense of this thread is to explain how to use the AsyncPlayerChatEvent correct, not how to write the best java code. If someone has an idea what should be added feel free to say it!
This doesn't work for me. It doesn't take into account the changes that have been made to format, so it just posts the original string in the chat.
Code:JAVA @EventHandler (priority = EventPriority.MONITOR)public void onPlayerChat(AsyncPlayerChatEvent evt){Player player = evt.getPlayer();String playerName = player.getDisplayName();if(plugin.getServer().getPluginManager().getPlugin("PermissionsEx") == null){String format = "<<class><player>>: <message>";format.replace("<player>", playerName);format.replace("<message>", evt.getMessage());if(plugin.Soldiers.containsKey(player.getName())){format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.RED + "Soldier" + ChatColor.WHITE + "]");}else if(plugin.Hunters.containsKey(player.getName())){format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.YELLOW + "Hunter" + ChatColor.WHITE + "]");}else if(plugin.Woodcutters.containsKey(player.getName())){format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.GOLD + "Woodcutter" + ChatColor.WHITE + "]");}else if(plugin.Farmers.containsKey(player.getName())){format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.GREEN + "Farmer" + ChatColor.WHITE + "]");}else if(plugin.Miners.containsKey(player.getName())){format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.GRAY + "Miner" + ChatColor.WHITE + "]");}else if(plugin.Fishermen.containsKey(player.getName())){format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.AQUA + "Fisherman" + ChatColor.WHITE + "]");}else{format.replace("<class>", "");}evt.setFormat(format);evt.setMessage(evt.getMessage().replaceAll("&", "ยง")); The code above just prints out "<<class><player>: <message>" when I talk.
When you do String.replace() you gotta use the string that the function returns. String replaced1 = message.replace("<name>", "MyName"); String replaced2 = replaced1.replace("<class>", "MyClass");