Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accessing multiple YAML files

So, I have been fiddling around with this for the past 5 hours and I can’t seem to figure out why I can’t access my second yaml file. I’ve been searching google for answers, and still haven’t found anything even remotely similar to what I am looking for. Now, before you ask if I have registered it onto the onEnable event, I have. The config.yml file works perfectly fine, and as I need it to. However, when I create a second yaml file, "blocks.yml", I can’t access the functions, for example:
I need to get data into the blocks.yml file, but using plugin.data.getBlocks().contains(...) the .contains() part shows an error, and it when I delete the .contains() part, my only options after the plugin.data.getBlocks() part are the try and lambda events. I am honestly extremely confused with this. I’ve even tried it without the .data part of the plugin.data.getBlocks() line, and it just doesn’t show getBlocks() altogether. Here is my config manager file;


import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ConfigManager {

    private main plugin;
    private FileConfiguration config;
    private File configFile;

    private FileConfiguration blocks;
    private File blocksFile;

    public ConfigManager(main plugin) {
        this.plugin = plugin;
    }

    public void reloadConfig() {
        if (configFile == null) {
            configFile = new File(plugin.getDataFolder(), "config.yml");
        }
        config = YamlConfiguration.loadConfiguration(configFile);
        InputStream defaultStream = plugin.getResource("config.yml");
        if (defaultStream != null) {
            YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultStream));
            config.setDefaults(defaultConfig);
        }
    }

    public void getConfig() {
        if (config == null) {
            reloadConfig();
        }
    }
    public void saveConfig() {
        if(config == null || configFile == null) {
            return;
        }
        try {
            config.save(configFile);
        } catch (IOException e) {
            plugin.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not save config to " + this.configFile, e.toString());
        }
    }
    public void saveDefaultConfig() {
        if(configFile == null) {
            configFile = new File(plugin.getDataFolder(), "config.yml");
        }
        if(!configFile.exists()) {
            plugin.saveResource("plugin.yml", false);
        }
    }

    public void reloadBlocks() {
        if (blocksFile == null) {
            blocksFile = new File(plugin.getDataFolder(), "blocks.yml");
        }
        blocks = YamlConfiguration.loadConfiguration(blocksFile);
        InputStream defaultBlocksStream = plugin.getResource("blocks.yml");
        if (defaultBlocksStream != null) {
            YamlConfiguration defaultBlocks = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultBlocksStream));
            blocks.setDefaults(defaultBlocks);
        }
    }

    public void getBlocks() {
        if (blocks == null) {
            reloadBlocks();
        }
    }
    public void saveBlocks() {
        if(blocks == null || blocksFile == null) {
            return;
        }
        try {
            blocks.save(blocksFile);
        } catch (IOException e) {
            plugin.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not save blocks to " + this.blocksFile, e.toString());
        }
    }
    public void saveDefaultBlocks() {
        if(blocksFile == null) {
            blocksFile = new File(plugin.getDataFolder(), "blocks.yml");
        }
        if(!blocksFile.exists()) {
            plugin.saveResource("blocks.yml", false);
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There is multiple things to say.

Your method getBlocks() return "void", so nothing. You should do something like this :

public FileConfiguration getBlocks() {
    if (blocks == null) {
        reloadBlocks();
    }
    return blocks;
}

Also, the JavaPlugin’s class already implement saveConfig() & saveDefaultConfig() method. You don’t have to copy them yourself. Do like that :

plugin.saveDefaultConfig(); // copy if not found
plugin.getConfig().set("a", "b");
plugin.saveConfig(); // save config
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading