Solved Changing the TAB list.

Discussion in 'Plugin Development' started by Astrophylite, Sep 18, 2015.

Thread Status:
Not open for further replies.
  1. Hello people,

    I am making a massive plugin (much like Essentials), and I am stuck on making their display name in the TAB list change... I have already got this:
    Code:
    package me.zircon.zirconessentials.commands.chat;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class Nickname implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            // When something other than a player sends the command.
            if(!(sender instanceof Player)) {
                if(args.length == 0 || args.length > 2) {
                    sender.sendMessage(ChatColor.RED + "Usage: /" + label + " <nickname> <player>");
                    return false;
                } else {
                    Player target = Bukkit.getPlayer(args[0]);
                    if(target == null) {
                        sender.sendMessage(ChatColor.RED + "That player cannot be found!");
                        return false;
                    } else {
                        String message = args[0];
                        String message2 = message.replaceAll("&", "§");
                        String nickname = message2.replace("§§", "&");
                        target.setDisplayName(nickname);
                        target.setPlayerListName(ChatColor.stripColor(nickname));
                        target.sendMessage(ChatColor.GREEN + "Your nickname was changed to " + nickname + ChatColor.GREEN + " by " + sender.getName() + "!");
                        sender.sendMessage(ChatColor.GREEN + "You changed " + target.getName() + ChatColor.GREEN + "'s nickname to " + nickname + ChatColor.GREEN + "!");
                        return true;
                    }
                }
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("nick")) {
                if(args.length == 0 || args.length > 2) {
                    p.sendMessage(ChatColor.RED + "Usage: /" + label + " <nickname> [player]");
                    return false;
                } else {
                    if(args.length == 1) {
                        String nickname = ChatColor.translateAlternateColorCodes('&', args[0]);
                        p.setDisplayName(nickname);
                        p.setPlayerListName(nickname);
                        p.sendMessage(ChatColor.GREEN + "You changed your nickname to " + nickname + ChatColor.GREEN + "!");
                        return true;
                    } else if(args.length == 2) {
                        Player target = Bukkit.getPlayer(args[1]);
                        if(target == null) {
                            p.sendMessage(ChatColor.RED + "That player cannot be found!");
                            return false;
                        } else {
                            String nickname = ChatColor.translateAlternateColorCodes('&', args[0]);
                            target.setDisplayName(nickname);
                            target.setPlayerListName(nickname);
                            target.sendMessage(ChatColor.GREEN + "Your nickname was set to " + nickname + ChatColor.GREEN + " by " + p.getDisplayName() + ChatColor.GREEN + "!");
                            p.sendMessage(ChatColor.GREEN + "You set " + target.getName() + "'s nickname to " + nickname + ChatColor.GREEN + "!");
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
    This code used to work before I started to use player#setPlayerListName(nickname);
    It is erroring because it's saying the TAB list can only support 16 letters, but I want to be able to use colours for it... Oh, also, ChatColor.translateAlternateColorCodes won't work for some reason, I have to use the replacing method.
     
  2. Offline

    mine-care

    Yeeah, No.
    replaceAll is used for regex'es and i dont see any regex here. Use replace() instead. Also if i understand correctly you try to somehow translate color codes? How about ChatColor.translateAlternateColorCodes(char symbol,Strin message); ans also what you meand it doesnt work?

    Lastly i didnt quite get what the problem is.
     
  3. @mine-care Thanks for the response! I've always used replaceAll, main reason is because replace() won't work in the case of &6text, atleast, not when I've used it. ChatColor.translateAlternateColorCodes, I'm going to try and use again but I'm pretty sure it makes no difference to the length of the nickname. The problem was when I set the nickname, I want it to change on the TAB list aswell, however, with colours, it won't work because the nickname is too long. ;(

    EDIT: It looks like I am already using ChatColor.translateAlternateColourCodes. Yeah, it's still erroring with the fact that the nickname is too long...
     
  4. Offline

    MCMatters

    @_zircon_ all you do is use scoreboard tab packets or deal w/ the 16 letter limit :)
     
Thread Status:
Not open for further replies.

Share This Page