[SOLVED] :Get position of a specified player onCommand? Not working.

Discussion in 'Plugin Development' started by ZeusAllMighty11, May 16, 2012.

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

    ZeusAllMighty11

    I'm implementing this into my code, so I don't want to post waht I have already. I know the basic imports.


    So it should be something like:

    Code:java
    1.  
    2. package obbystrength;
    3.  
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6.  
    7. import obbystrength.obbystrength;
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.Location;
    10. import org.bukkit.Material;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Entity;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.entity.EntityExplodeEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class posget {
    23.  
    24. public boolean onCommand( CommandSender sender, Command cmd, String label,String[] args )
    25. {
    26. Player s = (Player)sender;
    27. if (cmd.getName().equalsIgnoreCase("posget"))
    28. {
    29. if (args.length == 1) // If the length is zero, there are no arguments...
    30. {
    31. String argument = args[0];
    32. Player player2 = Bukkit.getPlayer( argument );
    33. if( player2 != null && player2.isOnline() )
    34. return true;
    35. }
    36. s.sendMessage(ChatColor.RED + "Coords: " + getLocation());
    37. }
    38. }
    39. }
    40.  
    41. }
    42. [syntax][/syntax]
     
  2. s.sendMessage(ChatColor.RED + "Coords: " + player2.getLocation().toString());

    Try it. If it wont work, try player2.getLocation() only.

    Also you can't return true before you are done, execution will stop at return true. Also, when using the && operator, you need to enclose each part where you explicitly does not check for a boolean, in parenthesis. So do this:

    1. if( (player2 != null) && player2.isOnline() ) {
    2. s.sendMessage(ChatColor.RED + "Coords: " + player2.getLocation());
    3. return true;
    4. }
    5. else
    6. {
    7. s.sendMessage(ChatColor.RED + "Cannot find player " + argument + ".");
    8. return true; //Return true despite of error, since we have already informed player of error
    9. }
     
  3. Offline

    Iron_Crystal

    I suggest using

    s.sendMessage(ChatColor.RED + "Coords: " + player2.getLocation().getBlock().getLocation());

    or something like that. (not on my coding computer so I don't know)

    What this does is return the block coordinates of the player. The benefit of this is because just returning the players location will return a long ugly double, whereas blocks have integer coordinates.
     
  4. Offline

    ZeusAllMighty11

    How do you register this in the plugin.yml?
    Iron_Crystal

    I have:


    Code:
      posget [args[0]:
        description: der7p
        usage: /<command>
        default: true
    
    Hm...I changed my plugin.yml to:

    Code:
      posget:
        description: der7p
        usage: /posget <user>
        default: true
    
    But it won't let me do /posget <playername>

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

    r0306

    It's not working because you are returning before the sendMessage() can be executed. Move it in front of the return statement like this:
    Code:
    if( player2 != null && player2.isOnline() ) {
    s.sendMessage(ChatColor.RED + "Coords: " + getLocation());
    return true;
    }
    
     
  6. Offline

    ZeusAllMighty11


    Nothing is executing now. No errors, no "invalid command" but nothing.

    I now have:
    Code:java
    1.  
    2. package obbystrength;
    3.  
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6.  
    7. import obbystrength.obbystrength;
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Location;
    11. import org.bukkit.Material;
    12. import org.bukkit.block.Block;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.entity.Entity;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.EventHandler;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.event.entity.EntityExplodeEvent;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class posget {
    24.  
    25. public boolean onCommand( CommandSender sender, Command cmd, String label,String[] args )
    26. {
    27. Player s = (Player)sender;
    28. if (cmd.getName().equalsIgnoreCase("posget"))
    29. {
    30. if (args.length == 1) // If the length is zero, there are no arguments...
    31. {
    32. String argument = args[0];
    33. Player player2 = Bukkit.getPlayer( argument );
    34. if( player2 != null && player2.isOnline() ) {
    35. s.sendMessage(ChatColor.RED + "Coords: " + player2.getLocation().getBlock().getLocation());
    36. return true;
    37. }
    38. }
    39. }
    40. return true;
    41. }
    42. }
    43.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  7. posget is not implementing CommandExecutor. As this is not the case I'll bet you didn't assign it to the command in onEnable().
     
  8. Offline

    r0306

    ZeusAllMighty11
    In your main class, did you create a new instance of your command executor in onEnable()?
    Code:
    private posget pg;
     
    pg = new posget(this);
    getCommand("posget").setExecutor(pg);
     
  9. r0306 That would tell him that pg isn't a CommandExecutor, so no, he didn't. ;)
    Also no need to save a reference to posget, so your code can be reduced to
    getCommand("posget").setExecutor(new posget(this));
     
    r0306 likes this.
  10. Offline

    ZeusAllMighty11

    Thank you! And thanks above too ^^
    testing if it works with others, then I will mark as solved571
     
Thread Status:
Not open for further replies.

Share This Page