Solved Color Codes

Discussion in 'Plugin Development' started by MrAwellstein, Apr 29, 2014.

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

    MrAwellstein

    I need to parse minecraft color coded (written as &<#> or can be §<#>) in my program and I don't understand how I should do this. I have a method that allows me to print in color (print(String text, Color color)), so all I need is to parse it. I would look at how minecraft does this but I don't have access to the source code right now.
    Any Ideas?
     
  2. Offline

    AdamQpzm

    MrAwellstein
    Code:
    ChatColor.translateAlternateColorCodes('&', "String to translate");
     
  3. Offline

    MrAwellstein

    This isn't part of the plugin but an application for the plugin.
     
  4. Offline

    AdamQpzm

    MrAwellstein Oh I see. Well here's the source for that method, copied from https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/ChatColor.java

    PHP:
    public static String translateAlternateColorCodes(char altColorCharString textToTranslate) {
            
    char[] textToTranslate.toCharArray();
            for (
    int i 0b.length 1i++) {
                if (
    b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i+1]) > -1) {
                    
    b[i] = ChatColor.COLOR_CHAR;
                    
    b[i+1] = Character.toLowerCase(b[i+1]);
                }
            }
            return new 
    String(b);
        }
     
  5. Offline

    MrAwellstein

    I think this gave me an idea how to do what I need to do. Thanks.
     
    AdamQpzm likes this.
  6. Offline

    badboysteee98

    If you want it so when you send a message you can send it like this

    Code:java
    1. player.sendMessage("&aThis will bring the Color green and hide the &a :)");

    Then I know how :)
     
  7. Offline

    MrAwellstein


    I really wasnt making myself clear. I needed to know how to parse color on my application, not the plugin, based off of the print(String, color) method I had.
    The way I did this was...

    Code:java
    1. public String colorSymbol = "§";
    2.  
    3. public void appendString(String text){
    4. if(!text.contains(colorSymbol)){getPanel().setColor(Color.BLACK).append(text);return;}
    5. text = "n"+text;
    6. for(String c: text.split(colorSymbol)){getPanel().setColor(ViewPanel.getColor(c.toCharArray()[0])).append(c.replaceFirst(c.toCharArray()[0]+"", ""));}
    7. getPanel().setColor(Color.black);
    8. }
    9. }


    Wasn't the best way to do it, but it worked.
     
Thread Status:
Not open for further replies.

Share This Page