Rainbow color, how to do it?

Discussion in 'Plugin Development' started by Dragon4c3, Aug 28, 2015.

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

    Dragon4c3

    Hey I've made a simple plugin(you can check it out if you want, http://dev.bukkit.org/bukkit-plugins/color-changer/) all the plugin does is whenever you type in /color <color> it runs the command
    p.setDisplayName(ChatColor.<COLOR> + p.getName() + ChatColor.RESET);
    p.sendMessage(ChatColor.YELLOW + "Your name has changed to " + ChatColor.RESET + ChatColor.<COLOR> + p.getName());. And my question is is there a way to get rainbow as a color, even if it's not possible do you know of any plugins that make it possible?
     
  2. I wrote a color class a while back that you might want to have a look at, I added some comments to help you undestand:

    Code:
    /**
         * Rainbowizes a given message.
         *
         * @param msg The message you would like to rainbowize
         * @param includeBlack Whether or not the array of colors will include black
         * @return The message in it's rainbow form.
         */
        public String rainbow(String msg, boolean includeBlack) {
            // Converting the message to an array of characters.
            char[] c = msg.toCharArray();
           
            String newMsg = "";
            // Color index
            int color;
            // Setting the color index to an appropriate number.
            if (!(includeBlack))
                color = 1;
            else
                color = 0;
            // Looping through each character in the array.
            for (char ch : c) {
                // Setting the message to a color using a custom translate method (ChatColor.translateAlt..)
                newMsg += trans("&" + color + ch);
                // Adding to the color index
                color++;
               
                // Resetting the color appropriately
                if (color == 9)
                    if (!(includeBlack))
                        color = 1;
                    else
                        color = 0;
            }
            // Returning the newly crafted message and resetting the colors.
            return newMsg + trans("&r");
        }
    (Don't copy it. Look at it, understand it then make your own version :))
     
  3. Offline

    au2001

    @MrBlackIsBack I'd personally would have done this:
    Create a List of ChatColors containing all the possible values:
    Code:
    List<ChatColor> colors = new ArrayList<ChatColor>(Arrays.asList(ChatColor.values()));
    colors.remove(ChatColor.RESET);
    if (!includeBlack)
        colors.remove(ChatColor.BLACK);
    Remove the unwanted ones (in your example, there is a boolean for black).

    In each loop, add the character with the color:
    Code:
    newMsg += colors.get(color) + "" + ch;
    And if the color is out of bound, reset it:
    Code:
    if (color >= colors.size())
        color = 0;
    EDIT: Thanks to @megamichiel's comment, here's a fix for color formats:
    Create a new boolean argument called "formats", after the includeBlack one.
    Then do (just after the includeBlack code):
    Code:
    if (!format) {
        colors.remove(ChatColor.BOLD);
        colors.remove(ChatColor.UNDERLINE);
        colors.remove(ChatColor.ITALIC);
        colors.remove(ChatColor.MAGIC);
        colors.remove(ChatColor.STRIKETHROUGH);
    }
    You could also pass a list of unwanted colors, and use List#removeAll().
     
    Last edited: Aug 28, 2015
    mrgreen33gamer and MrBlackIsBack like this.
  4. @au2001
    ChatColor also contains all chat formats.
     
    mrgreen33gamer and MrBlackIsBack like this.
  5. Offline

    au2001

    @megamichiel Updated, with chat formats and a ChatColor.RESET fix. Thank you for this reminder :)
     
Thread Status:
Not open for further replies.

Share This Page