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):
        public void chat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            String format = lang.getLang().getString("ChatFormat");
            if(Bukkit.getServer().getPluginManager().getPlugin("PermissionsEx") == null && Bukkit.getServer().getPluginManager().getPlugin("Vault") ==null) {
            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()) {

    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
            at$1.execute( ~[spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat$ [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_171]
            at Source) [?:1.8.0_171]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_171]
            at java.util.concurrent.ThreadPoolExecutor$ Source) [?:1.8.0_171]
            at 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 ~[?:?]
            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$1.execute( ~[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 $:
    and when i use it the message doesnt send in chat and outputs that errror
  4. Choco


    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.
  6. Choco


    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


    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.