Solved CraftPlayer{name="username"}

Discussion in 'Plugin Help/Development/Requests' started by Eich_, Apr 19, 2015.

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

    Eich_

    Hello. I've been coding a plugin to format chat, and one of the commands is /pm (standing for Private Message). The problem is that when I use Bukkit.getPlayer(args[0]) (the 0th arg is the player name), the output is "CraftPlayer{name=Eich}" (if you put my username in for the PM.)

    I don't know how to fix this, and it's really bothering me. Here's the code.
    Code:
            @Override
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                Player p = Bukkit.getPlayer(sender.getName());
                if (cmd.getName().equalsIgnoreCase("pm"));
                    if (args.length < 2){
                        p.sendMessage(ChatColor.RED + "Improper Synax! Use /pm (the other player's name) (message)");
                        return false;
                    }
                    if (args.length >= 2){
                        String msg = "";
                        for (int i = 1; i < (args.length); i++) msg+= args[i] + " ";                   
                        Player target = Bukkit.getPlayer(args[0]);
                   
                       
                    if (target != null){
                       
                        p.sendMessage(ChatColor.AQUA + "You " + ChatColor.YELLOW + " > " + ChatColor.AQUA + target + ":" + ChatColor.ITALIC + " " + ChatColor.GRAY + msg);
                        target.sendMessage(ChatColor.AQUA + p.getName()  + ChatColor.YELLOW + " > " + ChatColor.AQUA + "You" + ChatColor.ITALIC + ": " + ChatColor.GRAY + msg);
                    }
           
                        if (target == null){
                            p.sendMessage(ChatColor.RED + "Error: That player is no longer online.");
                            return true;
                           
               
               
           
                        }
            }
               
                return false;
    
    I'm having the problem with the TARGET. Thanks for the help, everyone!
     
  2. Offline

    bwfcwalshy Retired Staff

    @Eich_ Check sender before casting. Do #getName or #getDisplayName to output their name. #getName does their IGN, #getDisplayName does nickname, prefix, suffix etc.
     
  3. Offline

    Eich_

    @bwfcwalshy, thank you so much! What a silly mistake. It's working perfectly now. :)
     
  4. Offline

    bwfcwalshy Retired Staff

    @Eich_ Please mark this thread as Solved. See here for more info.
     
Thread Status:
Not open for further replies.

Share This Page