Solved Placeholders

Discussion in 'Spigot Plugin Development' started by Jaffaaaa, Aug 16, 2018.

  1. How do I create placeholders so people can configure messages in my config (e.g. &e{REFERED} &3just referred &e{REFEREE} &3and recieved &6${REWARD} nice one)
     
  2. String#replace("{REFEREE}", "Replacement");
     
    • Optimistic Optimistic x 1
  3. I tried this but I get an internal error.
    Code (Java):
    int rewardRefer = main.getPlugin(main.class).getConfig().getInt("RewardToReferer");
                   
                    String path = main.getPlugin(main.class).getConfig().getString("shoutMessage");
                   
                    path.replaceAll("{REFERED}", "sender.getName()");
                    path.replaceAll("{REFEREE}", "Bukkit.getPlayer(username).getName()");
                    path.replaceAll("{REWARD}", Integer.toString(rewardRefer));
                   
                    Bukkit.broadcastMessage(String.format(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")))
                            + " "
                            + path)));
    Error:
    Code (Text):
    [14:18:51 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'jrefer' in plugin JReferrals vRELEASE-2.1.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_144]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_144]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.util.regex.PatternSyntaxException: Illegal repetition
    {REFERED}
            at java.util.regex.Pattern.error(Unknown Source) ~[?:1.8.0_144]
            at java.util.regex.Pattern.closure(Unknown Source) ~[?:1.8.0_144]
            at java.util.regex.Pattern.sequence(Unknown Source) ~[?:1.8.0_144]
            at java.util.regex.Pattern.expr(Unknown Source) ~[?:1.8.0_144]
            at java.util.regex.Pattern.compile(Unknown Source) ~[?:1.8.0_144]
            at java.util.regex.Pattern.<init>(Unknown Source) ~[?:1.8.0_144]
            at java.util.regex.Pattern.compile(Unknown Source) ~[?:1.8.0_144]
            at java.lang.String.replaceAll(Unknown Source) ~[?:1.8.0_144]
            at com.jaffaaaa.jaffareferrals.complexCommands.jrefer.onCommand(jrefer.java:116) ~[?:?]
     
  4. Code (Text):
    path.replaceAll("{REFERED}", "sender.getName()");
                    path.replaceAll("{REFEREE}", "Bukkit.getPlayer(username).getName()");
    You wouldnt need to put the sender.getName in ""
    Also why are you using Bukkit.getPlayer(Username).getName instead of just username?
     
  5. Yeah, from what I remember you're not allowed to use { or } unless you escape them.
    Using \\
    Code (Text):
    path.replaceAll("\\{REFERED}\\", "sender.getName()");
    Valid point put ahead by @Peaches_MLG100, when you're calling a method you don't enclose it in "" or it'll be treat like it's just a normal string
     
    • Optimistic Optimistic x 1
  6. I get an error on line 123 now which is this:

    Code (Java):
    Bukkit.broadcastMessage(String.format(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")))
                            + " "
                            + path)));
     
    Code (Java):

    String path = main.getPlugin(main.class).getConfig().getString("shoutMessage");
                 
                    path.replaceAll("%referred%", sender.getName());
                    path.replaceAll("%referee%", username);
                    path.replaceAll("%reward%", Integer.toString(rewardRefer));
                 
                    Bukkit.broadcastMessage(String.format(String.format((ChatColor.translateAlternateColorCodes('&', main.getPlugin(main.class).getConfig().getString("prefix")))
                            + " "
                            + path)));
     
     
  7. Don't use replaceAll, it isn't suited for that (It works better for Regex). Use String#replace instead.
     
  8. Send the error.