Correct usage of AsyncPlayerChatEvent

Discussion in 'Resources' started by Sehales, Oct 24, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Sehales

    Code:java
    1.  
    2.  
    3. //WRONG USAGE
    4.  
    5. public class MyChatListener implements Listener{
    6. @EventHandler(priority = EventPriority.LOW)
    7. public void onPlayerChat(AsyncPlayerChatEvent event) {
    8.  
    9. String eventMessage = "write this before every message" + event.getMessage();
    10. Player eventPlayer = event.getPlayer();
    11. String format = "<group-prefix><player><group-suffix>:<message>";
    12. //replacing your values
    13. format.replace("<player>", player.getDisplayName());
    14. format.replace("<group-prefix>", MyPermissionManager.getPlayerGroupPrefix(player)); //something like that
    15. format.replace("<group-suffix>", MyPermissionManager.getPlayerGroupSuffix(player)); //something like that
    16. format.replace("<message>", eventMessage);
    17. e.setFormat(format);
    18. }
    19. }
    20.  
    21.  


    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
    1. AsyncPlayerChatEvent.setFormat()
    is using
    Code:java
    1. 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
    1.  
    2.  
    3. //CORRECT USAGE
    4.  
    5. public class MyChatListener implements Listener{
    6. @EventHandler(priority = EventPriority.LOW)
    7. public void onPlayerChat(AsyncPlayerChatEvent event) {
    8.  
    9. String eventMessage = event.getMessage();
    10. Player eventPlayer = event.getPlayer();
    11.  
    12. String format = "<group-prefix><player><group-suffix>:<message>";
    13. //replacing your values
    14. format.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 name
    15. format.replace("<group-prefix>", MyPermissionManager.getPlayerGroupPrefix(player)); //something like that
    16. format.replace("<group-suffix>", MyPermissionManager.getPlayerGroupSuffix(player)); //something like that
    17. format.replace("<message>", "%2$s");
    18. e.setFormat(format);
    19. e.setMessage("we are writing something before each message" + eventMessage); //do not change the message with setFormat(), use setMessage() instead!
    20. }
    21. }
    22. }
    23.  
    24.  

    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!
     
    Brian_Entei likes this.
  2. Offline

    Drkmaster83

    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.
     
  3. Offline

    Sehales

    That is crazy..., please post a code snippet of your Listener
    It is working for me correctly....
     
  4. Offline

    Drkmaster83

    Code:JAVA
    1. @EventHandler (priority = EventPriority.MONITOR)
    2. public void onPlayerChat(AsyncPlayerChatEvent evt)
    3. {
    4. Player player = evt.getPlayer();
    5. String playerName = player.getDisplayName();
    6. if(plugin.getServer().getPluginManager().getPlugin("PermissionsEx") == null)
    7. {
    8. String format = "<<class><player>>: <message>";
    9. format.replace("<player>", playerName);
    10. format.replace("<message>", evt.getMessage());
    11. if(plugin.Soldiers.containsKey(player.getName()))
    12. {
    13. format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.RED + "Soldier" + ChatColor.WHITE + "]");
    14. }
    15. else if(plugin.Hunters.containsKey(player.getName()))
    16. {
    17. format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.YELLOW + "Hunter" + ChatColor.WHITE + "]");
    18. }
    19. else if(plugin.Woodcutters.containsKey(player.getName()))
    20. {
    21. format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.GOLD + "Woodcutter" + ChatColor.WHITE + "]");
    22. }
    23. else if(plugin.Farmers.containsKey(player.getName()))
    24. {
    25. format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.GREEN + "Farmer" + ChatColor.WHITE + "]");
    26. }
    27. else if(plugin.Miners.containsKey(player.getName()))
    28. {
    29. format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.GRAY + "Miner" + ChatColor.WHITE + "]");
    30. }
    31. else if(plugin.Fishermen.containsKey(player.getName()))
    32. {
    33. format.replace("<class>", ChatColor.WHITE + "[" + ChatColor.AQUA + "Fisherman" + ChatColor.WHITE + "]");
    34. }
    35. else
    36. {
    37. format.replace("<class>", "");
    38. }
    39. evt.setFormat(format);
    40. evt.setMessage(evt.getMessage().replaceAll("&", "ยง"));

    The code above just prints out "<<class><player>: <message>" when I talk.
     
  5. Offline

    Wolftein

    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");
     
  6. Offline

    Drkmaster83

    What will the ending return string be? All of them?
     
  7. Offline

    Sehales

    yes it will return all but you can use replace = replace.replace(...) instead if you want
     
Thread Status:
Not open for further replies.

Share This Page