Chat Format error?

Discussion in 'Plugin Development' started by Astrophylite, Apr 3, 2016.

Thread Status:
Not open for further replies.
  1. Hello everyone!

    I recently coded a chat formatter plugin for someone in the Plugin Requests section and it was working when I published it. Sometime yesterday, they asked for a feature to be added and now for some reason it is erroring on setting the format.

    The error:
    Code:
    [07:24:23 ERROR]: Could not pass event AsyncPlayerChatEvent to ChatFormatter v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.java:1084) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1022) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_77]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.util.UnknownFormatConversionException: Conversion = 'm'
            at org.bukkit.event.player.AsyncPlayerChatEvent.setFormat(AsyncPlayerChatEvent.java:100) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at me.astrophylite.chatformatter.ChatFormatter.onPlayerChat(ChatFormatter.java:72) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            ... 11 more
    Any help will be appreciated,
    Astrophylite.
     
  2. Offline

    mcdorli

    Have you used replaceAll or anx methods, wich uses regEx?
     
  3. @mcdorli I have used #replace() but I don't think that uses regEx...

    Astrophylite.
     
  4. Offline

    mcdorli

    @Astrophylite Maybe show
    Maybe show us some code, replace can take in regEx
     
  5. @mcdorli Here is my code:
    Show Spoiler

    Code:java
    1.  
    2. package me.astrophylite.chatformatter;
    3.  
    4. import java.io.File;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.configuration.file.YamlConfiguration;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.EventPriority;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.player.AsyncPlayerChatEvent;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. import ru.tehkode.permissions.bukkit.PermissionsEx;
    20.  
    21. public class ChatFormatter extends JavaPlugin implements Listener {
    22.  
    23. FileConfiguration config;
    24. File cfile;
    25.  
    26. public void onEnable() {
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28.  
    29. getConfig().options().copyDefaults(true);
    30. config = getConfig();
    31. saveConfig();
    32. cfile = new File(this.getDataFolder(), "config.yml");
    33. }
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    36. if(cmd.getName().equalsIgnoreCase("chatformatter")) {
    37. if(!sender.hasPermission("chatformatter.main")) {
    38. sender.sendMessage(getPrefix() + ChatColor.RED + " You are lacking the permission node: chatformatter.main!");
    39. return true;
    40. }
    41.  
    42. if(args.length == 0 || args.length > 1) {
    43. sender.sendMessage(getPrefix() + ChatColor.RED + " Usage: /" + label + " <reload>");
    44. return true;
    45. }
    46. if(args[0].equalsIgnoreCase("reload")) {
    47. config = YamlConfiguration.loadConfiguration(cfile);
    48. sender.sendMessage(getPrefix() + ChatColor.GREEN + " You successfully reloaded ChatFormatter's configuration file!");
    49. return true;
    50. } else {
    51.  
    52. }
    53. }
    54. return true;
    55. }
    56.  
    57. @EventHandler(priority = EventPriority.HIGHEST)
    58. public void onPlayerChat(AsyncPlayerChatEvent e) {
    59. Player p = e.getPlayer();
    60. String prefix = null, suffix = null;
    61. if(PermissionsEx.getUser(p).getPrefix() != null || !PermissionsEx.getUser(p).getPrefix().isEmpty()) {
    62. prefix = PermissionsEx.getUser(p).getPrefix();
    63. }
    64. if(PermissionsEx.getUser(p).getSuffix() != null || !PermissionsEx.getUser(p).getSuffix().isEmpty()) {
    65. suffix = PermissionsEx.getUser(p).getSuffix();
    66. }
    67.  
    68. String format = ChatColor.translateAlternateColorCodes('&', config.getString("chat-format"));
    69. String customFormat = format.replace("%prefix", prefix
    70. .replace("%name", p.getName())
    71. .replace("%suffix", suffix)
    72. .replace("%message", e.getMessage()));
    73. e.setFormat(customFormat);
    74. }
    75.  
    76. public String getPrefix() {
    77. String prefix = config.getString("prefix") + " ";
    78. return ChatColor.translateAlternateColorCodes('&', prefix);
    79. }
    80. }

     
  6. Offline

    mcdorli

    Try changing the % to something else, like {} in the strings, and use that to replace.
     
  7. @mcdorli The error changed ever so slightly. Still on line 72...
    Code:
    [09:59:06 ERROR]: Could not pass event AsyncPlayerChatEvent to ChatFormatter v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.java:1084) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1022) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_77]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.util.UnknownFormatConversionException: Conversion = ' '
            at org.bukkit.event.player.AsyncPlayerChatEvent.setFormat(AsyncPlayerChatEvent.java:100) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            at me.astrophylite.chatformatter.ChatFormatter.onPlayerChat(ChatFormatter.java:72) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
            ... 11 more
    
    Code is the same just without the %'s in the replace.

    Astrophylite.
     
  8. Offline

    I Al Istannen

    @Astrophylite
    "UnknownFormatConversionException: Conversion = ' '"
    You probably know that there are a few placeholders for the format method. "%s" for Strings for example. Somewhere in the message you have "% ". This will be treated as a placeholder due to the leading "%" sign. Print out the format and find this rogue "%" sign and fix it ;)
     
  9. Offline

    Konato_K

    @Astrophylite NEVER put the message in the format, (also don't put the player name)

    The problem is that you're using the chat format wrong, put the proper string tokens for the formatter so it works as intended (either "%s" or %1$s and %2$s)
     
  10. Offline

    I Al Istannen

    @Astrophylite
    By looking at your code it seems like you misplaced the brackets.

    String customFormat = format.replace("%prefix", prefix
    .replace("%name", name)
    .replace("%suffix", suffix)
    .replace("%message", message
    ));

    The bold ones are the wrong brackets. All the italic code replaces the underlined String, not the format one. Move one of the two bold brakets behind "prefix" and it should work.
     
  11. Thank you guys for your help, I will try fixing this later on. c;

    Astrophylite.
     
Thread Status:
Not open for further replies.

Share This Page