Multiple configs won't go in proper folder

I’m trying to have my plugin create 2 default configs if they don’t already exist but they keep getting created in the root folder of the server

File coreFile = new File(this.configDir,“main.conf”);
File userFile = new File(this.configDir,“users.conf”);

these are causing the issue, they’re are being creating in the root directory because of the this.configDir.
instead try getParentFile().mkdir

also I’d try using a method to do all of this.
or
File configFile = new File(this.configDir, String As name of folder you want);

but I read that configDir is the easy way to get my plugins configuration directory

Here’s the usual way I do it.

/*
 * This variable (in the main class only) will be injected with a File instance pointing to the configuration file. If sharedRoot is false, it will be stored within config/pluginid/pluginid.conf. Otherwise, it will be stored in config/pluginid.conf.
 */

@Inject @DefaultConfig(sharedRoot = false) private File configurationFile;

public void prepareConfiguration() {
    configurationFile.getParentFile().mkdirs(); // This tells the system to create directories leading to the path where we need it.
    configurationFile.createNewFile(); // And then we create the new file. Load this into a config loader, write your defaults and you're done.
}

public void prepareExistingConfiguration() {
    if(configurationFile.exists()) {
        // do things
    }
}

EDIT: If you need to name configuration files differently, then just use new File(configurationFile.getParentFile(), "name.conf").

All of this is pseudo-code. Do not expect to compile.

I found the problem, it’s a mistake in SpongeCommon
Should be sharedRoot = false here
https://github.com/SpongePowered/SpongeCommon/blob/master/src/main/java/org/spongepowered/common/guice/SpongePluginGuiceModule.java#L82

1 Like

It is the easy way. :smile:

The only time I would recommend getting solely the @DefaultConfig is if you will not have multiple files.

If you won’t have multiple files, you needn’t worry about @ConfigDir at all.

should File coreFile = new File(this.configDir,"main.conf"); create main.conf in config/PluginID/ then? or is it a mistake in SpongeCommon as @simon816 said?

It should. You’re using it, correctly. However, there’s a problem in the current SpongeCommon implementation which creates the error you’re getting.

Any news on multiple configurations? I’m not quite sure about any of this :stuck_out_tongue:

@numbrs how I’ve done it works but for now they’re stored in the servers root folder, it seems to work fine and retrieve the configs normally so I can live with it till it’s sorted out.

@patey Well what about making multiple configurations inside the /config//? Shouldn’t this be an option when trying to keep your plugin configs all in one place?

It’ll do that when it’s done. It does it in the server root just cause it’s currently bugged.

I see. @TBotV63 :smile:

For those unaware, you can easily retrieve the folder /<server-root>/config/<plugin-folder>/ with:

@Inject
@ConfigDir(sharedRoot = false)
File folder;

EDIT: Oops! This has already been mentioned… Ignore meh xD