Get Offline Player

Discussion in 'Plugin Development' started by CTRL, Oct 23, 2013.

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

    CTRL

    Hi

    I need to have offline player, but instead of Player object i get null pointer exception.
    It works when player is online, username var is working well.

    Code:java
    1.  
    2. Player player = Bukkit.getPlayerExact(username);
    3.  
    4. if(player == null) {
    5. player = Bukkit.getOfflinePlayer(username).getPlayer();
    6. }
    7.  
     
  2. Offline

    1Rogue

    You have to use "OfflinePlayer", using "getPlayer()" on the OfflinePlayer object will return an online version, null in this case.
     
  3. Offline

    CTRL

    How to convert OfflinePlayer to Player? All my functions in plugin uses Player class.
     
  4. Offline

    The_Doctor_123

    CTRL
    Umm.. why would you use online methods for an offline player? There's no purpose in that.
     
  5. Offline

    CTRL

    In first line i fetch online player, and use OfflinePlayer when null. I need Player object there, online or not
     
  6. Offline

    1Rogue


    for what reason?
     
  7. Offline

    ArthurMaker

    Code:java
    1. Player player = Bukkit.getPlayerExact(username);
    2.  
    3. if(player == null) {
    4. OfflinePlayer player = Bukkit.getOfflinePlayer(username);
    5. }

    CTRL
    Use this.
     
  8. Offline

    xigsag

    How about :

    Code:java
    1.  
    2. Player player = (Player) Bukkit.getOfflinePlayer(username);
    3.  
    4. if(player == null) {
    5. player = Bukkit.getOfflinePlayer(username);
    6. }


    Edit: I don't know what I'm typing. Sorry.
     
  9. Offline

    CTRL

    xigsag, it wont work, cannot convert OfflinePlayer to Player, i tried.

    I store players data in HashMap with objects of class Member, that class has var using class Player, thats why i cant use OfflinePlayer.
     
  10. Offline

    The_Doctor_123

    CTRL
    But what are you doing that requires the online player?
     
  11. Offline

    xigsag

    CTRL Normally, you would use 'player' for command senders and event triggering player, right? I don't see how an offline player would be able to do either one.

    You'd have to tell us what you want to achieve, so that we could probably give you a better idea of how to do it, since this probably isn't a good idea, at all.
     
  12. Offline

    Goblom

    Bukkit.getOfflinePlayer(player);
     
  13. Offline

    xigsag


    Code:java
    1. //let's say he has a username variable.
    2.  
    3. String username = "xiggie";
    4.  
    5. Player player = Bukkit.getPlayerExact(username);
    6.  
    7. //wouldn't
    8. OfflinePlayer oplayer = Bukkit.getOfflinePlayer(player);
    9. //and
    10. OfflinePlayer oplayer = Bukkit.getOfflinePlayer(username);
    11.  
    12. //be the same?
     
    Goblom likes this.
  14. Offline

    1Rogue

    Can we seriously just answer this simple question already:

    Why is this necessary. What API could you possibly need from an online player instance that you could use for an offline player?
     
  15. Offline

    xigsag

    1Rogue Maybe he can't access the forums right now. Maybe he's out. Let's give him some time.
     
  16. Offline

    lzravanger

    If you need to give the offline player an item, just add them to a list and then give them the items they earned when they log in.
     
  17. Offline

    CTRL

    I need that object to have access to it all time, for example to get its gamemode, displaynick, everything. Why can't it be Player not OfflinePlayer? What is the difference? Maybe i could make new Player object and set there vars from OfflinePlayer?
     
  18. Offline

    xigsag

    CTRL The difference is that one is online and active in the server, and another isn't. You can't modify an offline player's gamemode. That's just how it is. The server can't detect a player who isn't there.
     
  19. Offline

    _Filip

    But, you can get the offline player's nbt data, from his player.dat, and change it there.
     
  20. Offline

    Garris0n

    But there's really no point in doing that, just change it when they log in.
     
  21. Offline

    The_Doctor_123

    CTRL
    Impossible to do without data file editing. You're going to need to make your own methods to change these things. Actually, I suggest you build up a queue for the changes you "made" and apply them on login. Also, you won't be able to store online players, so you're going to have to change your ways or face memory leaks/errors.
     
  22. Offline

    JPG2000

    1Rogue It looks like the right thread to ask this.


    Im not sure what OfflinePlayer does. I know its another player object though. What are the benifits? Why would someone use it over a normal Player object?
     
  23. Offline

    The_Doctor_123

    JPG2000
    An OfflinePlayer just provides some basic information about the player that does not include health, hunger, location, etc..
     
    1Rogue likes this.
  24. Offline

    Sagacious_Zed Bukkit Docs

    JPG2000 An OfflinePlayer represents a player that is not currently logged in to the server. You can see the methods for OfflinePlayer in the JavaDocs. Offline player is not a Player, but all Players are OfflinePlayer. You can use OfflinePlayer when you only need to invoke the methods on OfflinePlayer.

    CTRL
    You should be using the player name as the key, not the Player object.
     
    1Rogue and The_Doctor_123 like this.
  25. Offline

    one4me

    You can spawn an Entity and load the player's file to it and use it just like you would with any other Player. Or you could just edit the player's NBT data.
     
  26. Offline

    The_Doctor_123

    one4me
    That would be kinda stupid.. You'd have a whole load of players "online" that way.
     
  27. Offline

    one4me

    No, it'd be stupid for there to be multiple posts questioning the OP about why he wants to something, or telling him what he wants to do can't be done (when it very well can be.)

    Anyways, the post remains. If he wants to use an offline player in a command that needs the player to be online, it'd be easiest to just load the player's file to an Entity and call the command and then unload the Player. If he just wants information about the player it'd be easiest to read the player's NBT data. If he wants to get the information efficiently he should make some type of database to store information about all players, however that may be overkill for what he wants.
     
  28. Offline

    The_Doctor_123

    one4me
    We questioned him so many times because he did not respond to the question. It's a very vital question.
     
  29. Offline

    CTRL

    Ill try fix that without using Player object...
     
Thread Status:
Not open for further replies.

Share This Page