[SOLVED] Change Players name in TabList.

Discussion in 'Spigot Plugin Development' started by BowWhalley, May 14, 2015.

  1. I am making a plugin and I want it so I can do /nametag <color> <player>.
    I got it so I can set the color to myself but I want it so I can also set it to a seperate player. The problem is that I enter it as an argument so its not gonna work.

    The plugin changes the playerListName Color of the player.

    Code (Text):
    package me.bowwhalley.nametag;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin{
       
        public void onEnable() {
       
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("nametag")){
                if(args[0].equalsIgnoreCase("blue")){
                    Player p = (Player) sender;
                    p.setPlayerListName(ChatColor.BLUE + p.getName());
                    p.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                }
               
                if(args[0].equalsIgnoreCase("red")){
                    Player p = (Player) sender;
                    p.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                }
               
                if(args[0].equalsIgnoreCase("green")){
                    Player p = (Player) sender;
                    p.setPlayerListName(ChatColor.GREEN + p.getName());
                    p.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                }
               
                if(args[0].equalsIgnoreCase("reset")){
                    Player p = (Player) sender;
                    p.setPlayerListName(ChatColor.WHITE + p.getName());
                    p.sendMessage(ChatColor.GRAY + "Your name has been reset to " + p.getPlayerListName());
                }
               
               
            }
            return false;
        }
       
    }
     
     
  2. Code (Java):
    Player target = Bukkit.getPlayer(string);
     
    • Agree Agree x 2
  3. .toString()
     
    • Optimistic Optimistic x 1
  4. Thanks, I also tried to add a custom msg but now when I try the plugin it comes up with internal error and prints this in the console:
    Code (Text):
    [14:02:47 ERROR]: null

    org.bukkit.command.CommandException: Unhandled exception executing command 'nametag' in plugin ColoredNameTags v1.0

    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-870264a-0a645a2]

    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-870264a-0a645a2]

    at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1139) [spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:974) [spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-870264a-0a645a2]

    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_67]

    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_67]

    at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:712) [spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot.jar:git-Spigot-870264a-0a645a2]

    at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot.jar:git-Spigot-870264a-0a645a2]

    at java.lang.Thread.run(Thread.java:745) [?:1.7.0_67]

    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2

    at me.bowwhalley.nametag.main.onCommand(main.java:20) ~[?:?]

    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-870264a-0a645a2]

    heres the code of the plugin:
    Code (Text):
    package me.bowwhalley.nametag;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin{
     
        public void onEnable() {
     
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("nametag")){
                Player target = Bukkit.getPlayer(toString());
                String msg = args[2];
                if(args[0].equalsIgnoreCase("blue")){
                    Player p = (Player) sender;
                    if (msg.length() >= 0) {
                    target.setPlayerListName(ChatColor.BLUE + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + msg);
                    } else {
                    target.setPlayerListName(ChatColor.BLUE + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                    }
                    }
             
                if(args[0].equalsIgnoreCase("red")){
                    Player p = (Player) sender;
                    if (msg.length() >= 0) {
                        target.setPlayerListName(ChatColor.RED + p.getName());
                        target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + msg);
                        } else {
                        target.setPlayerListName(ChatColor.RED + p.getName());
                        target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                        }
                }
             
                if(args[0].equalsIgnoreCase("green")){
                    Player p = (Player) sender;
                    if (msg.length() >= 0) {
                        target.setPlayerListName(ChatColor.GREEN + p.getName());
                        target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + msg);
                        } else {
                        target.setPlayerListName(ChatColor.GREEN + p.getName());
                        target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                        }
                }
             
                if(args[0].equalsIgnoreCase("reset")){
                    Player p = (Player) sender;
                    target.setPlayerListName(ChatColor.WHITE + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been reset to " + p.getPlayerListName());
                }
             
             
            }
            return false;
        }
     
    }
     
     
  5. Now that you've added the message as a string, it's taken from args[2], but if you don't provide a message in the command (for eg: you type only "/nametag blue GODofCRAFTERS") it will show an error. I suggest enclosing the assignment in a try and catch statement or check the args.length to be 3 exactly.

    Also the message will only be considered as one word. You might want to think about how strings are taken in as arguments. I'd suggest dropping the message idea for now, if you're not ready for it. But if you need it, you need to form a loop to input the words as args separately and then join them to a String variable or use the Java StringBuilder to make your message.
     
  6. 1. How can I do that?
    2. I'm happy with leaving the msg as 1 word.
     
  7. The proper rule of thumb for arguments is to check the length, at least that's what I've observed for all this time. Just enclose the code block with an if statement:
    Code (Text):
    if(args.length == 3){
    //code block here
    }
     
  8. And this line is horribly wrong:
    Code (Text):
     Player target = Bukkit.getPlayer(toString());
    It should be:
    Code (Text):
     Player target = Bukkit.getPlayer(args[1]);
     
  9. I am still getting the error.

    Code (Text):
    package me.bowwhalley.nametag;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin{
     
        public void onEnable() {
     
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("nametag")){
                Player target = Bukkit.getPlayer(args[1]);
                String msg = args[2];
                if(args[0].equalsIgnoreCase("blue")){
                    Player p = (Player) sender;
                 
                    if(args.length == 3){
                            if (msg.length() >=0){
                                target.setPlayerListName(msg);
                                target.sendMessage(ChatColor.GRAY + "Your name has been set to " + target.getPlayerListName());
            }  
        }
                 
                 
                    if(args[0].equalsIgnoreCase("Blue")){
                    target.setPlayerListName(ChatColor.BLUE + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + target.getPlayerListName());
                    }
                }
             
                if(args[0].equalsIgnoreCase("red")){
                    Player p = (Player) sender;
                        target.setPlayerListName(ChatColor.RED + p.getName());
                        target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + target.getPlayerListName());
                     
                }
             
                if(args[0].equalsIgnoreCase("green")){
                    Player p = (Player) sender;
                        target.setPlayerListName(ChatColor.GREEN + p.getName());
                        target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + target.getPlayerListName());
                        }
             
                if(args[0].equalsIgnoreCase("reset")){
                    Player p = (Player) sender;
                    target.setPlayerListName(ChatColor.WHITE + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been reset to " + target.getPlayerListName());
                 
                }
             
             
            }
            return false;
        }
     
    }
     
     
  10. @BowWhalley Please post the command you type and also the error.
     
  11. @BowWhalley
    If the player was not found, it will throw a NullPointerException as it will return null. You need to check if (target == null). If it is then tell them the player wasnt found and return
     
  12. Possible Reasons for Errors in the Code above...
    • If only 1 Arg is given - ERROR
    • If not a single arg is given - ERROR
    • If args[0] is not reset, green, red, blue - ERROR
    So, first of all, check how many Arguments you have, maybe like this...
    Code (Text):
                    if(args.length == 0) {
                   
                    } else if (args.length == 1) {
                   
                    } else if (args.length == 2) {
                            if (msg.length() >=0){
                                target.setPlayerListName(msg);
                                target.sendMessage(ChatColor.GRAY + "Your name has been set to " + target.getPlayerListName());
                            }
                    } else if (args.length >= 3) {
                   
                    }
    Then... add else if and else when checking for your colour...
    Code (Text):

            if(args[0].equalsIgnoreCase("Blue")){
               target.setPlayerListName(ChatColor.BLUE + p.getName());
               target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + target.getPlayerListName());
             } else if(args[0].equalsIgnoreCase("red")){
               Player p = (Player) sender;
                 target.setPlayerListName(ChatColor.RED + p.getName());
                 target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + target.getPlayerListName());
             } else if(args[0].equalsIgnoreCase("green")){
               Player p = (Player) sender;
                 target.setPlayerListName(ChatColor.GREEN + p.getName());
                 target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + target.getPlayerListName());
             } else if(args[0].equalsIgnoreCase("reset")){
               Player p = (Player) sender;
               target.setPlayerListName(ChatColor.WHITE + p.getName());
               target.sendMessage(ChatColor.GRAY + "Your name has been reset to " + target.getPlayerListName());
             } else {
               //Do whatever if args[0] is not one of the above.
             }
     
     
    • Useful Useful x 1
  13. I'm a bit confused there, will it still allow me to use a custom message as a listname?
     
  14. Why are you importing 'import net.md_5.bungee.api.ChatColor;'? It should be 'import org.bukkit.ChatColor'
     
    • Like Like x 1
  15. There's an error with line 20
    Code (Text):
    String msg = args[2];
     
  16. what do I do then?
     
  17. Well first of all listen to this...
     
  18. So there are now 3 arguments?
     
  19. Are you wanting to get a certain player from the server? Because your getting the player that runs the command rather than getting a different player.
     
  20. I found a way of doing it but somethings wrong.


    Error from Console:
    Code (Text):
    [16:50:54 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'nametag' in plugin ColoredNameTags v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1139) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:974) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-870264a-0a645a2]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_67]
        at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_67]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:712) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot.jar:git-Spigot-870264a-0a645a2]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot.jar:git-Spigot-870264a-0a645a2]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_67]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.bowwhalley.nametag.main.onCommand(main.java:19) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-870264a-0a645a2]
        ... 15 more

    Plugin.yml
    Code (Text):
    main: me.bowwhalley.nametag.main
    name: ColoredNametags
    version: 1.0
    site: twitter.com/bowwhalley
    author: bowwhalley
    commands:
      nametag:
        usage: /nametag <color>
        description: Changes color of nametag.
        permission: nametag.color
      nametagname:
        usage: /nametagname:
        description: Changes name of nametag.
        permission: nametag.name

    Code from Plugin.
    Code (Text):
    package me.bowwhalley.nametag;


    import org.bukkit.ChatColor;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin{
     
        public void onEnable() {
     
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("nametag")){
                Player target = Bukkit.getPlayer(toString());
                if(args[0].equalsIgnoreCase("blue")){
                    Player p = (Player) sender;
                    target.setPlayerListName(ChatColor.BLUE + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                }
             
                if(args[0].equalsIgnoreCase("red")){
                    Player p = (Player) sender;
                    target.setPlayerListName(ChatColor.RED + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                }
             
                if(args[0].equalsIgnoreCase("green")){
                    Player p = (Player) sender;
                    target.setPlayerListName(ChatColor.GREEN + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been changed to " + p.getPlayerListName());
                }
             
                if(args[0].equalsIgnoreCase("reset")){
                    Player p = (Player) sender;
                    target.setPlayerListName(ChatColor.WHITE + p.getName());
                    target.sendMessage(ChatColor.GRAY + "Your name has been reset to " + p.getPlayerListName());
                }
             
             
            }
            return false;
        }
        public boolean onCommand1(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("nametagname")){
                Player p = (Player) sender;
                StringBuilder str = new StringBuilder();
                for(int i = 0; i < args.length; i++){
                str.append(args[i] + " ");
                p.setPlayerListName(str.toString().replace("&", "ยง"));
                p.sendMessage(ChatColor.GRAY + "Your name has been set to " + p.getPlayerListName());
             
            }
        }
            return false;
     
        }
    }