Solved getPlayer from args?

Discussion in 'Plugin Development' started by stamline, May 9, 2014.

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

    stamline

    Hi, how can I get a Player from a Command Argument?
    Just to make it simple, we think the Player name are argument 1.

    I have tried this:
    Code:java
    1. Player target = (Player) args[1];

    Code:java
    1. Bukkit.getOfflinePlayer(args[1]);

    The first get red underline, and the second one get yellow underline......
     
  2. Offline

    koencraft2002

    Dear Stamline,

    This should work

    Code:java
    1. //Check if the player has sent one argument
    2. if(args.length == 1)
    3. //Check if the player name the player has sent is online / not equal to null
    4. if(Bukkit.getPlayer(args[0]) != null)
    5. //Get the player
    6. Player player = Bukkit.getPlayer(args[0]);


    This works, but the second code you posted should work fine too, what does it say about it?
     
  3. Offline

    stamline

    I think it work now, with the second code

    Nope..... the player has to be online..... How can I make so I can make it run when the player is online or not. So it doesn´t matter....

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    teej107

    stamline Careful using the getPlayer(String name) method. It's becoming deprecated since Minecraft is going to a UUID based system to identity players. This would work as well:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("online"))
    2. {
    3. for(Player player : this.getServer().getOnlinePlayers())
    4. {
    5. if(player.getName().equals(args[0]))
    6. {
    7.  
    8. return true;
    9. }
    10. }
    11. }
     
  5. Offline

    Konkz

    PHP:
    Player target Bukkit.getPlayer(args[1]);
     
    if (
    target != null) {
    p.sendMessage(target " is online.");
    }else{
    p.sendMessage(target "is offline,");
    }
     
  6. Offline

    tryy3

    Konkz
    Wouldn't null mean that the player might not exists too and not just if his online or not?
     
  7. Offline

    Konkz

    Do you mean if the play is not existent? As if I typed "ifwehiofhewi"? Well, if you want to do that
    than you got to do some stuff on your own side; if you want to check through players
    that were on your server and if they are not on that list don't do it then it's up to you to set up
    a config/database to store that.

    In the instance that I presented null would mean that player is not on server, most plugins such as
    PermissionEx do that - if you add player "hiimnotreal" to the usergroup Admin it will add them, they
    may never go on server and such but they will be there.
     
  8. Offline

    tryy3

    Konkz I just got the impression that the OP wanted to handle both online and offline players, (hence why he used getOfflinePlayer() in 1 of his code's
     
  9. Offline

    Konkz

    Hence I used }else{

    If you'd do

    getConfig().set("MyGame.players." + target.getName()"); and the target was konkz - even know I was offline it would save my name.
     
Thread Status:
Not open for further replies.

Share This Page