Help with translateAlternateColorCodes

Discussion in 'Plugin Development' started by Sam1370, Oct 8, 2016.

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

    Sam1370

    Hey, I really need help on the translateAlternateColorCodes stuff. I couldn't get it to work so I had to settle with a bunch of if statements. How do you use it? Thanks!! (I'm new to Java and coding)
     
  2. Offline

    Zombie_Striker

    @Sam1370
    First, learn Java before working on bukkit. Trust me, you will get confused or stuck and the only way for us to help you would be if we wrote your plugin for you (I have lost count how many times this has happened) Please, if learn Java first before working on Bukkit. If you do not have a Java tutorial already, please select one from the following list HERE.

    To translate color codes, use the following
    Code:
    String translated = ChatColor.translate......('char that will be translated', "The string that will be translated")
    replace "char that will be translated" with the char (e.g. '&') and "The string that will be translated" with the string with the color codes.
     
  3. Offline

    Sam1370


    Well, I watched the first nine tutorials of a Java tutorial set that had, like, 150 videos, but I got bored xD I do have a basic knowledge of Java now...on my post I said "new" sorry if that wasn't clear enough
     
  4. Offline

    mythbusterma

  5. Offline

    Sam1370

    Well, kind of -- I got an error when I tried to use translateAlternateColorCodes in my plugin -- it may just be an error somewhere else because I only tested it once. I am testing it right now with a bunch of if statements:
    Code:
    if (localSpyMessage.contains("&4")) {
                        localSpyMessage = localSpyMessage.replace("&4", ChatColor.DARK_RED + "");
                    }
    
                    if (localSpyMessage.contains("&c")) {
                        localSpyMessage = localSpyMessage.replace("&c", ChatColor.RED + "");
                    }
    
                    if (localSpyMessage.contains("&6")) {
                        localSpyMessage = localSpyMessage.replace("&6", ChatColor.GOLD + "");
                    }
    
                    if (localSpyMessage.contains("&e")) {
                        localSpyMessage = localSpyMessage.replace("&e", ChatColor.YELLOW + "");
                    }
    
                    if (localSpyMessage.contains("&2")) {
                        localSpyMessage = localSpyMessage.replace("&2", ChatColor.DARK_GREEN + "");
                    }
    
                    if (localSpyMessage.contains("&a")) {
                        localSpyMessage = localSpyMessage.replace("&a", ChatColor.GREEN + "");
                    }
    
                    if (localSpyMessage.contains("&b")) {
                        localSpyMessage = localSpyMessage.replace("&b", ChatColor.AQUA + "");
                    }
    
                    if (localSpyMessage.contains("&3")) {
                        localSpyMessage = localSpyMessage.replace("&3", ChatColor.DARK_AQUA + "");
                    }
    
                    if (localSpyMessage.contains("&1")) {
                        localSpyMessage = localSpyMessage.replace("&1", ChatColor.DARK_BLUE + "");
                    }
    
                    if (localSpyMessage.contains("&9")) {
                        localSpyMessage = localSpyMessage.replace("&9", ChatColor.BLUE + "");
                    }
    
                    if (localSpyMessage.contains("&d")) {
                        localSpyMessage = localSpyMessage.replace("&d", ChatColor.LIGHT_PURPLE + "");
                    }
    
                    if (localSpyMessage.contains("&5")) {
                        localSpyMessage = localSpyMessage.replace("&5", ChatColor.DARK_PURPLE + "");
                    }
    
                    if (localSpyMessage.contains("&f")) {
                        localSpyMessage = localSpyMessage.replace("&f", ChatColor.WHITE + "");
                    }
    
                    if (localSpyMessage.contains("&7")) {
                        localSpyMessage = localSpyMessage.replace("&7", ChatColor.GRAY + "");
                    }
    
                    if (localSpyMessage.contains("&l")) {
                        localSpyMessage = localSpyMessage.replace("&l", ChatColor.BOLD + "");
                    }
    
                    if (localSpyMessage.contains("&n")) {
                        localSpyMessage = localSpyMessage.replace("&n", ChatColor.UNDERLINE + "");
                    }
    
                    if (localSpyMessage.contains("&o")) {
                        localSpyMessage = localSpyMessage.replace("&o", ChatColor.ITALIC + "");
                    }
    
                    if (localSpyMessage.contains("&k")) {
                        localSpyMessage = localSpyMessage.replace("&k", ChatColor.MAGIC + "");
                    }
    
                    if (localSpyMessage.contains("&m")) {
                        localSpyMessage = localSpyMessage.replace("&m", ChatColor.STRIKETHROUGH + "");
                    }
    
                    if (localSpyMessage.contains("&r")) {
                        localSpyMessage = localSpyMessage.replace("&r", ChatColor.RESET + "");
                    }
     
  6. Offline

    I Al Istannen

    @Sam1370
    You don't need the if, replace won't do anything if the sequence is not found.

    And there is no error with ChatColor#translateAlternateColorCodes('&', string)
    Just use it and show the code where you use it.
     
  7. Offline

    Firestar311

    There are two ways to get color codes, the safest way in case they change the behind the scenes character for chat colors (§, This is the Alt+21 key combo) is to use the ChatColor.translateAlternateColorCodes(char, newChar).
    The other method (This will break if they change the § symbol) is to replace & with §.
    Code:java
    1. variable = variable.replace("&", "§");
     
  8. Offline

    I Al Istannen

    @Firestar311
    And if you use your other method, something like "You & I" won't print as expected.
     
  9. Offline

    Sam1370

    Okay, thanks!
    Thanks! I went back to watching Java tutorials even though they're a little bit boring (xD) but thanks a lot!
     
  10. @Firestar311 No please do not recommend that! Always just recommend ChatColor.translateAlternateColorCodes there is no need recommending something that could be very easily broken!
     
Thread Status:
Not open for further replies.

Share This Page