Storing parties

Discussion in 'Spigot Plugin Development' started by Stevie212, Apr 25, 2017.

  1. What's the best way to store players party. When a play does /party create <Name>, it creates the party but I would need to store these parties to someone that I know when it can be accessed at anytime. So can anyone tell me how I should do that? Thanks
     
  2. If you want to can access it any time, you could use a file.
     
    • Agree Agree x 1
  3. I would store it in a HashMap with the creator of the party as key and an ArrayList with players as value.
     
  4. But if he stored it in a HashMap, the Data would get lost if the server is restarted/reloaded.
     
  5. If he's using it to negate party damage (friendly fire) and there's 30 people in 7 parties that's a lot of reading and writing to the disk. It should be stored in memory and written to the file on shutdown.


    Sent from my iPhone using Tapatalk
     
  6. If you dont want to do what the others have suggested. You could always use whats already in the API. Im talking about the team interface. Hopefully this helps.
     
  7. I think if I store it into a HashMap it will be better. So I was thinking of storing all the parties to an arraylist. And the players in a HashMap

    If I store the creator as the key how would I get the the party name :/

    I was thinking of storing the leader in the leader HashMap with the key as the party name and the value as the party creator name. The players in that party will be stored in an a HashMap called players. The key will be the party name and the value will be the players.

    I really do not know what to do. Storing it into a file will be easy and will save alot of time but I want this to be perfect and no errors. I need suggestions guys. .
     
  8. Does this API save the teams on reload?

    Edit sorry for the double post.. Hmm this API deals with scoreboards I don't need any scoreboards in this case
     
  9. Do you want to store the data in a database so you can reload it after a restart or not? If you only want to temp store the data I suggest storing it in a hashmap with the party name as key. If not the choise is up to you, you could store it in a .yml file, mysql or any other database.
     
  10. Store it as a hashmap or whatever and then write it to a file when the server shuts down and load it again when the server starts.
     
  11. Yes the team api saves the team. And no, it does not rewuire you to use a scoreboard. It is optional to use one.
     
  12. You could create an object called Party that has the name of the party and its members and store that in a HashMap with the UUID of the leader of the party like so:
    Code (Text):
    HashMap<UUID, Party> parties = new HashMap<UUID, Party>();
     
    • Winner Winner x 1
  13. If you want it to be accessed at all times, save it to a configuration file using either a party's name or the leader's UUID (Which would be partyLeader.getUniqueID().toString(), "partyLeader" being the player who created the party.).
     
  14. Even better than flat-file: Use MySQL or possibly even Redis to accomplish this.
     
  15. make an actual party object.

    Code (Java):

    public class Party {

         private UUID leader;
         private Set<UUID> members;

         public Party(UUID leader, Set<UUID> members) { //when creating party from file/external data storage
                this.leader = leader;
                this.members = members;
         }

         public Party(UUID leader) { //when creating party from command, given that members are to be added with another command, ex: /party inv
                this(leader, new HashSet<>());
                this.members.add(leader);
         }

         //getters & setters & other variable manipulation methods
    }


    public class PartyManager {

         private Set<Party> parties = new HashSet<>();

         //methods for retrieving parties, etc
    }
     
    you could also assign only a members variable in the class, and use a

    Code (Java):

    Map<UUID, Party> parties;
     
    and have the leader be the key of the map.
     
    #15 ChefJava, Apr 25, 2017
    Last edited: Apr 26, 2017
    • Agree Agree x 2
  16. Ok guys the thank you all so much for the ideas. Ill choose the best out of all of them and then let you'll know
     
  17. Choco

    Moderator

    The best and most object-oriented approach would be @xMakerx and @ChefJava's suggestions. I highly highly highly recommend their suggestions because it will allow you to further expand in the future if you wish to have more features regarding parties. As mentioned prior, it is also object-oriented friendly, and the most sane way of accomplishing something. If you have to store a Collection in a different type of Collection / Map, then you're doing something wrong.
     
  18. Store it in runtime, then on intervals or server restart (onDisable), serialize them and add to a file. On startup (onEnable), load them into memory again. You might wanna feature a cache pool, don't know if you want to worry about that though. I recommend using json(gson).
     
  19. I would rather store it in a Map<String, Party>, String being the id/name of the party. Then you can have a method to access it such as getPartyById(String id) which would return the Party object assosiated to that id.

    @xMakerx

    Then have a custom player object as well. Storing the id of which party they are in, so you could have a MyPlayer#getParty, which would just return getPartyById(getPartyId()).

    Would be more sufficient in the long run.