[TUT] How to translate color codes

Discussion in 'Resources' started by Jimfutsu, May 10, 2014.

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

    Jimfutsu

    Hello, y name is Jimfutsu and I have recently found a easy way to work around a problem that I have found annoying. Color Codes.

    First, you have to go somewhere in your Main class where a method wont be affected by anything else(I prefer right underneath the Class).

    The code that you will want to type is:

    Code:java
    1. public String colorize(String msg)
    2. {
    3. String coloredMsg = "";
    4. for(int i = 0; i < msg.length(); i++)
    5. {
    6. if(msg.charAt(i) == '&')
    7. coloredMsg += '§';
    8. else
    9. coloredMsg += msg.charAt(i);
    10. }
    11. return coloredMsg;
    12. }


    This will allow you to use:

    Code:java
    1. Player.sendMessage(colorize("&3Hello"));


    An example of command implementation and usage would be like this:

    Code:java
    1. public class Firehelm extends JavaPlugin implements Listener{
    2.  
    3. /** Methods **/
    4. public String colorize(String msg)
    5. {
    6. String coloredMsg = "";
    7. for(int i = 0; i < msg.length(); i++)
    8. {
    9. if(msg.charAt(i) == '&')
    10. coloredMsg += '§';
    11. else
    12. coloredMsg += msg.charAt(i);
    13. }
    14. return coloredMsg;
    15. }
    16. /** End Methods **/
    17.  
    18. public void onEnable(){
    19. getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22. public void onDisable(){
    23. }
    24.  
    25. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    26. Player p = (Player) sender;
    27. if(commandLabel.equalsIgnoreCase("Hi")) {
    28. p.sendMessage(colorize("&5Hi!!!!"));
    29. }
    30. return true;
    31. }


    Thanks for reading hope this helped!
     
  2. Offline

    ArthurMaker

    Isn't easier using ChatColor.translateAlternateColorCodes('&', STRING)?
     
    xTDKx, Cirno and SainttX like this.
  3. Offline

    Cirno

    Or you can just use ChatColor.COLOR instead of hardcoding the color into strings...
     
  4. Offline

    Jimfutsu

    Cirno ArthurMaker

    The ting though is that for me, when I code custom abilities or items, the owner usually wants color codes that are not found within ChatColor. Also, using translateAlernateColorCodes means that when you send a player a message or whatever, you have to put that in for every message. Using this method, all you have to do is (colorize("string")).
     
  5. Offline

    extended_clip

    I just usually create a method that can be accessed from any class.

    Code:java
    1. public void sms(Player p, String msg) {
    2. p.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
    3. }
     
  6. Offline

    DevRosemberg

    Cirno Well not if they are trying to get the colors from a Config, you could but it would look ugly.
    Jimfutsu Just use ChatColor.translateAlternateColorCodes('char', String);
     
  7. Offline

    Cirno

    If that's the case:

    Code:java
    1. public String colorize(String str){
    2. return ChatColor.translateAlternateColorCodes('&', str);
    3. }


    or if you want to do this without using ChatColor:

    Code:java
    1. public String colorize(String str){
    2. return str.replace('&', '§');
    3. }


    Curious though; what colors are you using if Bukkit doesn't support them? Bukkit supports all vanilla client chat colors.

    I'm going to point out all of the "mistakes" that the code has; it's merely my point of view, so do not take it as an insult.

    1) str +=str2 is horribly inefficient. Every time you run that statement, internelly, Java creates a new StringBuilder/Buffer and runs append("str2"). I suggest switching to a StringBuilder (StringBuffer if you're threading).

    2) Why are you even going through all the characters if String already has a built-in replace()? It would be hypocritical to say this (since I do this sometimes without knowing it), but don't re-invent the wheel.

    3) By using String.replace or your method, it automatically assumes that all characters are chat color characters.
     
  8. Offline

    Jimfutsu

    extended_clip

    I like that idea as it is efficient, I might change the way in the thread, but still personally, I think that this way is a lot easier.
     
  9. Offline

    extended_clip

    I feel with my way, I make it "easier" as I don't even have to type
    Code:java
    1. p.sendMessage(msg);


    I can simply call my method:
    Code:java
    1. if (!p.hasPermission("plugin.reload") {
    2. plugin.sms(p, "&cYou do not have permission!");
    3. }


    or if I wanted to even add my plugins message prefix to it,

    Code:java
    1. //this method sends a player a message with the plugin prefix attached to it
    2. public void smsP(Player p, String msg) {
    3. String prefix = plugin.prefix();
    4. p.sendMessage(ChatColor.transAlternateColorCodes('&', prefix + " &r" + msg));
    5. }


    It is all about how you choose to do it though, find what ever works best for you!
     
    Jimfutsu likes this.
  10. Offline

    Jimfutsu

  11. Offline

    xTrollxDudex

    Jimfutsu
    Never concat String with +=. Use StringBuilder, internally, += clones the current string and modifies the backing array to create the new String. Bukkit's method modifies the char array itself and returns a new String from the char array, which isn't all that bad of an idea until it returns a new String object.
     
  12. Offline

    BungeeTheCookie

    Oh my god. Just use this.
    Code:java
    1. public static String colorize(String message){
    2. return ChatColor.translateAlternateColorCodes('&', message);
    3. }

    You are basically making life harder by using your method. It is not simpler.
     
    cowslayer7890 likes this.
  13. Offline

    extended_clip

    Dont be so dramatic lol.
     
  14. Offline

    BungeeTheCookie

    I have to be dramatic, this is the end of Life as we know it!!!
     
    KingFaris11 and extended_clip like this.
  15. Offline

    Garris0n

  16. Offline

    theguynextdoor

    This is me every time I view either the plugin dev forums, or more recently, the resources section of said forums.
     
    ArthurMaker and KingFaris11 like this.
  17. Offline

    ArthurMaker

    oh darling, dat gif just made my night happier
     
  18. Offline

    Bloxcraft

    I just use ALT-21 -> §

    I memorized the color codes after about >6 months of using this
     
  19. Offline

    mazentheamazin

    I'm sorry, I just had to do it.
    Why is name Jimfutsu? How do you know that name is Jimfutsu?
     
  20. Offline

    desht

    To be more accurate, never use += (or String#concat(), which is the same thing) on Strings within a loop, which is what the OP was doing here, and it is indeed atrociously bad for performance.

    String concatenation outside a loop is generally OK, because the compiler will almost certainly spot that and convert it to use StringBuilder internally. It can't do that with code inside a loop.
     
  21. Jimfutsu Another problem: Use your code and try to use a phrase like "fish & chips". It won't work out too well.
     
    Plo124 likes this.
  22. Offline

    Plo124

    Cirno
    Dont use .replace() use .replaceAll()
    This way if theres more than 1 '&' symbol, which there probably is, it will replace them all.

    Also, to get the § symbol, hold down alt, and press 2, 1, then release alt. It doesnt work in Minecraft chat.
     
  23. Offline

    L33m4n123


    Well. 2, 1 from numblock ;-) Some Keyboards have it even faster, just simply shift + 3 ;)
     
  24. Plo124 Nope! You might think that from the name of it, but replace() actually does replace all occurrences. The only difference is that replaceAll() uses regex, whereas replace() doesn't. Which means that you should always use replace() unless you're using regex.
     
  25. Offline

    Garris0n

    Compilers are like secret helpers that clean up after you with no recognition. I feel bad for them :(
     
    desht likes this.
Thread Status:
Not open for further replies.

Share This Page