Replace something with something else?

Discussion in 'Plugin Development' started by NerdsWBNerds, May 25, 2012.

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

    NerdsWBNerds

    I have a config file which loads correctly, in this a person can enter a message to be displayed, I want it so it replaces things like *GREEN* with the corresponding ChatColor, how could I do this?
     
  2. Offline

    LucasEmanuel

    I cant understand what it is that you want to do. Can you give a better description?
     
  3. Offline

    Theodossis

    make a utilitie:
    Code:
    public static String colorFormat(String string){
            String format = string.replace("&0", ChatColor.BLACK.toString());
            format = format.replace("&1", ChatColor.DARK_BLUE.toString());
            format = format.replace("&2", ChatColor.DARK_GREEN.toString());
            format = format.replace("&3", ChatColor.DARK_AQUA.toString());
            format = format.replace("&4", ChatColor.DARK_RED.toString());
            format = format.replace("&5", ChatColor.LIGHT_PURPLE.toString());
            format = format.replace("&6", ChatColor.GOLD.toString());
            format = format.replace("&7", ChatColor.GRAY.toString());
            format = format.replace("&8", ChatColor.DARK_GRAY.toString());
            format = format.replace("&9", ChatColor.BLUE.toString());     
            format = format.replace("&a", ChatColor.GREEN.toString());
            format = format.replace("&b", ChatColor.AQUA.toString());
            format = format.replace("&c", ChatColor.RED.toString());
            format = format.replace("&e", ChatColor.YELLOW.toString());
            format = format.replace("&f", ChatColor.WHITE.toString());
     
            return format;
        }
    Support it:
    Code:
    utilitiename.colorFormat(//code here);
     
  4. Offline

    Sorroko

    An easier and more efficient verison of the above is this:
    Code:
    .replaceAll("(&([a-f0-9]))", "\u00A7$2")
    Use it like this:
    Code:
    String myString = "Hello &7World &5Colors!";
     
    String myColorString = myString.replaceAll("(&([a-f0-9]))", "\u00A7$2");
     
    player.sendMessage(myColorString);
     
Thread Status:
Not open for further replies.

Share This Page