Player player cannot be resolved

Discussion in 'Plugin Development' started by Tofun, Apr 16, 2012.

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

    Tofun

    Currently this is the code on my very first plugin..
    It is used for telling someone else your location.
    The only problem is: I get all the instances of the Player player underlined in red in Eclipse.
    Underlined stuff is marked with [ B][ /B]
    Code:
    package com.github.bastiaantk;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.EventHandler;
     
    public class LocateMe implements Listener {
     
        public static LocateMe plugin;
     
        public void LocateMeListener(LocateMe instance) {
                plugin = instance;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (args.length != 1) {
                sender.sendMessage("Valid use is: /locateme <player>");
                return false;
            }
            Player player2 = (Bukkit.getServer().getPlayer(args[0]));
            if (player2 == null) {
              sender.sendMessage(args[0] + " is niet online!");
              return false;
            } return false;
            if(cmd.getName().equalsIgnoreCase("locateme") && sender instanceof Player)
                [B]Player player[/B] = (Player) sender;
                Location playerLoc = player.getLocation();
         
                player2.sendMessage([B]player[/B] + "'s X-coordinates are: " + playerLoc.getX());
                player2.sendMessage([B]player[/B] + "'s Y-coordinates are: " + playerLoc.getY());
                player2.sendMessage([B]player[/B] + "'s Z-coordinates are: " + playerLoc.getZ());
            } return false;
        }
     
    }
     
  2. Offline

    Prgr

    Try this my friend
    Code:java
    1.  
    2. public class LocateMe implements Listener {
    3.  
    4. public static LocateMe plugin;
    5.  
    6. public void LocateMeListener(LocateMe instance) {
    7. plugin = instance;
    8. }
    9.  
    10. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    11. Player player = null;
    12. if (args.length != 1) {
    13. sender.sendMessage("Valid use is: /locateme <player>");
    14. return false;
    15. }
    16. Player player2 = (Bukkit.getServer().getPlayer(args[0]));
    17. if (player2 == null) {
    18. sender.sendMessage(args[0] + " is niet online!");
    19. return false;
    20. }
    21. if(cmd.getName().equalsIgnoreCase("locateme")&&(sender instanceof Player)){
    22. player = (Player) sender;
    23. Location playerLoc = player.getLocation();
    24. player2.sendMessage(player + "'s X-coordinates are: " + playerLoc.getX());
    25. player2.sendMessage(player + "'s Y-coordinates are: " + playerLoc.getY());
    26. player2.sendMessage(player + "'s Z-coordinates are: " + playerLoc.getZ());
    27. return true;
    28. } return false;
    29. }
    30.  
    31. }
    32.  


    I just initiated the variable "player" at the beginning with
    Code:java
    1. Player player = null;

    Look at its placement in above code!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  3. Offline

    Tofun


    Thanks for your support, I'll try that this evening.
    I'm not sure though how that would get the player that sends the command..
    Wouldn't this work better?

    Code:text
    1. Player player = (Player) sender;
     
  4. problem lines:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("locateme") && sender instanceof Player)
    2. [B]Player player[/B] = (Player) sender;
    just to help your out, its only defined at the context of the if-block, what isn;t allowed at the only stament, after that, its not definied in that context
     
  5. Offline

    Njol

    The actual problem is that you forgot an opening curly bracket after the if statement
    if(cmd.getName().equalsIgnoreCase("locateme") && sender instanceof Player)

    BTW: you have a return false; just above that if statement which will prevent the code to ever reach the statement.
     
    Tofun likes this.
  6. Offline

    Tofun

    Thanks! That was exactly the solution :)
     
  7. Offline

    Prgr

    Look at my code. It has to do with adding this in the beginning of the CommandExecutor . Not replacing anything.
     
  8. Offline

    Njol

    You should've probaly added some explanation to your code, e.g. what you changed and why.
     
  9. Offline

    Tofun

    Ah didn't see that, but I don't really see why I would add extra code to declare Player if I could do it in one line. Why do I need to place it before the CmdExe?
    This afternoon I tried my running the plugin, but it threw a ClassNotFound error.
    Does anybody see where that comes from?
    I'm still really newb at this, but I'm trying...
     
  10. Offline

    Prgr

    By placing it before the CmdExe, you are leaving a placeholder that you are able to then modify, I am not absolutely sure why but I believe it is to protect the plugin from accidentally accessing a variable that is not declared yet.

    Just a tip
    When I first loaded your code from the first post into Eclipse, right off the bat it caught a few errors, and so I suggest you switch to a more capable IDE like Eclipse, it save you a lot of time.
     
Thread Status:
Not open for further replies.

Share This Page