How to give ChatColor.RED a variable

Discussion in 'Plugin Development' started by jacklin213, Sep 8, 2012.

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

    jacklin213

    Im trying to replace all the color codes inside my config.yml to give them color
    eg) if someone uses the command:
    /Myplugin setmessage &a [Server] &f Welcomes you

    how would u replace &a with ChatColor.RED (i tried didnt work)
     
  2. Offline

    Infamous Jeezy

    Please post the code you've tried so far that isn't working.
    I'm not exactly sure what you're trying to achieve.
    Are you trying to make it so you can change the chat color via an argument when typing in a command?
     
  3. Offline

    skore87

    ChatColor.translateAlternateColorCodes("&".charAt(0), "Your Message");
     
  4. Offline

    jacklin213

    i think this will work thanks

    wait does that make all the "&" color change into the correct colors?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  5. Offline

    QuantumDev

    When I did it (I was unaware of ChatColor) I just replaced & with ยง
     
  6. Offline

    mavis

    Look at the second post in this thread for another really easy way to convert the & codes
     
  7. Offline

    jacklin213

    oh i was using that but it didnt have the (?i) which is important

    how can i use this
    Code:java
    1. public static String format(String string) {
    2. String s = string;
    3. for (ChatColor color : ChatColor.values()) {
    4. s = s.replaceAll("(?i)<" + color.name() + ">", "" + color);
    5. }
    6. return s;
    7. }
    8.  

    with this string builder
    Code:java
    1. if (args[0].equalsIgnoreCase("setmessage")) {
    2. StringBuilder sb = new StringBuilder();
    3. for (int i = 0; i < args.length; i++) {
    4. if (i != 0) {
    5. sb.append(' ');
    6. }
    7. sb.append(args);
    8. }
    9.  


    for this
    Code:java
    1. if (args[0].equalsIgnoreCase("setmessage")) {
    2. StringBuilder sb = new StringBuilder();
    3. for (int i = 0; i < args.length; i++) {
    4. if (i != 0) {
    5. sb.append(' ');
    6. }
    7. sb.append(args);
    8. }
    9. this.getConfig().set("Message", sb);
    10. this.saveConfig();
    11. PluginDescriptionFile pdfFile = getDescription();
    12. player.sendMessage(pdfFile.getName()
    13. + " The message has been changed to: "
    14. + sb.toString().replaceAll(
    15. "(&([a-f0-9]))", "\u00A7$2"));
    16. return true;
    17.  
    18.  


    nvm that wont work.
    when i do this
    Code:java
    1. String message = this.getConfig().getString("Message");
    2. message = message.replace("%p", player.getName());
    3. player.sendMessage(format(message));


    it doesnt work but when i do this
    Code:java
    1. String myString = "This <red>message <yellow>is <light_purple>AWESOME!";
    2. sender.sendMessage(format(myString));


    it works...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page