Solved Online and offline players

Discussion in 'Plugin Development' started by KingFaris11, Jun 1, 2015.

Thread Status:
Not open for further replies.
  1. Hi, this isn't a question about my plugin, but a question about the way Bukkit was programmed really... Anyway, so I was doing a null check on an argument of a Player like this:

    Code:
    public void addPlayer(Player player) {
        if (player != null && player.isOnline()) {
            // Do something
        }
    }
    
    Then I thought to myself, what's the point of doing "isOnline" when a Player always has to be online. So I looked at the SRC for the CraftPlayer class, and I noticed that it loops through all players on the server and checks if the name is equal to that of the checked player, if so, return true. My question is, why does it do that? Why not always return true, as Player extends OfflinePlayer, so OfflinePlayer can be true/false, but wouldn't a Player ALWAYS be online? When is there a case where the Player wouldn't be online?
     
  2. @KingFaris11 I guess that's true, the player would be null if they weren't online. OfflinePlayer it can be useful but not for Player.
     
    KingFaris11 likes this.
  3. Offline

    justin_393

    When you get a player by an argument, cast a Player to the String by Bukkit#getPlayer(); it will return null then, unless you use Bukkit#getOfflinePlayer();
     
  4. Yeah, it returns null, so what's the point of doing any code in CraftPlayer#isOnline() if the CraftPlayer MUST be online due to it not being null? That's my question. I'm pretty much trying to either say/ask that the isOnline() function in CraftPlayer (Player) is pointless, or if it isn't, why is it not?
     
  5. Offline

    justin_393

    Oh, I completely misunderstood you.. Yeah, I agree that isOnline() is pointless. I never gave much thought about it, just assumed that someone was tired of typing != null so made a method to check.
     
    KingFaris11 likes this.
  6. @KingFaris11 Two things

    1) isOnline() is an inherited method from OfflinePlayer - OfflinePlayers can be online or offline :)
    2) If a player leaves, and you still hold a reference to them (which you shouldn't be doing because memory leaks) then isOnline() will return false.
     
    KingFaris11 likes this.
  7. Yeah which is why I said it's useful in OfflinePlayer, but I didn't get why it didn't just return "true" in Player itself always, until I read your second point, thanks.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page