Solved Get Offline/Online Player as Player?

Discussion in 'Plugin Development' started by Bubelbub, Jan 15, 2013.

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

    Bubelbub

    Hello,

    how i could get a player by name?

    I used:
    Code:
                        Player pTo = this.plugin.getServer().getPlayer(pname);
     
                        if (pTo == null) {
                            return true;
                        }
    But with these Method i get only online Players...
    If i use "getOfflinePlayer" instead "getPlayer" i get the "OfflinePlayer" and i cant use pTo, because pTo is a Player and no OfflinePlayer....

    Thank you in advance.
     
  2. Offline

    gomeow

    You need to do:

    OfflinePlayer offP = Bukkit.getOfflinePlayer(String name);

    what is this for?
     
  3. Offline

    Bubelbub

    for "OfflinePlayer".
    But no online Player...

    How i could convert the OfflinePlayer into online Player?
    Class OfflinePlayer => Player.
     
  4. Offline

    gomeow

    You can't, because they aren't online

    You may be thinking of this:
    Code:java
    1. Player p = Bukkit.getPlayer(String name);
    2. if(p != null) {
    3. //Online
    4. }
    5. else {
    6. //Offline
    7. }
     
  5. Offline

    caxco93

    get offline player, then check if he is online

    Code:
    if(Bukkit.getOfflinePlayer(name).isOnline())
        //here he is online
    else
        //here he is offline
    you can also typecast an OfflinePlayer to a Player

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

    Bubelbub

    I need something like this...

    I would get a player wherever offline or online.
    And then i would make something with the player...

    If i only know if the player is offline or online i cant make something...

    examples.png
     
  7. Offline

    gomeow

    What are you trying to do?
     
  8. Offline

    Bubelbub

    In the example i will kick a player. (yes offline OR online player...)

    But one variable should be an OfflinePlayer AND a Online Player...

    Or is a online player a offlineplayer, too??
     
  9. Offline

    fireblast709

    Player extends OfflinePlayer. So you can cast Player to OfflinePlayer, but not a OfflinePlayer to Player
     
  10. Offline

    Panjab

    You can't kick somebody who isn't online Oo
     
  11. Offline

    Bubelbub

    Thank you!
    You mean this?
    Code:
                        OfflinePlayer pTo = this.plugin.getServer().getOfflinePlayer(args[0]);
    
                        if (pTo == null) {
                            pTo = this.plugin.getServer().getPlayer(args[0]);
                        }
    
                        if(pTo == null) {
                            // player dont exists
                        }
    I know...
    This was a example :D
     
  12. Offline

    fireblast709

    getOfflinePlayer(String) will always return an OfflinePlayer, if the player exists or not.
     
  13. Offline

    Bubelbub

    And this will work?

    Code:
    if(this.plugin.getServer().getOfflinePlayer(args[0]).getPlayer().hasPlayedBefore())
    {
             // Offline/Online - Exists
    }
    else
    {
            // Doesnt exists
    }
     
  14. Offline

    Craftiii4

    Use != null, as .hasPlayerBefore() might cause a null exception if they don't exist. Not sure though.
     
  15. Offline

    fireblast709

    Craftiii4 to quote myself here.
     
  16. Offline

    Bubelbub

    Final, tested.. (moderator could close this topic)

    Code:
    OfflinePlayer pTo = this.getServer().getPlayer(String playername);
    
    if (pTo == null) {
        pTo = this.getServer().getOfflinePlayer(String playername);
    }
    
    if (pTo == null || !pTo.hasPlayedBefore()) {
        // Player doesnt exists
    } else {
        if (pTo.isOnline()) {
            // player exists (online)
        } else {
            // player exists (offline)
        }
    }
     
    jojohnson1 likes this.
  17. Offline

    chasechocolate

    No need for a moderator to close this topic. If someone else has the same question as you, then they could locate themselves to this thread. Just add the [Solved] prefix to the thread title (under "Edit Thread").
     
    Bubelbub likes this.
Thread Status:
Not open for further replies.

Share This Page