I need help to adapt a code from Bukkit

Hi, I’m trying to adapt this Bukkit code for Sponge and I can’t get it to work.

private void validateConfig() {
        InputStream is = getResource("config.yml");

        FileConfiguration configuration = YamlConfiguration.loadConfiguration(new InputStreamReader(is));

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy_HH-mm-ss");

        Set<String> pluginKeys = configuration.getKeys(true);
        Set<String> configKeys = getConfig().getKeys(true);

        for (String s : pluginKeys) {
            if (!configKeys.contains(s)) {
                System.out.println("You are using an invalid config. Creating a new one...");

                File backupFolder = new File("plugins/Core/backup");
                if (!backupFolder.exists()) {
                    backupFolder.mkdir();
                }

                Path source = (Path) Paths.get("plugins/Core/config.yml");
                Path dest = (Path) Paths.get("plugins/Core/backup/config_" + format.format(date) + ".yml");
                File source2 = new File("plugins/Core/config.yml");

                try {
                    Files.copy(source, dest);
                    Bukkit.getConsoleSender().sendMessage("Generating a backup: \\plugins\\Core\\backup\\config_" + format.format(date) + ".yml");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Bukkit.getConsoleSender().sendMessage("Deleting file: \\plugins\\Core\\config.yml");
                source2.delete();

                registerConfig();

                return;
            }
        }
    }

It’s a method that checks the YAML configuration for errors and missing things. But I don’t know how to adapt it to HOCON.

Thanks in advance

Sure, more then happy to help, before we get started, are you wanting to adapt it to API 7 (MC 1.12.2) or API 8 (1.16.4) or the bleeding edge of API 9/10(MC 1.18.2 and 1.19)?

Also are you willing to update your code to at least Java 8? (``Date as been deprecated in Java since Java 8)

If you dont mind reading documentation, a great place to get started is Creating a Plugin — Sponge 8.0.0 documentation (The Sponge docs), but if your more of a JavaDocs sort of person then https://jd.spongepowered.org/

Edit: Just read the bottom of your post

The config loader is slightly different between API 7 and 8, but its generally the same. Here is the offical wiki Home · SpongePowered/Configurate Wiki · GitHub

But here is some TLDR code snippets

Loading from a file

File file;
ConfigurationNode root = HoconConfigurationLoader.builder().file(file).load();

If your not a fan of Hocon, you can also do JSON JacksonConfigurationLoader and YAML YamlConfigurationLoader

Getting to a node

In Bukkit, you would do

config.setInt("path.to.node", 1);

In Sponge Configuration, it doesnt use the magic value of ., but instead you go to the node using a array (it accepts it as a var array) and then set/get the value

rootNode.node("path", "to", "node").set(1);

You can also do it like

rootNode.node("path").node("to").node("node").set(1);

if you prefer

Getting all keys for a node

I saw in your code you have getConfig().getKeys(true). I dont belive there is a option for nested keys in Sponge (maybe there is, never looked into it), so I thought I would show you how to get all children keys for a node

Collection<Object> keys = rootNode.node("path", "to", "node").childrenMap().keys()

Note that its a Object and not a string, this is because the key can be anything that the configuration loader accepts, while most are strings, if you have a array, then the key would be a integer

Modern date

Nothing to do with configuration, but it bothers me that something depricated almost a decade ago (8 years ago) is still being used today.

Please use LocalDate, LocalTime and/or LocalDateTime depending on what you need

Hi thanks for the answer, it is helping me to guide me in the code adaptation.

But I have a question, how can I get the config keys from inside the plugin? I mean, not read the config from the folder but from inside the plugin.

So I can compare the configuration from the folder with the one inside the plugin. I don’t know if I make myself clear.

You mean compare a key (node key?) of a config to a hard coded key? Or a config value inside your jar?

Or do you just mean default values? Aka if the config value isn’t there it uses another value?