Solved Nicknames with color

Discussion in 'Plugin Development' started by Randomguy, Nov 3, 2015.

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

    Randomguy

    Well, the title explains what I'm going for. I'm making a plugin for my friends server, and I'm not sure how to do this... I have this (Scroll down past this code to see the problem):
    Code:
    if(commandLabel.equalsIgnoreCase("Nick"))
            {
                if(sender.hasPermission("spawnplugin.nick") || sender.isOp())
                {
                    if(args.length == 2)
                    {
                        @SuppressWarnings("deprecation")
                        Player t = Bukkit.getPlayerExact(args[0]);
                       
                        if(t == null)
                        {
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "That player is either offline or doesn't exist!");
                        }
       
                        if(args[1].contains("&0")) //PROBLEM
                        {
                            String[] color = args[1].split("&0");
                               
                            for(int i = 0; i < color.length; i++)
                            {
                                if(i != 1)
                                {
                                    color[i] = ChatColor.BLACK + color[i];
                                }
                            }
                        } //PROBLEM END
                       
                        if(args[1].length() > getConfig().getInt("nick"))
                        {
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Nickname too long! Max number of letters is " + getConfig().getInt("nick") + "!");
                            return true;
                        }
                        if(args[1].equalsIgnoreCase("&norm&"))
                        {   
                            t.setDisplayName(t.getName());
                            t.setCustomName(t.getName());
                           
                            if(sender instanceof Player)
                            {
                                Player p = (Player) sender;
                                t.sendMessage(ChatColor.DARK_GREEN + "Your nickname was changed to your normal name by " + p.getName() + "!");
                            }
                            else
                            {
                                t.sendMessage(ChatColor.DARK_GREEN + "Your nickname was changed to your normal name by console!");
                            }
                        }
                        else
                        {
                            t.setDisplayName(args[1]);
                            t.setCustomName(args[1]);
                           
                            if(sender instanceof Player)
                            {
                                Player p = (Player) sender;
                                t.sendMessage(ChatColor.DARK_GREEN + "Your nickname was changed to " + args[1] + " by " + p.getName() + "!");
                            }
                            else
                            {
                                t.sendMessage(ChatColor.DARK_GREEN + "Your nickname was changed to " + args[1] + " by Console!");
                            }
                        }
                    }
                    else
                    {
                        sender.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "You entered the command wrong!");
                    }
                }
                else
                {
                    sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission!");
                }
            }
    Well, it works and everything, except for the color. If you don't want to look for what isn't working here it is:
    Code:
      if(args[1].contains("&0")) //PROBLEM
                        {
                            String[] color = args[1].split("&0");
                              
                            for(int i = 0; i < color.length; i++)
                            {
                                if(i != 1)
                                {
                                    color[i] = ChatColor.BLACK + color[i];
                                }
                            }
                        } //PROBLEM END
    You probably can tell what is wrong. I know this doesn't work. I'm trying to add color using the formatting codes, starting with &0 (black). This did not work. If I add &0 in my nickname it does not turn black... I know this wouldn't work but I wanted to try... thought I could improvise... Sorry if the code is messy... I find it hard to read also...
     
  2. Offline

    Xerox262

    You need a return in here
    Code:
     if(t == null)
                        {
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "That player is either offline or doesn't exist!");
                        }
    
    Otherwise it'll throw errors anyway, and for
    Code:
    if(args[1].contains("&0")) //PROBLEM                  {
                            String[] color = args[1].split("&0");
                             
                            for(int i = 0; i < color.length; i++)
                            {
                                if(i != 1)
                                {
                                    color[i] = ChatColor.BLACK + color[i];
                                }
                            }
                        } //PROBLEM END
    Why not just replace &0 with ChatColor.BLACK?
    For example
    Code:
    String nickname = args[1];
    nickname.replace("&0", ChatColor.BLACK.toString());
    Also means you can restrict colors to certain permissions
     
  3. Offline

    Monollyth

    If you're looking to achieve colored nicknames, you can just use:

    Code:
    player.setDisplayName(ChatColor.translateAlternateColorCodes('&', args[i]));
     
  4. Offline

    Randomguy

    Would this work for only the text after the &0 though? not before...
     
  5. Offline

    Scimiguy

    @Randomguy
    In Monollyth's Exact code, every single place in args[1] where "&" is present, Java will attempt to replace it with a ChatColor.

    It's basically a modified replaceAll(Regex, String)
     
  6. Offline

    Randomguy

    Ok thanks!
     
Thread Status:
Not open for further replies.

Share This Page