Custom json message, cant send $

Discussion in 'Spigot Plugin Development' started by ImLiam2110, Nov 21, 2018.

  1. Hi iv made a custom json chat but i have 1 problem when a players sends $ sign i get an error. If you know why this is could you let me know and even better if you know a fix. thanks

    Code (Text):
    @EventHandler
        public void chat(AsyncPlayerChatEvent e) {
            e.setCancelled(true);
            Player p = e.getPlayer();
            lang.setupLang();
       
            String format = lang.getLang().getString("ChatFormat");
            if(Bukkit.getServer().getPluginManager().getPlugin("PermissionsEx") == null && Bukkit.getServer().getPluginManager().getPlugin("Vault") ==null) {
                return;
            }
       
            format = format.replaceAll("%name%", p.getDisplayName());
            format = format.replaceAll("%message%", e.getMessage());
               
            PermissionUser puser = PermissionsEx.getUser(p);
       
            format = format.replaceAll("%prefix%", puser.getPrefix());
            format = format.replaceAll("%suffix%", puser.getSuffix());
       

       
            format = format.replaceAll("%prefix%", "");
            format = format.replaceAll("%suffix%", "");
       
            String rank = plugin.getPermissions().getPrimaryGroup(p);
       
            TextComponent jmsg = new TextComponent(c.f(format));
            jmsg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(c.f("&2Rank: " + rank)).create()));

       
            for(Player players: Bukkit.getOnlinePlayers()) {
                players.spigot().sendMessage(jmsg);
            }
       
        }
    Error:

    Code (Text):
    [code][21:35:52 INFO]: 6pack issued server command: /n hi
    [21:35:59 ERROR]: Could not pass event AsyncPlayerChatEvent to pBase v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.java:1084) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1022) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_171]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_171]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_171]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_171]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    Caused by: java.lang.IllegalArgumentException: Illegal group reference: group index is missing
            at java.util.regex.Matcher.appendReplacement(Unknown Source) ~[?:1.8.0_171]
            at java.util.regex.Matcher.replaceAll(Unknown Source) ~[?:1.8.0_171]
            at java.lang.String.replaceAll(Unknown Source) ~[?:1.8.0_171]
            at sixpack.base.events.chatFormat.chat(chatFormat.java:39) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_171]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
     
    #1 ImLiam2110, Nov 21, 2018
    Last edited: Nov 21, 2018
  2. And it works when not using $? That doesn't really make sense. Print out the actual json and then we'll se if it's invalid.
     
  3. yeah. without using $: http://prntscr.com/llccvc
    and when i use it the message doesnt send in chat and outputs that errror
     
  4. Choco

    Moderator

    Because $ is a RegEx character. Parse it with \. i.e. \\$. You need two \'s because Java uses \ to parse characters as well so you need to parse the parse which ultimately results in \$ being sent to the RegEx compiler.

    Speaking of RegEx, replace values in Strings with #replace(). The alternative #replaceAll() replaces uses RegEx matching which you do not need
     
  5. Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.
    https://docs.oracle.com/javase/8/do...replaceAll-java.lang.String-java.lang.String-
     
  6. Choco

    Moderator

    The RegEx matching is done internally. This is of no use to the OP.
     
  7. okay so when i do
    Code (Text):
                    format = format.replaceAll("$", "\\$");
    it still sends the error but also when i dont even put $ it sends a $ out in chat
     
  8. Choco

    Moderator

    Yea, I'm not sure you understand what I'm trying to say here. lol.
    #replaceAll() uses RegEx, so what you've done now is used RegEx to attempt to fix something as a result of RegEx by creating that very same problem. Use #replace(), not #replaceAll().
     
    • Agree Agree x 1
  9. What method worked is causing me the same problem.