Solved List in a Enum?

Discussion in 'Plugin Development' started by CommonSenze, Sep 6, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    CommonSenze

    Hello all,

    Now I've been trying to create teams in the form of Enums and have a team contain all the players inside their enum. Heres my code
    Code:java
    1.  
    2. enum Teams {
    3. BLUE(0, "&9&lBlue Team", new ArrayList<Player>()),
    4. RED(1, "&c&lRed Team", new ArrayList<Player>()),
    5. GREEN(2, "&a&lGreen Team", new ArrayList<Player>()),
    6. ORANGE(3, "&6&lOrange Team", new ArrayList<Player>());
    7.  
    8. private List<Player> list;
    9. private int id;
    10. private String name;
    11.  
    12. Teams(int id, String name, List<Player> list) {
    13. this.id = id;
    14. this.name= name;
    15. this.list = list;
    16. }
    17.  
    18. public String getName() {
    19. return name;
    20. }
    21.  
    22. public int getID() {
    23. return id;
    24. }
    25.  
    26. public List<Player> getPlayers(){
    27. return list;
    28. }
    29.  
    30. public static Teams getSmallestTeam() {
    31. Teams smallestteam = Teams.RED;
    32. for (Teams team : Teams.values()) {
    33. if (smallestteam.getPlayers().size() > team.getPlayers().size()) {
    34. smallestteam = team;
    35. }
    36. }
    37. return smallestteam;
    38. }
    39.  
    40. public static void addPlayer(Player p) {
    41. Teams team = getSmallestTeam();
    42. team.getPlayers().add(p);
    43. }
    44.  
    45. public void setPlayers(List<Player> list) {
    46. this.list = list;
    47. }
    48.  
    49. public void removeAllPlayers() {
    50. list.clear();
    51. }
    52. }
    53.  


    Now, I have that inside another class for convenience. My question is as follows; if I use this method of saving players in a enum, is this going to work and be effective? Or am I wasting my time and just save a HashMap of Teams with Players? Thanks for all the help in advance.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @CommonSenze Hashmap, enum is read only, and if it isn't: it should.
    And use UUID's instead of Player objects
     
  3. Offline

    CommonSenze

    @timtower Alright, thanks for the quick help.
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page