Are HashMap Cyclic?

Discussion in 'Plugin Development' started by xtremetom, Feb 24, 2014.

Thread Status:
Not open for further replies.
  1. I have a hashmap containing various bits of info I want to be added to player related hashmap.

    The main hashmap is created in plugin load and added to the player hashmap as the player logs in.

    Upon various player actions the information from the player log map is editted. Howeever, I am finding that editing the playerlog map is editing the map created when the plugin is enabled.

    Here is a rough breakdown:

    Code:
    public void onEnable(){
      //create main hashmap
      prc.put(name, al);
    }
     
    ///// in listener
    void onPlayerJoin(PlayerJoinEvent event){
      plog.put(pname, prc);
    }
     
     
    public void blockbreak(BlockBreakEvent e) {
      //some code to edit the hashmap
      plogs.put(pname, infoupdate);
    }

    Problem is that upon editing the plogs the hashmap prc seems to be edited and so any player that joins has the new prc added to their plog. Also editing and plog affects all plogs.
     
  2. Offline

    Forseth11

    xtremetom On enable what is the var "name" and the var "al"
    For block break what is infoupdate.

    Can you show me what you have to initialize these hashmaps?
     
  3. Code:
        public static HashMap<String, HashMap<String, ArrayList<Integer>>> plog = new HashMap<String, HashMap<String, ArrayList<Integer>>>();
        public static HashMap<String, ArrayList<Integer>> rc = new HashMap<String, ArrayList<Integer>>();
        public static HashMap<String, ArrayList<Integer>> prc = new HashMap<String, ArrayList<Integer>>();
    name is the name of a task like "Mob Killer" and al is an arraylist of integers.

    infoupdate is prc edited:
    Code:
                      Map <String, ArrayList<Integer>> update = prc.get(pname);
                        update.get(aname).set(0, 0);
                        update.get(aname).set(1, complete);
    pname = players name
    complete = an interger count

    I did some testing it does seem as though and edits to the prc are global within plog.
     
  4. Offline

    NathanWolf

    Could you maybe post (or pastebin) the whole class? Or at least the relevant parts?

    It kind of sounds like you're making a single Map<String, ArrayList<Integer>> and assigning it to prc for every player (as in using the same instance everywhere) but without seeing the code that's doing all that we can't be sure.
     
  5. I am adding prc for each player within plog. Is there a nice way to make prc a separate instance for each player?
     
  6. Offline

    NathanWolf

    Well basically you would check prc to see if there is already a record for your player name in the map. If not, create a new Map<String, ArrayList<Integer>> and put it in the prc map for that player name.

    If the player does already exist, then just use the map that's there like you're doing now.

    And as a friendly suggestion, you may want to use some more descriptive variable names, especially when posting code to get help- it's kinda hard to tell what's going on here :)
     
  7. Offline

    CubieX

    What about using a class? You are using Java. So why not do it object oriented. ;)
    Create a class that has this player-specific hash maps as fields and also the players name and whatever other attribute you need.
    Then you can instanciate this class for every player and store those objects in a single HashMap<String, MyClass>.
    Add get() and set() methods to alter the HashMaps or other attributes in this class.
    Then you can manage this data very comfortable.
     
  8. Thanks to all the above info.
    In the end I referenced the required hashmap as a new HashMap(prc); per player and this seems to have stopped the cyclic nature of the whole thing.
     
    NathanWolf likes this.
Thread Status:
Not open for further replies.

Share This Page