Simple plugin configuration tutorial?

I’ve been reading the configuration guides on the sponge docs, but it seems overly complicated and I’m having a hard time figuring out what I should do for a really simple config. I just need to be able to create a default config if it doesn’t exist (either in code or from JAR, not sure which is better) and then read a few values at plugin initialize. I don’t want to have to put more code in than I need, and I don’t want to write a ton of my own code or code that doesn’t use the API well (If I have to I guess I will). Are there any tutorials or guides that show a lot of examples? Can anyone help me out?

P.S. I am coming from Bukkit/Spigot, what I used to do was just call saveDefaultConfig() in onEnable() and then when I need a value call getConfig().getInt(“path”) etc…

Same here, docs are quite confusing.

Yeah the new system using zml’s configurate can be slightly complicated to begin to use but once you have your head wrapped around it, it actually works out very nicely and is fairly easy to use.

As you might have read in the docs the system is completely different compared to Bukkit’s use of yaml. To begin
What i like to do inside my main class is create a method to create/setup the config for me.

public class MainClass {

    @Inject
    @ConfigDir(sharedRoot = false)
    private Path configDir;

    private Path configFile = Paths.get(configDir + "/config.conf");

    private ConfigurationLoader<CommentedConfigurationNode> configLoader = HoconConfigurationLoader.builder().setPath(configFile).build();
    private CommentedConfigurationNode configNode;

    @Listener
    public void onPreInit(GamePreInitializationEvent e){
        //Creates config directory, your could also create a method for this.
        if(!Files.exists(configDir)){
            try{
                Files.createDirectories(configDir);
            }catch(IOException io){
                io.printStackTrace();
            }
        }
        setup();
    }
    //Creates config file not directory
    public void setup(){
        if(!Files.exists(configFile){
            try{
                Files.createFile(configFile);
            
            }catch(IOException e){
                e.printStackTrace();
            }
        }else{
            load();
        }
    }

    //Loads config file if it already exists
    public void load(){
        try{
            configNode = configLoader.load();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    //And obviously saves config whenever needed.
    public void save(){
        try{
            configLoader.save(configNode);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

}

Might be some small indent errors or typos, i did this in the embedded markdown editor, if you have any questions just let me know,

Oh and yeah to access the config outside of the main class and to access the config directory you can create methods like so in the bottom of you main class (of course create a static instance and a getter first to access the main class)

public Path getConfigDir(){
    return configDir;
}
public CommentedConfigurationNode get(){
    return configNode;
} 

If anyone is able to look over my configuration changes to the docs and provide feedback I’d appreciate it.

I’ve gotten somewhat sidetracked and distracted by changes in my life at the moment, so I can’t quite remember how far I was through editing it the last I checked. So go nuts.

This looks like what I’ve seen, but isn’t configurate supposed to be able to handle config file and directory creation? How does that work? And then how would you load default values?

EDIT: The last example on this page https://docs.spongepowered.org/en/plugin/configuration/index.html shows three variables. This is what I meant by too much code. Are they all needed? What’s the simplest and best way?

You simply need the Path variable, which you can then use to get a File. If you use Configurate to get a Path it should be generated iirc.

Can you show an example?

Slight overkill, but Cookbook/ConfigDatabase.java at master · SpongePowered/Cookbook · GitHub

or

@Inject // is this needed?
@DefaultConfig(sharedRoot = false)
private Path config;

File f = config.toFile();
// Do stuff.

Something like that. I agree that the Docs are more of a hereiseverythingwecando than a straightforward guide.

I think you can also let it inject a File to the config file.

Okay. For my purposes a shared root might be okay, but I’ll worry about that later. Now how do I read simple node values at the root of the file (no more than 5 and no levels) and write defaults if they don’t exist? How In depth do I have to go?

Okay so I looked closer at that file you linked from github, and it looks like it may be able to help me some more. It’ll me more roundabout than Spigot, but that’s how most things are in the Sponge API so I’ll get started trying things out. I’ll ask more questions if I need to. Thanks!

Sorry, just saw your reply. It’s not more roundabout; it’s a completely different system. Spigot’s Configuration API depended on the fact that you only wanted one config file. After that, you’re on your own. Also, Path is the next-gen file. See http://stackoverflow.com/a/26658436/3152168. Sponge’s API allows you way more flexibility in how you make your config, abstracting HOCON or YAML using Configurate.

First, for many things I really do only need and want one simple config file. I really would love to spend more time writing code pertaining to the actual plugin than to just one config file.

Second, in Spigot, if there was a need for another config file, it was pretty straightforward to create a couple extra classes for the files and reimplement the functions that handled everything. At least that’s how I remembered it.

Even easier than that, you could just do FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), “players.yml”)), f’rintansce.

Yeah :slight_smile: Those were the good 'ol days