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 Player player = Bukkit.getPlayerExact(username); if(player == null) { player = Bukkit.getOfflinePlayer(username).getPlayer();}
You have to use "OfflinePlayer", using "getPlayer()" on the OfflinePlayer object will return an online version, null in this case.
In first line i fetch online player, and use OfflinePlayer when null. I need Player object there, online or not
Code:java Player player = Bukkit.getPlayerExact(username); if(player == null) {OfflinePlayer player = Bukkit.getOfflinePlayer(username);} CTRL Use this.
How about : Code:java Player player = (Player) Bukkit.getOfflinePlayer(username); if(player == null) { player = Bukkit.getOfflinePlayer(username);} Edit: I don't know what I'm typing. Sorry.
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.
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.
Code:java //let's say he has a username variable. String username = "xiggie"; Player player = Bukkit.getPlayerExact(username); //wouldn'tOfflinePlayer oplayer = Bukkit.getOfflinePlayer(player);//andOfflinePlayer oplayer = Bukkit.getOfflinePlayer(username); //be the same?
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?
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.
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?
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.
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.
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?
JPG2000 An OfflinePlayer just provides some basic information about the player that does not include health, hunger, location, etc..
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.
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.
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.
one4me We questioned him so many times because he did not respond to the question. It's a very vital question.