1.15.2 When to use Player wrapper classes?

Discussion in 'Spigot Plugin Development' started by kreashenz, Feb 12, 2020.

  1. I have a large plugin I'm using that contains a PlayerWrapper class - you know, one of the static Map<Playername, PlayerWrapper> with creating and removing the wrapper for each player. I have this 'wrapper' class storing only a fairly small amount of data (a couple Integers, Floats, Strings and mostly Lists) to attach to each player.

    I was just wondering if there's a limit, or a threshold, where I should be using a PlayerWrapper class instead of just storing everything in Hashies and Lists.
    Just for example, if I have to store 4 different variables, would it just be easier to use multiple HashMaps to store this data? Or if I'm storing a lot more variables (20+) HashMaps, would it just be more efficient to use a PlayerWrapper class? Does this largely depend on the way I retrieve the data?

    I don't know if I'm explaining what I'm asking properly, but I thought I'd give it a go anyway. Thanks.

    [EDIT] I've just read in a previous thread (semi-related to this) that storing a PlayerWrapper in a static Map is not good practice, but rather to store them in a class that 'handles' the wrapper Map. Can someone explain why this is possibly the case?
     
    #1 kreashenz, Feb 12, 2020
    Last edited: Feb 12, 2020
  2. With one value mapped to a players UUID a map is ok.
    When you've two values that are mapped to a players UUID wrap it into an object with these N > 1 attributes. You are able to encapsulate logic better and you don't need to access different maps.
     
  3. MiniDigger

    Supporter

    just like what ysl said, this also helps you to encapsulate logic.
    you want to know exactly where stuff is added and removed to avoid memory leaks. having everything that manages those PlayerWrappers (what an awful name) in one class makes it very easy to handle.
    could also do it using generics and use the class as part of a lib you can reuse for different projects and stuff