1.8.8 Adding requests from HashMap to player's gui

Discussion in 'Spigot Plugin Development' started by antSTAR, Jun 16, 2021.

  1. Strahan


    Yea, I misconstrued what you meant. As to the rest, do you mean if a player logs out, the UUID disappears from the map? I didn't know that. That's interesting.
  2. no, the UUID does not disappear from the map, but I don't think it's a problem because an object reference only takes up 4 bytes AFAIK^^

    And once a player logs in again, they are already/still in the map. That's why I prefer this method. Even with a million of players, the UUID references only take up 4MB of space.

    But of course, it's fine too to store player references, I just think it's more work to handle all the login/logout stuff so I always use UUIDs instead. But of course, both ways are fine. It's just personal preference.

  3. Size doesn't actually matter. As long as you know how to manage the memory and how u use it, at this point Player reference is more efficient way as a key and value for player requests. Since you don't want offline player to stay inside the hashmap, because it will be useless.
  4. The reason to use UUIDs over Player objects isn't optimization. Player instances get removed from the memory as soon as a player leaves. If you are keeping track over a longer period you'd need to do a lot of extra work saving it where as a UUID instance won't be removed so you can keep track of it while the player can get offline and online again.
    • Agree Agree x 1
  5. Exactly
  6. The only downside is for the long period of time the UUID or the data will stay even if the player is offline. So you can't track which UUID is offline and must be removed from the map.
  7. What is the cleanest way to get a player from an UUID?
    (UUID to Player object)
  8. Bukkit#getPlayer(UUID)
    • Agree Agree x 1
  9. That's why you should listen to an event like PlayerQuitEvent and remove the player or their UUID from the collection at that point.
    If you're keeping it in-memory a little longer to save it to a database later or anything of that sort, then you should use a separate list of "dirty" data that is cleared after the database is updated.
  10. Does that work if a player is offline?
    As @Lifeonblack said.
    In other words, can I do this?
    Code (Java):
    List<UUID> list
    for(UUID id : list){
        Player ply = Bukkit.getPlayer(id);
        if(ply != null){
            p.sendMessage("That player is online.");
            p.sendMessage("That player is offline!");
    • Like Like x 1
  11. I usually use Bukkit#getOfflinePlayer(UUID) and then OfflinePlayer#isOnline()
    I'm not sure if you can just check if the result of Bukkit#getPlayer(UUID) is null; as the documentation puts it a Player object doesn't necessarily have to be "connected". However, Player extends OfflinePlayer so you can always cast if they are online.
    • Friendly Friendly x 1
  12. #getOfflinePlayer is slower because it has to read the cache of all players that have joined the server before, and if the player isn't there, it goes on to run a blocking web request to the Mojang API to create one from.
    If you just want to check if a player is online or not, then @iFeliN3x0_'s example is much more efficient.

    Edit:// also yes, Bukkit#getPlayer returns null if the player is not online at that time. The only time that a Player object may represent a player that is not connected is when a reference of one is kept until after they've logged out. Needless to say, it wouldn't be much use once the player has logged out.
    #32 Escad, Jun 21, 2021
    Last edited: Jun 21, 2021
    • Like Like x 1
    • Agree Agree x 1
    • Informative Informative x 1