Solved Removing magic and formatting for string

Discussion in 'Spigot Plugin Development' started by Bobcatsss, Jan 31, 2020.

  1. Pretty much I'm not sure how to remove magic and formatting from a string while still allowing color codes.

    I have tried something like this to remove "&k" but the problem with this is if the players name is eg "kate" and they put "&" infront of their name without a following color code it's going to remove the first letter of their name since it's "k"

    Code (Text):
    String replaced = "&6Bob&kcatsss".replaceAll("(?<!&)&([Kk])", "");
    #1 Bobcatsss, Jan 31, 2020
    Last edited: Jan 31, 2020
  2. I believe this is the regex your are looking for.
    Code (Text):
  3. I don't use Regex much but why not use ChatColor.stripColors? (Something along those lines) and then add the String back again?
    • Agree Agree x 1
  4. Strahan


    Agreed. Regardless of what the name says, it strips all formatting not just colors.

  5. I need to keep the colors just not the formatting or magic.
  6. Yeah, you can do that. Strip it, then add it back
  7. Not sure what you mean, If i add it back it's going to add the formatting and magic back which is not what i want.
  8. On another topic, if the String is from a lore or an enchantment directly, wouldn't it be with a '§' instead of a '&'?
    Then, you'd just have to strip away every '§' char instead of the '&'
  9. The simplest option in my opinion would be to just not allow the '&' character unless used for color codes, but I digress.
    You should be able use #replace on the translated String to change ChatColor.COLOR_CHAR.toString()+"k" to "&k"
  10. Would translating only the colors (with your own modified version of ChatColor#translateAlternateColorCodes) help? The decompiled method is pretty much this:
    Code (Text):
    public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) {
            char[] b = textToTranslate.toCharArray(); // Character array

            for(int i = 0; i < b.length - 1; ++i) {
                if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1) {
                    b[i] = 167; // Change the character to §
                    b[i + 1] = Character.toLowerCase(b[i + 1]);

            return new String(b);
    So instead of the "0123456789AaBbCcDdEeFfKkLlMmNnOoRr" string, you'd just want "0123456789AaBbCcDdEeFfRr" (Rr for reset). That way it won't translate the &k and hell won't even touch it.
    • Like Like x 1

  11. I'll give this a go. Thanks TM
    • Like Like x 1
  12. No worries, my only concern with this is that because it doesn't remove the codes, it doesn't remove the codes. (e.g. &kTrying_to_test will remain the same way as it is)

    As far as I know, distinguishing between inputs like &kate and &knope is a different task altogether. On the bright side, it does make the user realize they can't use formatting.

    Extra edit: Trying to translate color codes entirely as mentioned above will remove the &k from &kate anyways, so really trying to then remove the formatting would be a slightly bigger pain.

    If you want to remove the formatting codes following translation though, maybe check for lowercase ones instead of uppercase with Regex? As far as I know the pattern would just be "&[k-o]"
    #12 Thatsmusic99, Jan 31, 2020
    Last edited: Jan 31, 2020
  13. That's true but since this is only to allow them to add color to their name i can just color the input (without translating magic or formatting) then replace any formatting/magic codes and finally strip the color codes and check if it matches their username.

    Then i also have to count how many colors they are trying to use since this is a reward that allows them to only use a certain amount of colors xD
  14. I see, so what you may want then is:
    1. Firstly count all of the times a chat color comes up, such as &3. One way is through using the Regex pattern "(?i)&[0-9a-f]" (we're going to ignore uppercase letters for now in case of &Alex) and using this method provided on Stack Overflow. I'll let you do what you need to do after that bit.
    2. Translate the colour codes.
    3. Then remove the remainder from the Regex "&[k-o]" (again, ignoring uppercase characters).

    Sadly at that point we can't really help those who keep their names lowercase - and after all, if it has to match their username, wouldn't it only make sure the name is alphanumeric anyways (thus won't contain the "&" character)?
  15. Well it's only going to count the same color code as one, so they can use the same color code more than once but it will only count as one color.
    When i check it against their name I'm going to remove the color codes before, this is just to check they haven't tried to change anything in their name.