Casting an OfflinePlayer to a Player

Discussion in 'Plugin Development' started by SourceForums, Feb 21, 2014.

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

    SourceForums

    Hey there, the title really says it all for this. I've done a bit of research myself but couldn't find the right one. Most people just used null checks or hasPlayedBefore check to an OfflinePlayer but that's not really what I'm looking for. I need a way to directly cast an OfflinePlayer to a Player. I've tried something like:
    Code:java
    1. Player targetPlayer = (Player) Bukkit.getServer().getOfflinePlayer(args[0]);

    but as expected, it just returned a bunch of errors. Tips?
     
  2. Offline

    xTigerRebornx

    SourceForums OfflinePlayer has a getPlayer() method that returns a Player object, but you'd still need to actually check if the player is online depending on the situtation.
    Edit: Method will return null if the Player isn't online
     
  3. Offline

    SourceForums

    xTigerRebornx
    Thanks, that problem is now solved but now I cannot use the targetPlayer variable?
     
  4. Offline

    Garris0n

    Cannot use? As in what...
     
  5. Offline

    SourceForums

  6. Offline

    Garris0n

    If it returns errors post the errors.
     
  7. Offline

    SourceForums

    Garris0n
    My bad, I forgot. Here's the error:
    Code:
    [12:06:54] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'bounty' in plugin SourceBounty v1.0.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at net.sourceforums.sourcebounty.commands.CommandBounty.onCommand(CommandBounty.java:74) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 13 more
    Line 74 is the following:
    Code:java
    1. File file = new File("plugins" + File.separator + "SourceBounty" + File.separator + "userdata" + File.separator + targetPlayer.getName() + ".yml");

    Refer to the first strip of code on this thread to see what targetPlayer is defined as. Please reply ASAP.
     
  8. Offline

    Garris0n

    Why don't you just use getPlayerExact instead of getOfflinePlayer.getPlayer? And it's null, hence the NullPointerException.
     
  9. Offline

    SourceForums

    Garris0n
    Why is it a null?
    EDIT: Also, there is no getPlayerExact method for this. :(
    EDIT: Do we just place in a hasPlayedBefore & null check to make sure that it's not null?
    EDIT: Nope, that just causes this line to cause error:
    Code:java
    1. if(targetPlayer.hasPlayedBefore() && targetPlayer != null){
     
  10. Offline

    Garris0n

    Why is there no getPlayerExact method? There's a getPlayerExact method... use server.getPlayerExact("name") to get the player. The offline player is pointless.
     
  11. Offline

    SourceForums

    Garris0n
    Did that, still the same error as I've mentioned in the third edit of my reply before this. :(
     
  12. Offline

    Garris0n

    Then the player is offline.
     
  13. Offline

    SourceForums

    Garris0n
    I need the code to execute even if the player if offline by altering their userdata (playerName.yml). How will I go about doing this?
     
  14. Offline

    Garris0n

    Well you already had the OfflinePlayer. That's for...well, you know...offline players.
     
  15. Offline

    SourceForums

    Garris0n
    Well, that caused some errors. :( Any ways to fix this?
     
  16. Offline

    Garris0n

    If you only need their name, just use the string you already have.
     
  17. Offline

    SourceForums

    Garris0n
    I need to do other things such as sending messages too though. :(
     
  18. Offline

    xTigerRebornx

    SourceForums You can't send a message to an player if they aren't on the server, can you?
     
  19. Offline

    SourceForums

  20. Offline

    xTigerRebornx

    SourceForums Well, what else do you want to do that requires more then just the player's name.
     
  21. Offline

    SourceForums

    xTigerRebornx
    I have my own separate method that requires a Player variable. :(
     
  22. Offline

    Stoux

    SourceForums Then you'll have to change the method so it works with an OfflinePlayer? What's the method/what does it do?
     
  23. Offline

    mazentheamazin

    SourceForums
    Check if the player is equal to null, if not, then run the code and send the messages. If it is null, then duplicate the method you made and change the String variable. (Assuming you're just wishing to edit the config file and send them messages)
     
  24. Offline

    SourceForums

    Stoux
    That won't work in my case unfortunately.
    mazentheamazin
    That's the whole problem here I believe. :(
     
  25. Offline

    CubieX

    Use the OfflinePlayer and check if he ".hasPlayedBefore()". (the OfflinePlayer, NOT the Player!)
    So you know this players name is a valid and known one. Means: He has user data.
    Then you can do your .yml manipulations with this players name.
    No need to use a Player object here. You only need the name.

    By the way: What is this "user data" you are talking about? Where does it come from?
    And what do you actually want to do with this player?

    This
    is not really helpful
     
  26. Offline

    SourceForums

    CubieX
    Sorry for not being specific enough. The userdata is simply a YAMLcreated for each player. Also, I'll post the 'method' as soon as I have access to my PC.
    EDIT: Nevermind guys, I somehow managed to make the method compatible with OfflinePlayers. Nonetheless, I've learnt a lot, thanks again! :D
     
  27. Offline

    SourceForums

    mazentheamazin
    CubieX
    Stoux
    xTigerRebornx
    Garris0n
    Nevermind guys, well, when I tested the code, I get a NullPointerError:
    Code:
    [19:29:14] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'bounty' in plugin SourceBounty v1.0.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at net.sourceforums.sourcebounty.commands.CommandBounty.onCommand(CommandBounty.java:84) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 13 more
    on the line:
    Code:java
    1. if(targetPlayer.hasPlayedBefore() && targetPlayer != null){

    Did I do something wrong here? Please reply ASAP. Thanks! :D
    P.S. Sorry for double posting, but since this is a new alert (?), I had to let the guys know by tagging them.
     
  28. Offline

    Stoux

    SourceForums
    Code:java
    1. if (targetPlayer != null && targetPlayer.hasPlayedBefore()) {


    You need to do the null check before trying to do functions on it (since it will NullPointer if it is null).
     
  29. Offline

    Jalau

    also you could just use .isOnline()?
     
  30. Offline

    SourceForums

    Stoux
    Thanks, I'll try that. ;)
    Jalau
    That's breaking the whole purpose of this. :(
     
Thread Status:
Not open for further replies.

Share This Page