Replace all not working?

Discussion in 'Spigot Plugin Development' started by FroZenKeyboard, Oct 12, 2017.

  1. Ok, so i am currently making a minigame. But the scoreboard can't replace & to §. And i don't know why. No errors, and the code seems fine.
    Code (Text):
            for(String l : ChestPvP.getPlugin(ChestPvP.class).sc.getStringList("layout.scoreboard")) {
                Team lm = s.registerNewTeam(l.replaceAll("&", ""));
                lm.setDisplayName(l.replaceAll("%empty%", "").replace("&", "§").replace("%sec%", String.valueOf(3)).replace("%min%", String.valueOf(1)));
                lm.addEntry(l);
                o.getScore(l).setScore(score);
            }
    Btw, just replaced %sec% and %min% to something random to actually see if it worked.
     
  2. Use a better way to get your main class or use:
    Code (Text):
    ((ChestPvP) Bukkit.getPlugin(/*insert name of your plugin here*/ "name"))
    You can use ChatColor.translateAlternateColorCodes('&', text); to replace & with §
    I will never understand why people do this. ;-;
     
    #2 nicuch, Oct 12, 2017
    Last edited: Oct 13, 2017
    • Funny Funny x 1
  3. The replace all accepts a Regex as argument, not a simple String
     
  4. Consider ChatColor#translateAlternateColorCodes instead.
     
  5. Did not work.
     
  6. Use .replace(String, String) instead of replaceAll();
     
  7. anyways that is recommended because replace even replaces for say suppose &z which isnt a color code but it will still replace, but not in chatcolor.translate
     
  8. Also tried that. Didn't work either :/
     
  9. I figured it out, but now text replaces then the old text which didn't get replaced is still there.

    My updated code:
    Code (Text):
            for(String l : ChestPvP.getPlugin(ChestPvP.class).sc.getStringList("layout.scoreboard")) {
                Team lm = s.registerNewTeam(l.replaceAll("&", ""));
                lm.setPrefix(l.replaceAll("&", "§").replace("%empty%", "").replace("%min%", String.valueOf(5)).replace("%sec%", String.valueOf(1)));
                lm.addEntry(l);
                o.getScore(l).setScore(score);
            }
    Not so mutch different, just used setPrefix instead of setDisplayname xd
     
  10. -> lm.setPrefix(l.replace("&", "§")

    maybe?

    Code (Text):
    ChatColor.translateAlternateColorCodes('&',"msg");
    and make sure you import org.bukkit.ChatColor instead of the bungeecord one
     
  11. Didn't work.
     
  12. what do you mean by didnt work? does it give an error?

    Also, what exactly is
    Code (Text):
    Team lm = s.registerNewTeam(l.replaceAll("&", ""));
    supposed to do?
    Insead of doing l.replaceAll("&", "") are you trying ChatColor.translateAlternateColorCodes('&',l);?
     
    #12 TheOpGaming, Oct 12, 2017
    Last edited: Oct 12, 2017
  13. Code (Text):
        for(String l : ChestPvP.getPlugin(ChestPvP.class).sc.getStringList("layout.scoreboard")) {
            Team lm = s.registerNewTeam(ChatColor.translateAlternateColorCodes('&', l));
            lm.setPrefix(ChatColor.translateAlternateColorCodes('&',l.replace("%empty%", "").replace("%min%", String.valueOf(5)).replace("%sec%", String.valueOf(1))));
            lm.addEntry(l);
            o.getScore(l).setScore(score);
        }
     
  14. Strahan

    Benefactor

    What's the point of using String.valueOf if you are gonna pass it a constant? Just do "5" and "1".
     
    • Agree Agree x 1
  15. just stop...

    use ChatColor#translateAlternateColorCodes


    you're doing it wrong then.
     
    • Agree Agree x 1
  16. You need to post your code to see what you are doing instead of just saying "Did not work", we can't help you if you are not showing us what you are doing.
     
    • Agree Agree x 1
  17. or just use constructors :)
     
    • Agree Agree x 1
  18. This thread isn't about OP's use of static plugin instance, give the guy a break...

    @FroZenKeyboard I think your problem isnt the replace but the fact youre not re-assigning your result to a variable. 'l' does not change value as a result of replace/replaceAll, String is immutable. If you intended to add a team scoreboard entry with the same values as the team display name, you need to assign it as such.

    Code (Text):
    Team lm = s.registerNewTeam(l.replaceAll("&", ""));
    String lineWithColors = ChatColor.translateAlternateColorCodes('&', l);
    String lineWithPlaceholders = lineWithColors.replace("%sec%", "12").etc...;

    lm.setDisplayName(lineWithPlaceholders);
    lm.addEntry(lineWithPlaceholders);
    o.getScore(lineWithPlaceholders).setScore(score);
     
    Otherwise, I can not see what else is wrong. You will have to make it clearer what you actually want to see. It would also help to use string constants under test instead of reading it from your plugin's configuration, because it reduces the number of potential places that this could break. Debug logic.

    Also, put a log or System.out.println of lineWithPlaceholders so you can verify that it is correct, without depending on the display on the scoreboard. Another way to break down the number of possible failure points to find the issue.
     
    • Useful Useful x 1
  19. Thanks, that worked! Thank you.
    I am creating it? What kind of other version is it? xd
     

Share This Page