Solved Ignore the &1,2 etc. more than once

Discussion in 'Plugin Development' started by hellobrad100, Jun 14, 2016.

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



    I am making a friendly title command, which works so far, only I have also added the ability for it to work with the colour codes. &1, &2, &3 etc.

    The problem it has is that it will adopt the colour code and set it when you use the title, however it keeps the &1 at the begining of it. I have substring'd it, but if the player then uses another.. lets say &l for bold, I have the same issue again.

    I've tried the replaceAll() method etc. And I can't find a way on how to do it. Hence why I'm now here.

    Any ideas? Thanks.

            CraftPlayer cp = (CraftPlayer) p;
            String text = title;
            String dob = null;
            String colour = "";
            String bold = "false";
            String italic = "false";
            String underlined = "false";
            String strike = "false";
            if(text.contains("&0")) colour = "black";
            if(text.contains("&1"))    colour = "dark_blue"; 
            if(text.contains("&2")) colour = "dark_green"; 
            if(text.contains("&3")) colour = "dark_aqua"; 
            if(text.contains("&4")) colour = "dark_red"; 
            if(text.contains("&5")) colour = "dark_purple"; 
            if(text.contains("&6")) colour = "gold"; 
            if(text.contains("&7")) colour = "gray"; 
            if(text.contains("&8")) colour = "dark_gray"; 
            if(text.contains("&9")) colour = "blue"; 
            if(text.contains("&a")) colour = "green"; 
            if(text.contains("&b")) colour = "aqua"; 
            if(text.contains("&c")) colour = "red"; 
            if(text.contains("&d")) colour = "light_purple"; 
            if(text.contains("&e")) colour = "yellow"; 
            if(text.contains("&f")) colour = "white"; 
            if(text.contains("&l")) bold = "true"; 
            if(text.contains("&n")) underlined = "true"; 
            if(text.contains("&o")) italic = "true"; 
            if(text.contains("&m")) strike = "true";
            dob = text.substring(2);
            PacketPlayOutTitle t = new PacketPlayOutTitle(EnumTitleAction.TITLE,
                    ChatSerializer.a("{\"text\":\""+ dob + "\",\"color\":\"" + colour + "\",\"bold\":"+bold+",\"italic\":" + italic + 
                            ",\"underlined\":" + underlined + ",\"strikethrough\":"+ strike +"}")
                    , 20, 50, 30);
  2. Offline


    EDIT: Nevermind, making a title.

    If you can't figure this out, something like TitleAPI will help you.
  3. Offline


    Just take the text and remove all those color codes using String#replaceall(colorcode, "");

    [Edit] Just reread your post. Can you post how you were using the .replaceAll method?
  4. Offline


    I've tried so many ways of doing it I forgot the exact way I did do it.. I'm trying to work with this:

                String and = "&"+text.indexOf("&"+1);
                dob = text.replaceAll(and, "");
    Not working and I can see why.. but I can't honestly think of another way of doing it.

    Thanks for replying though
  5. Offline


    message = message.replaceAll("&1", ""+ChatColor.BLUE);
    Should work...
  6. Offline

    I Al Istannen

    If the names are case sensitive, you would need to use "String#replace("&1", "dark_blue")".
    Using the chatcolor enum there is just not needed.

    BUT, i would make it differently. As all the names are the same as the name of the chat color, just in lower case. So that opens some new options:
    for chatColor in ChatColor.values()
    message = message#replace("&" + chatColor#getChar, chatColor#name()#toLowerCase());​

    This will work for every chatcolor there is, also for the formats. You would need to exclude from the list if you don't want that. But it shortens ~ 15 lines to 3 while maintaining the readability, which is an improvement. At least I think so. To each their own.

    If you have a better idea or I made a mistake, point it out!
  7. Offline


    I've decided to scrap the Bold, underline etc, as it really isn't needed.

    But thanks for your help anyway everyone.
Thread Status:
Not open for further replies.

Share This Page