Save/Load hashmap

Discussion in 'Plugin Development' started by Burnsalan18, Nov 19, 2014.

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

    Burnsalan18

    I cant get these methods to work properly. Have the save method in ondisable and the load method in the onEnable method. Any thoughts?

    Code:
            public void saveMap(){
                List<String> s = getConfig().getStringList("Players");
                for (String p : taskID.keySet()) {
                    s.add(p);
            }
         
            getConfig().set("Players", s);
            saveConfig();
            }
            public void loadMap(){
                List<String> s = getConfig().getStringList("Players");
                for(String l : s){
                   
                    taskID.put(l, 0);
                    getConfig().set("Players", null);
                }
            }
    
     
  2. Offline

    pookeythekid

    Burnsalan18 What about it isn't working? I can tell you one thing for sure: you just forgot to saveConfig(); in your loadMap() method.
     
  3. Offline

    Burnsalan18

    lol that explains why it wasnt removing from the config. But when I check if the taskID contains the key in my player join event it doesnt work. As if the loadMap method does not return the string to the hashmap.

    Code:
            @EventHandler
            public void onPlayerJoin(PlayerJoinEvent event){
                Player player = event.getPlayer();
                if(taskID.containsKey(player.getName())){
                    endSafari(player);
                }
            }
     
  4. Offline

    pookeythekid

    Burnsalan18 Ah, that's what else is wrong. :p You set the config player content stuff to null inside of your for loop, so you only get one player into your map before the config's content is set to null and then you don't have anything to get more players from. That may have seemed a little sloppy; I'm not very focused at the moment and I haven't gotten too much sleep lately...
     
  5. Offline

    Burnsalan18

    Ill remove the part where it sets the config to null and see what happens.

    Edit: Alright so I couldnt figure it out so I changed to the SLAPI and it still doesnt work.

    Code:
            public void onSave(){
                try {
                    SLAPI.save(taskID, getDataFolder() + File.separator + "players.bin");
                } catch(Exception e) {
                    e.printStackTrace();
                  }
            }
            public void onLoad(){
                try {
                    taskID = SLAPI.load( getDataFolder() + File.separator + "players.bin");
                } catch(Exception e) {
                    //handle the exception
                    e.printStackTrace();
                }
            }
    Is it just me cause loading hates me?
     
Thread Status:
Not open for further replies.

Share This Page