Loading from config file

Discussion in 'Spigot Plugin Development' started by JacksonHogan, Jan 14, 2018.

  1. I'm trying to load saved data from a config file.
    Code (Text):
    public void createMap(String name, Location spawn){
            String serializedLoc = serializeLoc(spawn);
            MapSwitcher.plugin.getConfig().set("maps."+size+".name", name);
            MapSwitcher.plugin.getConfig().set("maps."+size+".spawn", serializedLoc);
            MapSwitcher.plugin.saveConfig();
            Map map = new Map(size, name, spawn);
            maps.add(map);
            size++;
            return;
        }
       
        public void loadMaps(){
            for(String id : MapSwitcher.plugin.getConfig().getConfigurationSection("maps").getKeys(false)){
                String name = MapSwitcher.plugin.getConfig().getString("maps."+id+".name");
                String loc = MapSwitcher.plugin.getConfig().getString("maps."+id+".spawn");
                createMap(name, deserializeLoc(loc));
            }
            return;
        }
    Config.yml
    Code (Text):
    message_mapexists: The specified map name already exists! Please try another name.
    message_nomapavailable: There are currently no maps available!
    message_mapswitched: 'The map was successfully switched to: %s'
    message_5minutes: The map will be switching in 5 minutes!
    message_1minute: The map will be switching in 1 minute!
    message_5seconds: The map will be switching in 5 seconds!
    int_switchdelay: 30
    permissions:
      cmd_mapswitcher: mapswitcher.use
    maps:
      '1':
        name: TESTF
        spawn: Ironwood3,-575,98,31,293.36407,6.9763412
      '2':
        name: TESTFA
        spawn: Ironwood3,-561,105,41,101.612915,26.066936
     
    The issue is that it loads it like this...
    https://gyazo.com/c416631b806e83d350dfc007e1f0fd0d
    It loads the same map three times instead of loading both seperate maps in the config.

    EDIT:
    Found my problem...
    Code (Text):
    public void loadMaps(){
            for(String id : MapSwitcher.plugin.getConfig().getConfigurationSection("maps").getKeys(false)){
                String name = MapSwitcher.plugin.getConfig().getString("maps."+id+".name");
                String loc = MapSwitcher.plugin.getConfig().getString("maps."+id+".spawn");
                Map map = new Map(Integer.parseInt(id), name, deserializeLoc(loc));
                maps.add(map);
                size++;
            }
            return;
        }
     
    #1 JacksonHogan, Jan 14, 2018
    Last edited: Jan 14, 2018
    • Like Like x 1
  2. simpleauthority

    Supporter

    Please mark this thread as solved & lock it from further replies so that we can ensure we don't waste time looking through a solved post.