Solved Saving/Loading <String, Integer> Hashmap.

Discussion in 'Plugin Development' started by MasterGlueXX, Mar 30, 2016.

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

    MasterGlueXX

    Hello, I'm sorta new to Java and coding plugins, recently I've been working on a plugin that gives you points for killing mobs. So I created a hashmap for points.
    Code:
    private HashMap<String, Integer> points;
        {
        points = new HashMap<String, Integer>();
        }
    Next is loadPoints and savePoints
    Code:
    public void loadPoints() {
            points = new HashMap<String, Integer>();
            if(getConfig().getConfigurationSection("points") != null ) {
                Set<String> set = getConfig().getConfigurationSection("points").getKeys(true);
                for(String credit : set) {
                    int value = getConfig().getInt(credit);
                    points.put(credit, value);
                }
            }
        }
       
        public void savePoints() {
            for(String point : points.keySet()) {
                int value = points.get(point);
                getConfig().set("points."+points, value);
            }
            saveConfig();
        }
    Now when I reload it saves in a config.yml folder, but the problem is it duplicates the data for some reason.
    Like when it's supposed to save as '{MasterGlueXX=0}': 0. It saves as something like '{{MasterGlueXX=0}=0}': 0. Then on the next reload it saves like '{{MasterGlueXX=0}=0, {{MasterGlueXX=0}=0}=0}': 0, and so on. It also doesn't delete the previous line so all of these stack up until its at a point where when I try to view the hashmap through a command in game it crashes me saying its to long.

    Would be amazing if someone can find out what's wrong. If you need any more of my code let me know.
     
  2. Offline

    TheDiamond06

    Uhm I don't think you wrote this line right:
    Code:
    getConfig().set("points."+points, value);
    
    You have your string "point" looping the keys. However you have ("points."+points) not ("points." + point)
     
  3. Offline

    MasterGlueXX

    OH wow... I'm so dumb. Thanks for that and one more question if you don't mind. If I wanted to clear the key and the value in the loadPoints how would I do that, because everytime i try to delete everything in the config file it just reappears at where it was last saved. I tried clearing the hashmap but it just reappears again. Thanks a lot for pointing out that first mistake.
     
  4. Offline

    TheDiamond06

    Are you trying to clear ALL the points, or just one player's points. Either way can I see your code of how you are trying to clear it so far.
     
  5. Offline

    MasterGlueXX

    Code:
    public void loadPoints() {
            points = new HashMap<String, Integer>();
            if(getConfig().getConfigurationSection("points") != null ) {
                Set<String> set = getConfig().getConfigurationSection("points").getKeys(true);
                for(String credit : set) {
                    int value = getConfig().getInt(credit);
                    points.put(credit, value);
                    set.clear();
                }
            }
        }
     
        public void savePoints() {
            for(String point : points.keySet()) {
                int value = points.get(point);
                getConfig().set("points." + point, value);
            }
            saveConfig();
        }
    I added the set.clear() It sort of works but i want it to save like MasterGlueXX = 0, but it saves as {{MasterGlueXX=0}0} Which i think means its treating the old key and value as an entire key (Correct me if I'm wrong which I probably am)
    EDIT- Oh sorry and by clear i mean so that it doesnt save like {{MasterGlueXX=0}0} which i think is why its happening. Another thing i want it to keep the integer value so for example it saves like MasterGlueXX = x (x = amount of points) Again probably wrong.
    Thanks for the reply.
     
    Last edited: Mar 30, 2016
  6. Offline

    TheDiamond06

    Your saving is just fine. However I don't get what you are doing with loading the points into the HashMap. You set your Set<String> to the configuration sections with getting the keys as true. Put that as false and it will load as it should. Also you should get rid of set.clear(); when you are loading points. If you want to clear everyones points, or just one player's points create another method to do so. You would then just set their value instead of their points number, to null.
     
  7. Offline

    MasterGlueXX

    Thank you very much for that, I'll look more into this stuff and hopefully completely understand what exactly this code means/does. It's saving perfectly fine now. Sorry if I was making very obvious mistakes, still very new to hashmaps in general let alone saving/loading them. This will help me out a lot. Have a nice day :D
     
Thread Status:
Not open for further replies.

Share This Page