Problems with editing/saving custom configuration

Discussion in 'Spigot Plugin Development' started by Soap_Ninja, Oct 9, 2019.

  1. Basically, I wanted to create a custom YAML configuration file to hold permission configurations. I created a subclass of YamlConfiguration called PermissionConfiguration. The file is created and loaded without errors, but when I try to add data to it and save it from my main class, it is still blank. However, if I try to add data and save it from the PermissionConfiguration class, it works. I'm not sure why, but I believe it is a java problem.
    Any help would be greatly appreciated.

    Relevant portions of my main plugin class:
    Code (Java):
    public class PLUGINNAME extends JavaPlugin implements Listener
    {
    PermissionConfiguration permConfig;
    //omitted
        @Override
        public void onEnable()
        {
            //omitted
            permConfig = new PermissionConfiguration(this);
            permConfig.set("groups.default.default", true);
            permConfig.saveConfig();
        }
    }
    Whole PermissionConfiguration Class
    Code (Text):
    package me.soap_ninja.cisco;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class PermissionConfiguration extends YamlConfiguration
    {
        private JavaPlugin plugin;
        private File permFile;
        private YamlConfiguration permConfig;
       
        public PermissionConfiguration(JavaPlugin plugin)
        {
            this.plugin = plugin;
            loadPermissionConfig();
           
        }
        public void loadPermissionConfig()
        {
            //setup permissions file
            this.permFile = new File(plugin.getDataFolder() + "\\permissions.yml");
            if (!this.permFile.exists())
            {
                this.permFile.getParentFile().mkdir();
                plugin.saveResource("permissions.yml", true);
            }
           
            //load permissions configuration
            this.permConfig = new YamlConfiguration();
            try
            {
                this.permConfig.load(this.permFile);
            }
            catch (IOException | InvalidConfigurationException e)
            {
                e.printStackTrace();
            }
        }
        public YamlConfiguration getConfig()
        {
            return this.permConfig;
        }
        public void saveConfig()
        {
            try
            {
                this.permConfig.save(this.permFile);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return;
        }
    }
     
     
  2. Tip:

    Rarely is it a java problem, more than likely it is a problem of code working exactly as you designed it, but not how you wanted it designed.

    There isn't really a reason for you to make a subclass of YamlConfiguration. Just init the file for example in your 'main' class and allow access through there (really should be in a file manager but thats down the road). You run into too many issues if you are trying to do your method and you aren't 100% sure what you are doing. Never go more complicated than you need to be unless you have a specific reason to and in this case there really isn't a reason.

    If you want a jump start on how to do a file manager, take a look at BadBones69's FileManager: link

    Its very easy to use and comes with some documentation on how to implement it yourself.

    Implementing it into your main class is super easy as well.
    Link1 and Link2.

    Using it elsewhere is also super easy: link

    It's a handy little class that I would recommend taking a look at to learn how it works and implementing it yourself into plugins.
     
  3. Issue is simple you have 2 YamlConfiguration object
    A - PermissionConfiguration
    B - permConfig in PermissionConfiguration

    And what you are doing is:

    A.set("something");
    B.save("someFile");
     
  4. to elaborate, your permissionconfiguration is ALREADY a yamlconfiguration, yet youre creating a new yamlconfiguration inside of it. so youre saving and loading the one thats created inside permissioncofiguration, but youre setting values to permissionconfiguration itself and not to the one you are saving and editing.

    you dont need a custom configuration for this unless youve omitted the reason youre doing this.
    instance a new YamlConfiguration wherever you need iwth YamlConfiguration#load(File) and delete your PmerissionConfiguration class,
    or do not create a new yamlconfiguration inside your permission configuration class.
     
  5. Rodriqez Warren1001 Thanks for the replies. I meant that I thought it was a problem with my implementation in Java, as opposed to a problem with my implementation of the Spigot API. I didn't mean to imply that my code was perfect.
    I'm mostly just making this plugin to practice what I'm learning about java and I wanted to play around with subclasses. I plan on creating a method in PermissionConfiguration that lets me get an array of strings containing all the permissions of a certain group so I can easily assign them to players, and I thought a dedicated class would make it neatest.
     
  6. the default yamlconfiguration offers a fair amount of usefulness already. #getStringList gets a list, if you NEED an array just toArray your list.