Replacing color codes &,§

Discussion in 'Plugin Development' started by Jamscott, Jun 1, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Jamscott

    Code:java
    1. Player p = e.getPlayer();
    2. PermissionUser user = PermissionsEx.getUser(p);
    3. String prefix = user.getPrefix();
    4. e.setJoinMessage("§9Enderbox> §7Join: " + prefix + p.getName().replaceAll("&", "\u00A7"));



    I need help when i try to get there prefix which is a color for PermissionEx and put it infront of there name on join it doesnt work its comes though like &4...

    Could someone help!

    PermissionEx file
    groups:
    Op:
    default: false
    prefix: '&4'
    build: true
    permissions:
    - ender.mod
    - '*'

    [​IMG]
     
  2. Offline

    OriginalitayHD

    Try this
    Code:
    e.setJoinMessage("§9Enderbox> §7Join: " + prefix + p.getName().replaceAll("&", "§"));
     
  3. Offline

    Sabersamus

    A great way you can do it now, is ChatColor#translateAlternativeColorChar or something like that
     
    1Rogue likes this.
  4. Offline

    OriginalitayHD

    Better yet do this Jamscott
    Code:
      private String fixtext(String string)
      {
        string = string.replaceAll("&0", ChatColor.BLACK.toString());
        string = string.replaceAll("&1", ChatColor.DARK_BLUE.toString());
        string = string.replaceAll("&2", ChatColor.DARK_GREEN.toString());
        string = string.replaceAll("&3", ChatColor.DARK_AQUA.toString());
        string = string.replaceAll("&4", ChatColor.DARK_RED.toString());
        string = string.replaceAll("&5", ChatColor.DARK_PURPLE.toString());
        string = string.replaceAll("&6", ChatColor.GOLD.toString());
        string = string.replaceAll("&7", ChatColor.GRAY.toString());
        string = string.replaceAll("&8", ChatColor.DARK_GRAY.toString());
        string = string.replaceAll("&9", ChatColor.BLUE.toString());
        string = string.replaceAll("&a", ChatColor.GREEN.toString());
        string = string.replaceAll("&b", ChatColor.AQUA.toString());
        string = string.replaceAll("&c", ChatColor.RED.toString());
        string = string.replaceAll("&d", ChatColor.LIGHT_PURPLE.toString());
        string = string.replaceAll("&e", ChatColor.YELLOW.toString());
        string = string.replaceAll("&f", ChatColor.WHITE.toString());
        string = string.replaceAll("&m", ChatColor.MAGIC.toString());
        string = string.replaceAll("&u", ChatColor.UNDERLINE.toString());
        string = string.replaceAll("&i", ChatColor.ITALIC.toString());
        string = string.replaceAll("&B", ChatColor.BOLD.toString());
        string = string.replaceAll("&r", ChatColor.RESET.toString());
        return string;
     
  5. Offline

    OrangeGuy

    Code:java
    1. public static String colorStr(String str)
    2. {
    3. if (str != null)
    4. {
    5. return ChatColor.translateAlternateColorCodes('&', str);
    6. }
    7. return null;
    8. }


    Simply use this method :)
     
  6. OriginalitayHD Jamscott replacAll() is used for regex - replace() will also replace all occurrences in the String, so you should always use replace() unless using regex. And the problem with just replacing the & with the § is that it will not distinguish between when it's used normally and when it's used for colours - i.e. what if you want to use the message "fish & chips"? It will come out "fish § chips".
     
  7. Offline

    Garris0n

    No. No no no no no no no no no no no no no no no no no no no no no no no no no. NO. Isn't there a gif for this?
    Ah, here it is.
    [​IMG]

    Use "yourString= ChatColor.translateAlternateColorCodes('&', yourString);". Never ever ever ever regardless of anything do this in a different way. There is a method in Bukkit. I don't care if it's chaining replace() or replaceAll() or using regex or replacing characters with others or doing ridiculous reassignments of strings or printing it out and mailing it to a team of cambodian monkeys to rewrite with the blood of a child sweat shop worker and send back. USE THE API.
     
    Konkz, AdamQpzm, Lolmewn and 6 others like this.
  8. Offline

    mythbusterma

    Also remeber if you want to add a color code to your text you can use ChatColor.<name>.toString() + "yourstring" instead of typing signal characters in your code.
     
  9. Offline

    Relicum

    static import is a prim example to shorten the code then
    Code:java
    1. String yourString = RED + "more text " + UNDERLINE + "CLICK ME";
     
    WinX64 likes this.
  10. Offline

    whitehooder

    You've obviously gotten a ton of replies with working examples already, but here's a way to do it with regex ;)
    Code:java
    1. public String colorize(String s) {
    2. String msg = s.replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
    3. return msg;
    4. }


    People will hate on you for using this code snippet, so please don't.
     
    Plo124 likes this.
  11. Offline

    Garris0n

     
    Konkz likes this.
  12. Offline

    whitehooder

    Garris0n Haha x'D Chill out mate, I just think it's way cooler using regex. There's something magical to it ;) Such a powerful tool.
     
  13. Offline

    xTrollxDudex

    whitehooder
    This is not the place to use it. The API was meant to prevent inconsistencies, so use it. Use regex for when you need to find stuff outside of the Bukkit API.
     
  14. Offline

    whitehooder

    xTrollxDudex
    I agree with both of you on this, but since when did the Bukkit forums become like this? I didn't say he should use my method, I just gave him another way of doing it, and I bet that's the way it's done in the API as well. Of course it's both easier and more convenient in all ways to use the methods in the Bukkit API. We're here to help fellow coders, not to hate on each other.
     
  15. Offline

    1Rogue


    I believe the API does iterative replacement, but I haven't looked at the ChatColor src in a while.
     
  16. Offline

    xTrollxDudex

    whitehooder
    1. It's not implemented that way.
    2. The fact that it is easier is just an opinion
    3. Again, this isn't the place to do it. Use regex elsewhere where it is needed.
    4. This is all for consistency.
    5. In the future, noobs will come along and use your method being falsely informed of an "easier way"
    6. API methods are provided for a reason
    7. I'm not hating. Your method of doing it is invalid to guidelines presented by the API.
    8. Since you decide to present an "alternative" to using the default API method, and thereby breaking consistency, you're the one against "helping other coders".
     
    Garris0n likes this.
  17. Offline

    whitehooder

    1Rogue xTrollxDudex
    Thanks for correcting me. For you newbs out there, use the methods provided by the Bukkit API. It's easier ;)
     
  18. Offline

    Garris0n

    "Showing your way to do it" is fine when "your way" doesn't involve misinformation that teaches any new programmers who come along the thread bad habits.

    If there's somebody who posts some code somewhere and you show your improved version, great, but the problem is your version is not improved in this case and could actually do damage.
     
  19. Offline

    whitehooder

    Fine. Forget I ever said anything. I'll remove it if you want me to, but how on earth would it do any damage?
     
  20. Offline

    Garris0n

    Because anybody using it is doing something wrong and won't know better.
     
  21. Offline

    whitehooder

    It isn't wrong, it's just not using the Bukkit API for it thus making it a little safer if say, for instance, Mojang decides to change the color code system so my method would no longer work. There's really no other way it could or would do any "damage". Anyhow, we should stop this, I don't want to hate on anyone, nor to be hated on.

    Have a nice day ;)
     
  22. Offline

    Garris0n

    A.K.A. wrong.
     
  23. Offline

    Konkz

  24. Offline

    whitehooder

    Konkz
    Haha :p sure did.

    Garris0n
    This is not a big deal. It's not "wrong" either just because it doesn't use the API. Things like making use of NMS methods etc. are way worse as it will break the plugins for every new Minecraft version released. I've used that method from the start and gotten used to it. Again, I'm not encouraging newbies to use it, just showing them how I've always done it and how it could be achieved with the powerful tool I love so much ;)
     
  25. Offline

    Garris0n

    There is no API for those, so there is no alternative to fixing it every version. If there was an API, using NMS for that would be bad as well.

    I could use NMS for anything, but it would be pointless unless there was no API for said thing. The API exists as a bridge between Mojang's unpredictability and the plugins, and you should use it whenever possible.

    Regex is lovely, however ditching the API for it is not a good idea. A lot of NMS stuff is pretty fun, does that mean I should just quit using the Bukkit API altogether? No.
     
    Konkz likes this.
  26. Offline

    whitehooder

    That's nowhere near what I said. Of course you should try to keep away from using NMS so that your plugins won't break. Although this little thing about how we replace &'s with §'s is nowhere near a big deal, I completely agree with you that we should utilize what's available to us through the API. I said it, I agree, and I've agreed from the start.

    Newbies; using the API pretty much prevents your plugins from breaking whenever Mojang decides to update Minecraft.
     
  27. Offline

    Garris0n

    It's the mentality of it. You should never use anything else to do something the API can do. Even if this one small thing seems like it will never change, don't do it. It's worse because, not only are you doing it, but you're saying "hey everyone who's just starting out, stuff like this is okay!", which it isn't.

    Regardless of whether you insist you're "not encouraging newbies to use it" (you shouldn't encourage anyone to use it, but whatever), you posted it (and said it's how you do it). Somebody could come along and say "hey that looks nice, and it's okay since he does it, so I'll do it too". You are inadvertently encouraging people to use it.
     
    whitehooder likes this.
  28. Offline

    whitehooder

  29. Offline

    Garris0n

    You don't need to apologize, just don't do it. :p
     
    AdamQpzm likes this.
  30. Offline

    1Rogue


    The main idea is that a regex pattern won't be future-proofed to new values whereas ChatColor will be kept up-to-date. If you implemented your own iterative solution using the enum values, that might be on par, but why reinvent the wheel?
     
Thread Status:
Not open for further replies.

Share This Page