Map does contains a player but debug says it doesn't...

Discussion in 'Spigot Plugin Development' started by Proudyy, Aug 13, 2018.

  1. Hey!
    I'm currently trying to code balloons and wrote a Balloon Class with a constructor and the listeners so far...
    Balloon Class: https://pastebin.com/KjYKHHsL
    Listener Class: https://pastebin.com/Hx9SiPmX
    InvClickEventListener: https://pastebin.com/L0mKFeC9
    Everytime if i spawn the balloon and add it to the map (includes the spawn method) i try to ask if it has the player in it..
    But then it says it doesnt..
    Here the line for the Map:
    public Map<Player, Balloon> balloons = new HashMap<>();
     
  2. Never store players in an map, store uuids
     
  3. The map is the only thing that you refer to the object li (LobbyItems) for, it seems, which you create a new instance of each time you also make a new instance of your Balloon. A brand new instance of LobbyItems with an empty map won't have a player in it.
     
  4. I understand what you mean but how do i have to change the code xd
    An example pls
     
  5. That doesn't matter...
    In this case @
    Escad tells the truth..
     
  6. You'd have to pass an instance of the LobbyItems class to the Balloon class when the constructor is used. You can get it through fields/methods in another class it already has, or you can directly pass it in the constructor.
    Also, while what Naturegg said is somewhat true (at least, as far as storing Players goes - just make sure you remove them, but UUIDs are better), I think a bit more likely to be a problem would be using a Player object in your Balloon class. You want to change that to UUID, since the player object isn't constant.
     
  7. I just fked on this and did it to static:
    Code (Text):
    public static Map<Player, Balloon> balloons = new HashMap<>()
    And it works!
    But the velocity of the hidden bat which i used to put the fallingblock on doesn't gets set the velocity...
    That's so frustrating!!
     
  8. Unless you know what you're doing when you use static, I'd advise against using it and finding a better solution.
     
  9. Nope not rly.
    I don't know much about Java. Thats the problem, hh. xd
    I also don't know why the Velocity doesn't gets set...
    Can you guys help me, please? :O
     
  10. Yes it does matter, storing the whole player object uses a lot more memory than storing just an uuid.
     
  11. av0

    av0

    Are you aware that Java stores references to objects in its collections and not the objects themselves? The reference is to a Player that's already in memory, so please refrain from misleading people.
     
  12. There is no point in storing a player object if he is not using anything in it.
     
  13. av0

    av0

    It seems you misunderstand how a Map functions; a distinct key is mapped to some value. You're also not *storing* anything, as the Player object is already loaded into memory (i.e. the Collection<? extends Player>). A Player can be used as a key in an IdentityHashMap, which is more efficient than a HashMap (assuming you're not concerned with the Player changing sessions). Again, please stop misleading others.
     
  14. Only the hash of the player is used as a key. So no duplication of player data in memory is done.
    Most time the memory leak happens because the value is not removed, when it should be.
     
    • Agree Agree x 1
  15. av0

    av0

    That's incorrect; In a HashMap, the Player's hashCode is used to determine the bucket that the reference is stored in. Multiple objects can be stored in a bucket. The bucket is then iterated over, calling Object#equals to determine if the Player exists as a key. If it does, then the value that the Player maps to is returned; otherwise, null is returned. And memory leaks have nothing to do with this.
     
  16. That is what I said with no duplication between of the player object. Your right.

    We only have the problem that the player instances are not garbage collected because they are referenced.
    -> so on Player quit -> do a map.remove(player);
     
    #16 ysl3000, Aug 14, 2018
    Last edited: Aug 14, 2018
  17. av0

    av0

    Exactly; this is why an IdentityHashMap is superior in this case.