Convert String (PlayerName) to Player?

Discussion in 'Plugin Development' started by KaiBB, Feb 15, 2012.

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

    thehutch

    KaiBB
    Are you still looking for help on this topic? I think the whole NPE problem has been sorted :D
     
    PandazNWafflez likes this.
  2. Offline

    KaiBB

    I think it's worked out :)
     
    thehutch likes this.
  3. Offline

    bsquidwrd

    This may be considered bumping, but I found this thread very useful for when I was trying to do the exact same thing. Thank you OP for asking the question.

    I do have one question about this though, how can I do this for an OfflinePlayer? I want to be able to turn a String into a Player with/without them having to be online. Is there one single method to do this by chance? I have tried Bukkit.getOfflinePlayer(String); but that returns OfflinePlayer, and I need Player.

    Any and all help is appreciated. Thank you in advance!
     
  4. Offline

    Njol

    You can use offlinePlayer.getPlayer() or Bukkit.getPlayer(string). Both will return null if the player is not online.
     
  5. Offline

    boduzapho

    Actually it is => OfflinePlayer player = Bukkit.getOfflinePlayer("Player Name");
    "
    Gets the player by the given name, regardless if they are offline or online.
    "

    Use it with -> if(player.IsOnline()); if you want to do something to the player that requires them to be online at the time.

    I use it to return player Names to ensure ArrayLists use the proper name case and spelling,

    Code:
    OfflinePlayer pp = Bukkit.getOfflinePlayer(args[0]);
    buildfriend.addbuildfriend(player.getDisplayName(), pp.getName());
    When you do something like this CASE matters:

    Code:
    if (Data.tobuildfriends.containsKey(player.getDisplayName()))
    if the name was not added with the proper case it will fail the check.
     
  6. Offline

    Bubelbub

    Hmm, sorry for "Push".

    But how i acutally could find a offline/online player?
    I'm not interested to know if the player is online or offline.

    Code:
                        Player pTo = Bukkit.getPlayer(args[0]);
    
                        if (pTo == null) {
                            String str = this.plugin.translationConfig.getConfig().getString("user.never.played").replace("{player}", args[0]);
                            p.sendMessage(str);
                            return true;
                        }
    Said that the player doesnt exist.
    But the player exists and is offline...

    Any usefull code?
     
Thread Status:
Not open for further replies.

Share This Page