Create, edit, and access json in config folder?

Hello!

I’d like to create a json to manage data for my plugin I’m making, and was wondering what the easiest way to create, edit, and access a json file would be? I feel like this is going to be different than the standard config file, so I thought I would check. Any and all information possible is great and will be extremely appreciated! Thanks!

I’d either recommend Jackson or Configurate. There is documentation on how to use Configurate on the SpongeDocs, though you’d need to replace references of ‘Hocon’ with ‘Json’, obviously. Also, there may be some technical differences, but @zml could probably answer those better than I.

If you do use Configurate, you need to ship configurate-json with your plugin, else it will not work. Only the base library and configurate-hocon are shipped with sponge.

2 Likes

If you do ship configurate-json with your plugin, please shade it, otherwise it will cause version mis-matches with others that use it.

1 Like

So despite a pretty solid foundation in Java, I don’t really have much experience working with things like Configurate or Jackson. I know how to work with JSONs generally, but could I possibly get a step by step explanation of how to set up a sample JSON with one of those? <3

There’s much more in-depth, understandable documentation and examples on Jackson around the web that we could provide here.

Wouldn’t there be extra steps to install it to a sponge plugin and set the location to the proper config folder though?

https://docs.spongepowered.org/en/plugin/advanced/config-in-depth/index.html
This shhuld have the information to get you started on using configurate with sponge. Also, for JSON it’s almost the same.

Alright, I’ll go ahead and try to make something small tonight and see how it goes!

This looks very do-able! Quick logic question though, if I’m using the config file to hold lots of plugin data, is it better to retrieve all of the data and store it locally in my plugin for access at runtime, or should I just access values directly from the file when necessary? I have a gut feeling that it would be better to pre-load my data into a local hash map in the plugin, although I’m not sure.

Generally, it’s better to pre-load data and then periodically update the local storage with the new data on the server.

Okay that makes sense, thanks @FerusGrim! Another question, I have it successfully making the config file, however I’m having trouble putting it in a folder for just my plugin in the config folder. I’ve tried:

String path = "config"+File.separator+"pixelmasters"+File.separator+"data.conf";
this.dataFile = new File(path);
this.dataFile.getParentFile().mkdirs(); 
this.loader = HoconConfigurationLoader.builder().setFile(this.dataFile).build();

however I get an IO error. It successfully works and puts the config file in the root server folder when I do:

this.dataFile = new File("data.conf");
this.dataFile.getParentFile().mkdirs(); 
this.loader = HoconConfigurationLoader.builder().setFile(this.dataFile).build();

and I’ve decided to just use HoconConfigurate by the way, instead of JsonConfigurate, since there’s more documentations and no specific need for JSONs.

You’re doing this the difficult way. :stuck_out_tongue: You can get your configuration directory easily.

@Inject @ConfigDir(sharedRoot = false) private File configDir;
1 Like

So then how would I properly set the file location to the configDir you specified and name it data.conf? Would I still use the File.separator’s for the path?

Progress of trying:

String path = configDir + File.separator + "pixelmasters" + File.separator + "data.conf";
this.dataFile = new File(path);
this.dataFile.getParentFile().mkdirs(); 

Makes almost what I want, except it puts the folder “myplugin” with “data.conf” inside it in my root directory in a folder named null. Maybe I need to get the path from the configDir somehow when making the String?

Progress of trying part two:

I tried getting the path of the config file with the following methods, however it crashed :confused:

String path = configDir.toPath().toString() + File.separator + "pixelmasters" + File.separator + "data.conf";

configDir.getPath() didnt work either. :frowning:

Progress of trying part three:

String path = "config" + File.separator + "pixelmasters" + File.separator + "data.conf";

worked, although I didn’t use your easy way haha…

Okay new question (ugh):

I want to create a test node called ignore and set the value of it to “hi”. When I run the following code, it creates the data.conf file in the right place, however it doesn’t set any values inside it (when I open it after this code runs, it’s empty). Help?

this.loader = HoconConfigurationLoader.builder().setFile(this.dataFile).build();
this.rootNode = loader.createEmptyNode(ConfigurationOptions.defaults());
ConfigurationNode loadNode = rootNode.getNode("ignore");
loadNode.setValue(true);
try {
    this.loader.save(loadNode);
} catch(IOException e) {
    this.logger.warn("Wand Command - IOException saving testNode");
}

What am I doing wrong? @FerusGrim

File dataFile = new File(this.configDir, "data.conf");

What about my most recent question of setting config file values @FerusGrim?

File dataFile = new File(this.configDir, "data.conf");
ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder().setFile(potentialFile).build();

ConfiguratonNode root;

try {
  root = loader.load();
} catch(IOException e) {
  // create a new node if we couldn't load an existing one
  root = loader.createEmptyNode(ConfigurationOptions.defaults());
}

// Get the node and set its value to hi
rootNode.getNode("test").setValue("hi");

try {
    loader.save(rootNode);
} catch(IOException e) {
    log.warning("Encountered an IO exception while attempting to save config.", e);
}

Result:
data.conf:

test=hi

For more information see the Sponge Documentation, it’s all nicely written down there. We help those who help theirselves.

Thanks @TBotV63! It was just a little unclear to me on how I needed to chain the rootNode.getNode(“test”).setValue(“hi”) based on the documentation, but it all makes sense to me now!

2 Likes