YamlConfiguration写入空文件

时间:2018-06-14 16:51:38

标签: java file bukkit

每次玩家加入一些默认值时,我都会创建一个自定义.yml文件。以下是我用来创建自定义配置的内容:

public class Config
{
    private String path;
    private YamlConfiguration config = null;
    private File configFile = null;
    private HashMap<String, Object> configDefaults;

    Config(String path)
    {
        this.path = path;
        Logger.log("Set Path");
        this.configDefaults = new HashMap<>();
        Logger.log("Set HasMap");
        this.getConfig();
        Logger.log("Set getConfig");
    }

    public void create()
    {
        Logger.log("Creating config");
        this.configFile = new File(SkyBlock.getInstance().getDataFolder() + File.separator + path.replaceAll("/", File.separator));
        if (!this.configFile.exists())
        {
            try
            {
                Logger.log("Trying");
                this.configFile.createNewFile();
                Logger.log("Created new File");
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        this.config = YamlConfiguration.loadConfiguration(this.configFile);
        Logger.log("Set this.config var");
    }

    public YamlConfiguration getConfig()
    {
        Logger.log("Start of getConfig");
        create();
        Logger.log("Trying create of config");
        Logger.log("Returning config");
        return this.config;
    }

    public void save()
    {
        create();
        Logger.log("Running create");
        try
        {
            this.config.save(this.configFile);
            Logger.log("Saved config");
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public void addDefault(String configPath, Object value)
    {
        Logger.log("Added default " + configPath + " With value of: " + value);
        this.configDefaults.put(configPath, value);
    }

    public void writeDefaults()
    {
        if (this.configDefaults.isEmpty())
        {
            Logger.log("It's empty");
            return;
        }
        for (String key : this.configDefaults.keySet())
        {
            config.set(key, this.configDefaults.get(key));
            Logger.log("Set the option: " + key + " | to value: " + this.configDefaults.get(key));
        }
        save();
    }

    public void createSection(String section)
    {
        this.config.createSection(section);
    }
}

它完美无缺。但是,当尝试写入它时,它会创建一个空白文件。代码I用来写:

    private void createFiles()
    {
        folder = new Folder("PlayerData");
    }

    public void addPlayer(Player player)
    {
        createFiles();
        Config config = new Config("PlayerData/" + player.getUniqueId() + ".yml");

        config.createSection("Messages");
        config.addDefault("Messages.Receive", true);
        config.createSection("ChatColor");
        config.addDefault("ChatColor.Color", ChatColor.WHITE);
        config.addDefault("ChatColor.Bold", false);
        config.addDefault("ChatColor.Strikethrough", false);
        config.addDefault("ChatColor.Underlined", false);
        config.writeDefaults();
    }

    public void setColor(Player player, ChatColor color)
    {
        addPlayer(player);
        Config config = new Config("PlayerData/" + player.getUniqueId() + ".yml");
        FileConfiguration conf = config.getConfig();
        conf.set("ChatColor.Color", color.name());
        config.save();
    }

    public ChatColor getColor(Player player)
    {
        addPlayer(player);
        Config config = new Config("PlayerData/" + player.getUniqueId() + ".yml");
        FileConfiguration conf = config.getConfig();
        return ChatColor.valueOf(conf.getString("ChatColor.Color"));
    }

我对控制台进行了一些调试:

[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet Path[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet HasMap[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mStart of getConfig[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mCreating config[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mTrying[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mCreated new File[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet this.config var[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mTrying create of config[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mReturning config[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet getConfig[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mAdded default Messages.Receive With value of: true[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mAdded default ChatColor.Color With value of: [0;37;1m[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mAdded default ChatColor.Bold With value of: false[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mAdded default ChatColor.Strikethrough With value of: false[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mAdded default ChatColor.Underlined With value of: false[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet the option: ChatColor.Strikethrough | to value: false[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet the option: ChatColor.Bold | to value: false[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet the option: ChatColor.Color | to value: [0;37;1m[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet the option: Messages.Receive | to value: true[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet the option: ChatColor.Underlined | to value: false[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mCreating config[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSet this.config var[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mRunning create[m
[07:25:19] [Server thread/INFO]: [0;33;22m[21m[SB] -- [0;33;1mSaved config[m

1 个答案:

答案 0 :(得分:2)

问题是由于create()函数的这一行引起的。

this.config = YamlConfiguration.loadConfiguration(this.configFile);

调用save()函数时,该函数将调用create()函数。结果,this.config(包含您刚刚放入的所有内容)被新的YamlConfiguration覆盖,其中没有任何数据。然后,将此空的YamlConfiguration写入文件,从而得到一个空文件。

我的解决方案是在您的save()函数中更改以下内容: 将create();替换为手动检查以查看文件是否存在:

this.configFile = new File(SkyBlock.getInstance().getDataFolder() + File.separator + path.replaceAll("/", File.separator));
    if (!this.configFile.exists()) {
        try {
            this.configFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这样,this.config将不会被覆盖。