Home > Software design >  Accessing multiple YAML files
Accessing multiple YAML files

Time:12-04

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);
        }
    }
}

CodePudding user response:

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

CodePudding user response:

To be frunk, the description of the problem doesn't help so much to help you.

Looking to your design pattern I found a difference between the methods saveDefaultConfig and saveDefaultBlocks.

Looking to this methods I could suggest your last command plugin.saveResource("blocks.yml", false); to be changed to plugin.saveResource("plugin.yml", false);.

Additionally, method contains is not inside the code snippet you added in the question. Try to search inside the files of your project the word contains, because probably the error is related to this command. Probably you use this command on an object that doesn't contain such a method.

Update

Change the method getBlocks to,

public YamlConfiguration getBlocks() {
    if (blocks == null) {
        reloadBlocks();
    }
    return blocks;
}
  • Related