Player cannot be resolved

Discussion in 'Plugin Development' started by WALK2222, Feb 27, 2013.

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

    WALK2222

    Hi there,
    I am trying to call this variable "player" but for some reason I can't. Its probably something super simple but I just can't figure it out :/

    (code below)

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
            if(cmd.getName().equalsIgnoreCase("playerinfo")){
                Player target = sender.getServer().getPlayer(args[0]);
     
                if(target == null){
                    //If the player is offline
                    Player player = Bukkit.getPlayer(sender.getName());
                    player.sendMessage("That person is NOT online!");
                }else{
                    //If the player is online
                    PlayerLocation.getlocation(target, sender);
                }
                //If the player does not enter any parameters
                if(label.equalsIgnoreCase("playerinfo")){
     
           //Error here V
                    player.sendMessage("Wrong parameters!  You need a usernamename after playerinfo!");
                }
            }
            return false;
        }
    Thanks!
     
  2. Offline

    Technius

    In the playerinfo label check, the variable "player" does not exist.

    Why are you checking the command label of the command? It's redundant.

    Insert an argument check (args.length == 1) before you do stuff or else you may get errors when the users do not enter arguments.
     
  3. Offline

    WALK2222

    Alright, I understand. I was trying to find if the user didn't enter any arguments with that command label (which is obviously much easier with args.length)
    Thanks!
     
  4. Offline

    Technius

    Yeah, that stuff is done with the arg.length check. Command label is just the command that the player typed in. If your command is "/foo" but the player typed in its alias of "/bar", then the command label will be "bar". Command.getName() will return "foo".
     
  5. Offline

    number1_Master

    Aside from using CommandLabel, I recommend checking if the sender is an instance of a player using this code:
    Code:java
    1. if(sender instanceof Player) // Do stuff
     
Thread Status:
Not open for further replies.

Share This Page