Solved getPlayer returning null

Discussion in 'Plugin Development' started by Konkz, May 28, 2014.

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

    Konkz

    Before anyone tells me I need to use UUID I will straight off say I don't need to, I may adapt it to use UUID's but it's not needed as all data is stored for 10 min max.

    Anyway, I have a method which adds a player to a spectator, it calls another method "onSpectatorAdd" which then handles all the stuff.

    The AddSpectator takes a ArenaPlayer which is my player, I get their name and broadcast it to server like so

    PHP:
    Bukkit.broadcastMessage("ArenaPlayers name: " arenaPlayer.getUsername());
    it will return "ArenaPlayers name: konkz" which is correct. But when I try to make the Arena Player into bukkit player by doing this
    PHP:
    Player bukkitPlayer Bukkit.getPlayer(arenaPlayer.getUsername());
    it will return null. So if I do
    PHP:
    Bukkit.broadcastMessage("Bukkit player: " bukkitPlayer.getName());
    it will throw a NPE here.

    Any ideas why this method is returning null when I provide the right username?
     
  2. Offline

    THEREDBARON24

    bukkitPlayer = Bukkit.getPlayer(arenaPlayer.getName()) I am sure its deprecated, however, just suppress warnings. Also, I believe that the method is still valid, it is just no longer recommended. It's the same for player.getTargetBlock and getting block data, the only way to do it is to use deprecated methods....not sure what to say about that.
     
  3. Offline

    Konkz

    getPlayer is only deprecated to raise the awarness of developers to start using UUID, but I could not care less about it as I said I don't feel like converting most of this into UUID if it will work as fine with players name.

    Yes, it still works in all other methods therefore it must be valid. This is the only time I met myself not being able to find a fix for this alongside of two other developers. If nobody will find what the issue may be than I will just sacrificesome performance and do it the longer way around with looping and such.

    Also, I forgot to mention in the thread, the error will always point to the line that I start doing bukkitPlayer.___ - therefore stack trace is not required as it contains nothing useful, neither is code as for some reason the player becomes null when it's passed through getPlayer method.
     
  4. Offline

    Aengo

    Konkz Why don't you make your own methods for getting players? Here are mine
    Code:java
    1. public static Player getPlayer(UUID id)
    2. {
    3. for(Player player : Bukkit.getOnlinePlayers())
    4. {
    5. if(player.getUniqueId().equals(id))
    6. {
    7. return player;
    8. }
    9. }
    10. return null;
    11. }
    12.  
    13. public static Player getPlayer(String string)
    14. {
    15. for(Player player : Bukkit.getOnlinePlayers())
    16. {
    17. if(player.getName().toLowerCase().contains(string.toLowerCase()))
    18. {
    19. return player;
    20. }
    21. }
    22. return null;
    23. }
    24.  
    25. public static Player getPlayerExact(String string)
    26. {
    27. for(Player player : Bukkit.getOnlinePlayers())
    28. {
    29. if(player.getName().equalsIgnoreCase(string))
    30. {
    31. return player;
    32. }
    33. }
    34. return null;
    35. }


    Konkz Also are you sure that your ArenaPlayer's name isn't null?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  5. Offline

    Konkz

    It can't be as I print the arenaPlayer.getUsername(); and it prints "konkz" therefore it's not null.

    That was my "long way around", Aengo. But I see no point in it if there is something that works already in Bukkit's API as it works in all other cases but this one.
     
  6. Offline

    Aengo

    Konkz Oh, was just checking. Well I like non deprecated methods so I prefer to use my own;)
     
  7. Offline

    Konkz

    As I am aware that getPlayer will not be removed and it's only deprecated method I use I just disabled deprecations from giving me warnings but just them being ignored therefore I don't have to add any suppress warnings and such.
     
  8. Offline

    Unknowncmbk

    For some reason it seems like the server isn't registering you as being online. Look into that ;)
     
  9. Offline

    RawCode

    debug messages anyone?
     
  10. Offline

    Konkz

    Have been hundreds of debug messages.

    I seem to of located the issue, whenever I do this debug message
    PHP:
    Bukkit.broadcastMessage("Is konkz on: " bukkitPlayer.getName().equalsIgnoreCase("konkz"));
    and I'm hidden (Using p.hide(...)) than it will return false. But if I'm not it will return true. Perhaps a bug
    in Bukkit? Weird how looping through online players does not find it.
     
  11. Offline

    RawCode

    debug messages looks like System.out.println() not Bukkit.broadcast().
     
  12. Offline

    Konkz

    I've been doing both, as I really hate the Multicraft console. Plus, they both
    can achieve the same result with what I'm doing.
     
  13. Offline

    Necrodoom

    Does getName prints "konkz"? If yes, then that would always return true.
     
  14. Aengo If you avoid deprecated methods by recreating a less reliable method for it, you kind of miss the point of deprecation.
     
  15. Offline

    Konkz

    I was looping through all the players and getting each of the names then checked if it equals "konkz".
    Whenever I printed "getName" it would print everyones but mine.
     
  16. Offline

    Necrodoom

    since other vanish plugins can detect vanished players fine, id take a guess and say you are not online or the problem is elsewhere in your code.
     
  17. Offline

    Aengo

    The point of this deprecation is to create awareness of the UUID switch though. For short term, eg a few minutes a player name is fine to be used.
     
  18. Offline

    fireblast709

    Konkz Can you show the code of ArenaPlayer (and if it at another location, where you set the value returned by getUsername())?
     
  19. Offline

    zackpollard

    Using a player name is fine as long as you are not going to be storing the data between player logins. "A few minutes" is not taking this into account, the condition I mentioned above should govern whether or not you use UUIDs in your code.
    Your methods are exactly the same as how Bukkit does them, you are just creating more unnecessary code.
    Firstly, you should include a method in your ArenaPlayer class called getBukkitPlayer(). This will mean you can simply grab the ArenaPlayers bukkit player object from there. If ArenaPlayer's will be cleared when a player logs off, it is safe for you to store the Player object in the ArenaPlayer class in order to gain access to it later, as it will only be referenced. If you are planning to store them after players log off, then you should be both using UUIDs, and should use these UUIDs to match the player using getPlayer(UUID) in the getBukkitPlayer() method I mentioned above.
     
  20. Offline

    Konkz

    I found the issue, adding a simple 10 tick delay after respawn until anything gets executed fixes it.
    After player respawns if I print to get length of players on it will return 1 if 2 players are on but if I give it
    10 tick delay I get 2.
     
  21. Offline

    rsod

    Konkz so you want to say that a dead but not respawned player doesn't counts as online?This should be a bukkit bug then.
     
  22. Offline

    fireblast709

    rsod According to the source (read as: my interpretation of the source), the player is removed from the online player list, and readded after the PlayerRespawnEvent has been fired. Note that (according to the comments) this is actually vanilla behaviour.
     
Thread Status:
Not open for further replies.

Share This Page