Solved HashMap saving Gangs

Discussion in 'Plugin Development' started by nuno1212sss, Feb 27, 2014.

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

    nuno1212sss

    Hi I would like to know how to make a hashmap be able to save A Gang, without only saving it's name by a String, enum's are not going to work but I was hoping for someone to tell me a good way to do this(The "Gang" I want to save should actually contain all of the players in it, it's name, it's level, and it's owner),
    Ty in advance.
     
  2. Offline

    bobacadodl

    Do you know the basics of OOP? If not, I would recommend getting a better understanding of java first.

    Simply create a "gang" class to store the data requires for the gang, and have a List of gangs
     
    calebbfmv and Traks like this.
  3. Offline

    nuno1212sss

    bobacadodl "Recently" new to Java and this part is completly new to me, so right(Only 14, so you could be a little bit more comprehensive) never worked loads with those types of classes.
     
  4. Offline

    Vandrake

  5. Offline

    nuno1212sss

    Vandrake thanks :D that kind of helped me a little bit.
     
    Vandrake likes this.
  6. Offline

    bobacadodl

    Vandrake How exactly is polymorphism useful here?...

    What you want to do is create a new class, called "Gang". This gang class stores the necessary variables and information for each gang.

    For example..
    Code:
    public class Gang {
        private String name;
        private List<String> players = new ArrayList<String>();
        private int level = 0;
        private String owner;
       
        public Gang(String owner, String name) {
            this.name=name;
            this.owner=owner;
        }
       
        public String getName() {
            return name;
        }
       
        public List<String> getPlayers() {
            return players;
        }
       
        public void addPlayer(String player) {
            players.add(player);
        }
       
        public int getLevel() {
            return level;
        }
       
        public void setLevel(int level) {
            this.level=level;
        }
       
        public String getOwner() {
            return owner;
        }
    }
    Now, in your main plugin class, you could have a hashMap of Gangs and their names, for example

    Code:
    HashMap<String, Gang> gangs = new HashMap<String,Gang>();
    Then you could get a gang object by doing

    Code:
    Gang g = gangs.get("gangName");
    Then set its level, add players to it, etc.

    Or, you can create a new gang object and add it to the hashmap by doing

    Code:
    Gang g = new Gang("ownerName","gangName");
     
    gangs.put("gangName", g);
     
    calebbfmv likes this.
  7. Offline

    nuno1212sss

    bobacadodl thanks allot that should be perfect :D
     
    bobacadodl likes this.
  8. Offline

    Vandrake

    What you justdid there with the Gangs... That's called polymorphism
     
  9. Offline

    bobacadodl

    You sure? Its not from what I've learned. I thought polymorphism was when you have multiple classes that have different usages/functionality while sharing a common interface.

    This is just a single class that stores accessible data
     
Thread Status:
Not open for further replies.

Share This Page