[SOLVED] - My config files have {} for their nodes? How to remove?

I am attempting at creating a cross-compatible plugin, with the same essential configuration methods. I am using the exact same library that sponge uses, however, when I go to add or set items in my config and save it, I always end up with this really funny formatting, and with the “{}” brackets around some things. How would I remove these “{}” brackets and set the formatting to normal?

Do you have the code you’re using…?

I do, I use just a config object to manage stuff like this. Please note that when looking through this code, it is just a test plugin, and I completly understand that I should NOT be using it the way I am. Here is my class:

https://hastebin.com/vojenusiho.cs

Basically I just pass in a path, and a default value if I want to, and It should set it for me. Is it the method I am using that is messing it up?

Okay, so let me give you a quick rundown on how Configurate works and what you need to look into redesigning.

A configuration file is simply a representation of nodes where each node has a key and a value. Some nodes hold simple values like a boolean, int, or String, while others might hold lists of values or other nodes (aka children nodes).

When we get a node, we’re essentially requesting the node along a given path of keys where each key is an Object. For example, if we have the Item node shown in the picture from the original post, we can use itemNode.getNode("Id") to give us the node for the Id of the item (idNode).

In your example, you’re using String#split on an input, which results in a String[] array. The #getNode method uses varargs to accept any number of Objects - which are converted into a implicit array - and then called with #toString. While theoretically this should work fine, it can have undesirable results at times - especially with Java’s #split. Instead, it’s best to be working with a path of Objects, such as in getNode("Items", 0, "Item").

Now, it’s critical to know that our idNode is never null. However, we may have a situation where the given node isn’t present in the file - for example, if we get a node for the quantity of the item with itemNode.getNode("Quantity"), our resulting quantityNode isn’t present in the file. As such, the node is said to be virtual, which can be checked with quantityNode.isVirtual().

There’s a lot of other things to cover, so here’s a bunch of bullet points on things to consider.

  • Review the Configurate Wiki. Please.
  • Read the Sponge Docs for configuration
  • Use Object paths when working with nodes
  • Use #isVirtual to check if a node exists in the mapping
  • Use specialized methods like #getInt, #getList(TypeToken token), etc.
  • Use built-in default values - #getValue(Object default)
  • Use java.nio for handling Paths and the Files class
  • Do not fail silently on IOExceptions - at least print an error message and stacktrace
  • If your Path is from a String, chances are it’s wrong. See @DefaultConfig
  • No need to call load all the time if you’re not expecting the file to change
  • If you plan on having a default config, use the Asset Manager

As for your original question, I’d bet it’s a symptom of these other issues.

3 Likes

Thank you so much for the overall rundown!

A lot of this I did know, but I did not know most of it. I tried looking through the documentation of sponge, spigot, configurate, and even snakeyaml for more information on why this might be happening.

I can kind of start to see now as to how it would be an issue when I do string.split(). I thought maybe it would separate it out like sponge said when you do getNode(String, string, string) is == getNode(String).getNode(String).getNode(String).

However, if I were to do config.getNode(String).setValue(String); In my config, that node would still be surrounded in {} <- those brackets. I will try using more specific methods instead of using object I guess.

If the problem still persists, do you think I should try using SnakeYaml?

Brackets are standard for defining something that is an object, and I presume the same holds true in YAML. Are you sure messagesNode.getNode("Get Message").setValue("Item Given") doesn’t work?

Also, #setValue is independent on the actually Object type - it either uses #toString or serializers, if available.

Varargs in #getNode should theoretically handle it properly (after all, String[] is an Object[], but I’ve always seemed to have difficulty with it. If I recall, Configurate automatically separates strings containing . - might be worth a try.

Considering Sponge uses Configurate, I’d avoid using any other external library - Configurate works extremely well for my needs (which to be fair isn’t much :p), so it might even be a step down. It also could be worth looking into using HOCON as well - it’s a bit more verbose, but I’ve found I make less mistakes now than back in the Bukkit days and I’ve come to like it.

It still happens. :confused: All im doing is getting/setting the configuration node directly and it still does the same thing. :confused:

And I attempted using Hocon earlier with commented configuration nodes, however it looks a lot more complex. I personally don’t mind the complexity, but this is a public resource im working on for others who prefer to have a simpler configuration file. My goal is to attempt to get the config to look like the Bukkit API version of configs

Lol i made some progress xD

Hocon is not more complex. Hocon is simply different. Instead of indentation, it uses braces.

Meanwhile, a ConfigurationNode is nothing more than a node tree. The YamlConfigurationLoader is what determines what the resulting YAML looks like. You can use the DumperOptions to configure what resulting output should be. You want FlowStyle.BLOCK, since IIRC the default is FlowStyle.AUTO which chooses between braces and indentation based on complexity. Bukkit’s API uses FlowStyle.BLOCK.

Lol, its all opinion :smiley:

Anyways, I just wanted to let you know… you are a god among men… I have been trying to fix this issue for almost 2 weeks and just could not figure it out xD I posted on several other forums too with no hope. So thank you so much! <3