Get ChatColor from string

Discussion in 'Plugin Development' started by JeroenV, Mar 13, 2013.

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

    JeroenV

    Hey,

    I'm currently hooking into factions and for its 'getFactionRelation' method it uses color codes to let you know if it's an 'ENEMY', 'NEUTRAL' or 'ALLY'.

    Now I'm having troubles finding out how to parse the chatcolor (or color code) out of this string. Is there anybody that can help me out?

    Here's the method from factions (if it's any help):
    Code:
        // Same as above, but with relation (enemy/neutral/ally) coloring potentially added to the tag
    public String getPlayerFactionTagRelation(Player speaker, Player listener)
    {
    String tag = "~";
     
    if (speaker == null)
    return tag;
     
    FPlayer me = FPlayers.i.get(speaker);
    if (me == null)
    return tag;
     
    // if listener isn't set, or config option is disabled, give back uncolored tag
    if (listener == null || !Conf.chatParseTagsColored) {
    tag = me.getChatTag().trim();
    } else {
    FPlayer you = FPlayers.i.get(listener);
    if (you == null)
    tag = me.getChatTag().trim();
    else // everything checks out, give the colored tag
    tag = me.getChatTag(you).trim();
    }
    if (tag.isEmpty())
    tag = "~";
     
    return tag;
    }
    From the github: https://github.com/MassiveCraft/Factions/blob/master/src/com/massivecraft/factions/P.java

    Thanks,
    Jeroen V.
     
  2. Offline

    caseif

    The way that I've parsed color codes in the past is by using the vanilla MC color code instead of the Bukkit one (i.e. the ones that use section symbols). I've used it for items, so I don't know how well it will work in chat, but it's worth a shot.

    I realized while typing this that there's a ChatColor.getChatColor() method, or something similar. You could probably find it in the JavaDocs.
     
  3. Offline

    JeroenV

    That doesn't work, the only thing that comes close is ChatColor.valueOf(String name) but here the name should the color name. The main problem is that I'm not sure what kind of color coding factions uses, if I print out the message it has the color already so there's a big chance it's the normal ChatColor. Now I just need to retrieve it from the string..

    Anybody able to help me out?

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

    RealDope

    Kind messy but yeah:
    Code:JAVA
    1.  
    2. public ChatColor getChatColor(String color) {
    3. for(ChatColor c : ChatColor.class.getEnumConstants()) {
    4. if(c.name.equalsIgnoreCase(color)) {
    5. return c;
    6. }
    7. }
    8. return null;
    9. }
    10.  
     
  5. Offline

    JeroenV

    That doesn't work either, if the faction name is 'test' and I print it out, you'll see test in a specific color. However the error says the String ==>
    Now, I've seen this kind of color coding before in minecraft. I'm unsure though where I can find an enum to get the right value.
     
Thread Status:
Not open for further replies.

Share This Page