Bukkit.getPlayer(UUID) or weak reference

Discussion in 'Spigot Plugin Development' started by Bernard2521, Aug 18, 2018.


Bukkit.getPlayer(UUID) or weak reference

  1. Bukkit.getPlayer(UUID)

    3 vote(s)
  2. Weak Reference

    1 vote(s)
  1. It does depend on the situation, it is just too generic for this to be answered ...
    Even more, using a WeakReference collection (Like the WeakReferenceMap) is not suitable in a hot method, it is much preferable listening to the PlayerQuitEvent and remove from the map while using an HashMap/null the reference.
    • Agree Agree x 1
  2. Do u mean WeakHashMap? But a WeakHashMap manipulations are much faster than event handling.

    when should i use weak reference, when should i use Bukkit.getPlayer(UUID)?
  3. Well, again, it does depend. (Yes, I mean WeakHashMap, :/)
    I generally using Weak Reference when I am not able to check if I need to clear the instance or not. For players or chunks (For example), you could just listen to the even and remove the hard reference.
    Plus, not that i am getting into this question, what do you even mean by "when should i use weak reference, when should i use Bukkit.getPlayer(UUID)?". I generally store the hard reference of the player and remove it when needed so I know when I need to finalize the object which contains the player instance.
    So, what I mean is, depends on your code style.

    PS: I am not 100% sure that event handling requires that much of performance hit, you are just calling a method ...
  4. For Players, you shouldn't need WeakReferences. Just handle them on quit.

    If you do need to persist outside of player sessions, use UUIDs.
    • Agree Agree x 1