Modifying a FileConfiguration

Discussion in 'Plugin Development' started by Reflxction, Nov 6, 2017.

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

    Reflxction

    Hi,
    I've been running into a problem when trying to save a string to a configuration, then saving the file. Here's my method:

    Code:
        public void saveArena(String arena) {
            List<String> s = plugin.getMapsFile().getStringList("Arenas");
            s.add(arena);
            plugin.getMapsFile().set("Arenas", s);
            try {
                plugin.getMapsFile().save(new File(plugin.getDataFolder(), "maps.yml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    The string seems to be added to the file, but the problem is probably when it saves it. No stack-traces were thrown.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @xTechno_ What is the issue then? What is the error?
     
  3. Offline

    Reflxction

    @timtower Since it doesn't save, I can't tell if it actually does add the string to the config. What I'm trying to achieve is make it save, but I can't seem to find the proper way since the way before doesn't seem to work
     
  4. Offline

    Caderape2

    @xTechno_ The method is good. The problem might come from your 'plugin.getMapsFile()'
    After set the new list, loop 'plugin.getMapsFile().getStringList("Arenas")' for see if everything is fine.
     
  5. Offline

    TheWolfBadger

    Is getMapsFile() returning a YAML configuration?
     
  6. Offline

    Reflxction

    @Caderape2 Seems to print the value of the getter correctly. When I looped it threw an NPE, which could mean that it wasn't adding the string. Not sure what's wrong when adding it to the list :/


    @TheWolfBadger it returns a FileConfiguration. Not sure if there's any difference though
     
  7. Offline

    TheWolfBadger

    You should be using YamlConfigurwtion for custom configuration files. Can we see your getMapsFile() method please?
     
  8. Offline

    Reflxction

    @TheWolfBadger
    Declaring the variable:
    Code:
        public File mfile = new File(getDataFolder(), "maps.yml");
        public FileConfiguration maps = YamlConfiguration.loadConfiguration(mfile);
    
    Getter:
    Code:
        public FileConfiguration getMapsFile() {
            return maps;
        }
    
     
    Last edited: Nov 6, 2017
  9. Offline

    Caderape2

    @xTechno_
    If you have a NPE while looping and the getter is valid, so your list is null.
    Use the method contains for see if the path exists.

    You might want to post your actual class and the main class for a better help.
     
  10. Offline

    Reflxction

    @Caderape2 I did some further debugging and the NPE was actually the "plugin". I forgot to initialize it in the onEnable method, and the array is printed correctly. The problem is, it doesn't add the arena to the file. I print before creating it and after creating it, and it just prints "[]" meaning it's empty. Any idea? :/
     
    Last edited: Nov 7, 2017
  11. Offline

    Reflxction

    1- Sorry for the bump/necropost and the double post, pls no warn
    EDIT by Moderator: Necro's are impossible these days, auto locks.

    2- I've got it to working. In case anyone ran into the problem, heres the code

    Code:
        public void saveArena(String arena, CommandSender sender) {
            List<String> list = new ArrayList<>();
            list.add(arena);
            ArenaObj obj = new ArenaObj(arena, ((Player) sender).getWorld());
            plugin.getMapsFile().set("Arenas." + arena + ".Name", obj.getArenaName());
            plugin.getMapsFile().set("Arenas." + arena + ".World", obj.getWorld().getName());
            //do your thing to save the file
        }
    
     
Thread Status:
Not open for further replies.

Share This Page