Custom configuration files don't saves.

Discussion in 'Spigot Plugin Development' started by MartinPL, Aug 17, 2018.

  1. Hello! I know that there are many threads like this but i still can't find solution. I want to have other class to manage these files. I have one but files didn't save.

    Here's my FileManager class:

    Code (Text):

    public class FileManager {
       static String path = Main.getInst().getDataFolder().toString();
       
       
       public File createFile(String name) {
           File f = new File(path + "/" + name + ".yml");
           if(f.exists()) return f;
           create(name);
           
           return f;
       }
       
       public YamlConfiguration getConfig(String name) {
           File f = createFile(name);
           
           YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
           return yml;
       }
       
       public void save(String name) {
           File f = createFile(name);
           YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
           try {
               yml.save(f);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       
       private void create(String name) {
           File f = new File(path + "/" + name + ".yml");
           FileConfiguration yml = YamlConfiguration.loadConfiguration(f);
           try {
               yml.save(f);
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
    }
     
    And here's code where I set something in config:

    Code (Text):

    if(cmd.getName().equalsIgnoreCase("config")) {
               if(args.length == 1) {
                   FileManager fm = new FileManager();
                   fm.getConfig("test").set("hmm", "okay");
                   fm.save("test");
               }
           }
     
    It's creating a new empty file.

    Any suggestions?

    Thanks in advance.
     
  2. Try setting your fields via a YamlConfiguration object.
     
  3. Code (Text):
    fm.getConfig("test").set("hmm", "okay");
    It's setting via YamlConfuguration object
    Code (Text):
    public YamlConfiguration getConfig(String name) {
     
  4. Do you save the file after you write it? Not the field!
     
  5. FileManager fm = new FileManager();
    fm.getConfig("test").set("hmm", "okay");
    fm.save("test");

    Code (Text):

    public File createFile(String name) {
           File f = new File(path + "/" + name + ".yml");
           if(f.exists()) return f;
           create(name);
           
           return f;
       }
       
       public void save(String name) {
           File f = createFile(name);
           YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
           try {
               yml.save(f);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
     
    It's saving YamlConfiguration. Do I have to save File? And is this yml.save(f) good?
     
  6. Hmm
    Code (Text):

    [15:42:06 WARN]: Unexpected exception while parsing console command "config ty"
    org.bukkit.command.CommandException: Unhandled exception executing command 'config' in plugin rlFirma v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:636) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:437) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.lang.IllegalArgumentException: The embedded resource 'plugins/rlFirma/test.yml' cannot be found in plugins\rlFirma.jar
            at org.bukkit.plugin.java.JavaPlugin.saveResource(JavaPlugin.java:202) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at real.life.firma.FileManager.save(FileManager.java:34) ~[?:?]
            at real.life.firma.Main.onCommand(Main.java:60) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            ... 8 more
     

    When added:
    Code (Text):

    public void save(String name) {
           File f = createFile(name);
           YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
           try {
               yml.save(f);
               Main.getInst().saveResource(path + "/" + name + ".yml", true); //THIS
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
     
     
  7. You must add your file inside your project.
     
  8. And there's problem. I must have dynamic files with dynamic name like player nick. Is there any solution for this? I can use my own files but maybe there is another solution :p
     
  9. OK then, remove the saveResource method. And change to this:
    Code (Java):
    public void save(Player p) {
            File f = new File(plugin.getDataFolder().getAbsolutePath(), p.getName() + ".yml");
            FileConfiguration c = YamlConfiguration.loadConfiguration(f);
            c.set("myField", myValue);
            c.save(f);
    }
     
  10. Have this:
    Code (Text):

    public void save(Player p) {
            File f = new File(Main.getInst().getDataFolder().getAbsolutePath(), p.getName() + ".yml");
            FileConfiguration c = YamlConfiguration.loadConfiguration(f);
            c.set("myField","test");
            try {
               c.save(f);
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
     
    No error but config is empty

    Code (Text):

    if(cmd.getName().equalsIgnoreCase("config")) {
               FileManager fm = new FileManager();
               Player p = (Player) sender;
               fm.save(p);
           }
     
     
  11. You literally save the resource you just loaded from disk. You need to save the YamlConfiguration loaded earlier instead.
     
  12. So how I have to do this?
     
  13. Change this:
    Code (Text):
    c.set("myField","test");
    to this if there is no such field before:
    Code (Text):
    c.createSection("myField","test");
     
  14. Add a FileConfiguration parameter to the save method and use that instead of loading the config again.
    You can call set with a path that doesn't completely exist. You only need to call createSection when you need the ConfigurationSection instance.
     
  15. createSection is for (String string) or (String Map<?,?>) so this didn't work

    Any solutions? Btw in class Main simply File file = new File();
    YamlConfiguration yml = new...
    yml.set(path, object) worked perfectly. It was saving