Development Assistance Save yaml with 'custom' saveConfig() method

Discussion in 'Plugin Help/Development/Requests' started by Ziron5, Apr 4, 2015.

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

    Ziron5

    Hi Community :),

    I am at the very beginning of the development of this plugin so my question is very easy:

    I have those two Methods in my Main class:

    Code:
    public static YamlConfiguration getConfig(String config) // teams | teamChests | loginHistory | spawnpoints | messages
        {
            File file = new File("plugins/MrVaro/" + config + ".yml");
            return YamlConfiguration.loadConfiguration(file);
        }
     
        public static void saveConfig(String config)
        {
            File file = new File("plugins/MrVaro/" + config + ".yml");
            YamlConfiguration yaml = YamlConfiguration.loadConfiguration(file);
         
            try {
                yaml.save(file);
                System.out.println("SAVED"); //DEBUG
            } catch (IOException e) {
                System.out.println("FALLLI"); //DEBUG
            }
        }
    And in another Class this code:

    Code:
    if(args[0].equalsIgnoreCase("spawnpoint") || args[0].equalsIgnoreCase("sp"))
                {
                    int id = 0;
                    try
                    {
                        id = Integer.parseInt(args[1]);
                    } catch (NumberFormatException e)
                    {
                        p.sendMessage(ChatColor.RED + "/mv spawnpoint <id>");
                        return true;
                    }
                    Location loc = p.getLocation();
                    int x = loc.getBlockX();
                    int y = loc.getBlockY();
                    int z = loc.getBlockZ();
    
                    YamlConfiguration config = Main.getConfig("spawnpoints");
                    config.set("spawns." + id + ".x", x);
                    config.set("spawns." + id + ".y", y);
                    config.set("spawns." + id + ".z", z);
                    Main.saveConfig("spawnpoints");
    
                    p.sendMessage("Success" + x + y + z + "   " + id);
    
                }
    The line 'Main.saveConfig("spawnpoints")' doesn't seem to do it's job. The perviously set values don't show up in the file even though the text editor says, the file has been edited by an external program. But it just stays empty. It works when I do the saving directly "manually" in the code but I want it to be easy and slim.

    Can someone tell my why the set vaules somehow get lost in the process of invoking the saveConfig() method and maybe a way to fix that?

    Thanks in advance! :)

    Ziron5
     
  2. Offline

    nverdier

    @Ziron5 If you want an easy way to manage custom/multiple configs, check out the Config class of EnviousAPI. Link in signature.
     
  3. Offline

    Ziron5

  4. Offline

    nverdier

    @Ziron5 Because you're loading the configuration from the file and then saving it right back to the file.
     
  5. Offline

    Ziron5

    isn't that how it's supposed to be? :O
     
  6. Offline

    nverdier

    @Ziron5 You want to load it, modify it, and then save it.
     
    Ziron5 likes this.
  7. Offline

    Ziron5

    Oh I get the mistake now. On loading the config again in the saveConfig() method I anullate the changes I made before.
    Thanks dude!

    For the record here are the modified methods:

    Code:
    static YamlConfiguration yaml;
        public static YamlConfiguration getConfig(String config) // teams | teamChests | loginHistory | spawnpoints | messages
        {
            File file = new File("plugins/MrVaro/" + config + ".yml");
            yaml = YamlConfiguration.loadConfiguration(file);
            return yaml;
        }
       
        public static void saveConfig(String config)
        {
            File file = new File("plugins/MrVaro/" + config + ".yml");
           
            try {
                yaml.save(file);
                System.out.println("SAVED"); //DEBUG
            } catch (IOException e) {
                System.out.println("FALLLI"); //DEBUG
            }
        }
     
  8. Offline

    nverdier

    @Ziron5 No need for the static...
     
  9. Offline

    Ziron5

    But I can't invoke the Method from the other class then
     
  10. Offline

    nverdier

    @Ziron5 Just pass an instance of the class to whatever class you want to use the methods in.
     
    Ziron5 likes this.
  11. Offline

    Ziron5

    That leads me to an error that says I activated the plugin twice

    ^
     
  12. Offline

    nverdier

    @Ziron5 You don't create a new instance, you just pass the original instance through a constructor.
     
  13. Offline

    Ziron5

    omg I did it wrong for such a long time D: I coded an entire plugin with making everything static xD Well. Guess I have to thank you again kind sir :D I am a better programmer now ^^
     
  14. Offline

    nverdier

    @Ziron5 Glad to be of assistance :D
     
    Ziron5 likes this.
Thread Status:
Not open for further replies.

Share This Page