How to use Simplix Storage

0
Share

Simplix Storage is a configuration file api designed to be similar to Bukkit’s Configuration API however it does not depend on it at all and is coded in a much more optimized way. It also supports file types other than yaml such as json and toml.

Pros/Cons

Pros

  • Is similar to Bukkit’s file configuration api meaning there isn’t much to learn and isn’t that hard to migrate from
  • Allows for more stream line code with their getOrDefault methods or getOrSetDefault.
  • Easy custom serializables
  • Easier to write code more efficiently
  • More settings (such as reload settings & preserving comments)

Cons

  • The development is quite slow
  • Documentation needs some updating (however I am working on an updated wiki for it)
  • No annotations system

Now that you’ve decided If you want to use it or not we can started on how to use it.

Setting up your config file

Now as I said above, Simplix storage supports multiple file types. Currently Json, Yaml & Toml. (You can read more about these here). For this tutorial I am going to use yaml as that is the standard in Bukkit plugin development. However the syntax should be the same for all file types which is nice.

To create a config file we’re going to need the Yaml class. If you are making a json file, its going to be Json and Toml for toml. Pretty self explanatory. They all take the same parameters. Just different class names. To create our config we can just do this: (Make sure your in your plugin’s main class)

Yaml config = new Yaml("config.yml", this.getDataFolder().getAbsoloutePath(), this.getResource("config.yml");
Code language: Java (java)

What this is going to do is load a yaml file in our plugin’s data directory. If the file does not exist, it will create a new one loading the data from the config.yml file in our resources folder. Now if you wanted to just have it create an empty file without anything from the resources folder, you can just remove the last parameter

How to do this without the spigot api?

If you are trying to use this api outside of the bukkit/spigot api, you can use these methods to get the parameters instead:

Yaml config = new Yaml("config.yml", System.getProperty("user.dir"), this.getClass().getResourceAsStream("/config.yml"));
Code language: Java (java)

This will create a config.yml file in the same folder that your jar is being run in or load an existing one if it is found. Again if you want it to just create an empty file when it is created, just remove the last parameter.

There are a few other methods of doing this however this is the easiest and best in my opinion.

Basic usage

The following code will show how to get basic data:

String stringData = config.getString("my-string"); int intData = config.getInt("my-int"); boolean booleanData = config.getBoolean("my-section.boolean-data"); FlatFileSection section = config.getSection("my-section"); for (String key : section.singleLayerKeySet() /* Getting a "SingleLayerKeySet" will just get the keys in the section and none in any subsections */) { System.out.println(key); }
Code language: Java (java)

With the config file looking something like this:

my-string: "foo-bar" my-int: 42 my-section: boolean-data: true second-data: "The key of this will also be printed"
Code language: YAML (yaml)

Hopefully its similar to what your used to. The only big change is the sections class name and method names.

Time saving and writing optimised code

This is when Simplix Storage can really start to shine. Instead of doing this:

if(!config.contains("unset-data") { config.set("unset-data", "My unset data"); } return config.getString("unset-data");
Code language: Java (java)

Instead you can just do

config.setDefault("unset-data", "My unset data"); // Or even!! config.getOrSetDefault("unset-data", "My unset data"); // this set the second parameter if the value does not exist. // Or if you dont want to set it: config.getOrDefault("unset-data", "My unset data"); // Will return the second parameter if it is unset however wont set it in the config
Code language: Java (java)

Then some performance saving stuff looks a bit like this:

// Bad yaml.set("Key-1", "Value-1"); yaml.set("Key-2", "Value-2"); yaml.set("Key-3", "Value-3"); // Good yaml.getFileData().insert("Key-1", "Value-1"); yaml.getFileData().insert("Key-2", "Value-2"); yaml.set("Key-3", "Value-3"); // Benefit: File will only be written to once. // Bad yaml.remove("Key-1"); yaml.remove("Key-2"); yaml.remove("Key-3"); // Good yaml.removeAll("Key-1", "Key-2", "Key-3"); // Benefit: File will only be written to once.
Code language: Java (java)

Reloading

With Simplix Storage you don’t actually have to worry about reloading. It will do it automatically every time the file is changed. However you can change the setting on what it should do by doing config.setReloadSetting(ReloadSetting). You can also read about what the different settings mean here.

If you do want to reload config, you just do config.forceReload(). It sounds unsafe, but don’t worry, its just the same as reloading a file with Bukkit’s configuration api.

Conclusion

That’s pretty much everything you should know about Simplix Storage. Any other information will be on the wiki. If you have any questions, shoot me a reply, dm. Alternatively you can contact the owner of the library via a GitHub issue.

https://github.com/Simplix-Softworks/SimplixStorage/wiki/

https://github.com/Simplix-Softworks/SimplixStorage/


Subscribe to my newsletter!

Interested in how to add update notifications to your plugin? Read this: