【问题标题】:Minecraft plugin config.yml sections doesn't saveMinecraft 插件 config.yml 部分不保存
【发布时间】:2021-04-06 03:35:12
【问题描述】:

我正在编写一个基于 spigot 编码的 Minecraft 插件。 但是我有以下问题,我正在尝试通过插件编辑配置文件,但是当我尝试存储信息时,没有存储数据。例如,当我将 nameArchive 更改为 Archive Stats 并重新加载服务器时。然后配置name返回Archive

Archive:
  Material:
    ==: org.bukkit.inventory.ItemStack
    v: 2586
    type: BOOKSHELF
  ArchiveAtStart: false
  Name: Archive

当我将名称更改为“存档统计”时

Archive:
  Material:
    ==: org.bukkit.inventory.ItemStack
    v: 2586
    type: BOOKSHELF
  ArchiveAtStart: false
  Name: Archive Stats

并重新加载服务器,它仍然变回“存档”

Archive:
  Material:
    ==: org.bukkit.inventory.ItemStack
    v: 2586
    type: BOOKSHELF
  ArchiveAtStart: false
  Name: Archive

所以这里有 2 个代码要显示:

package com.Fender.Bedrock.files;

import com.Fender.Bedrock.Bedrock;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CustomConfig
{

    private static Bedrock plugin;
    public static File file;
    private static FileConfiguration customFile;

    public static void setup(Bedrock plugin1)
    {

        plugin = plugin1;

        file = new File(plugin.getDataFolder(), "config.yml");


        if(!file.exists())
        {
            try {
                file.createNewFile();
            }catch (IOException e){

            }
        }

        customFile = new YamlConfiguration();
        save();


        try{customFile.load(file);}
        catch (IOException | InvalidConfigurationException e)
        {
            e.printStackTrace();
        }

    }

    public static FileConfiguration get()
    {

        return customFile;
    }


    public static void save()
    {
        try
        {
            customFile.save(file);
        }catch (IOException e)
        {
            System.out.print("Couldn't save file!");
        }
    }

    public static void load()
    {
        try
        {
            try {
                try {
                    customFile.load(file);
                }catch (InvalidConfigurationException i){
                    System.out.print("Couldn't load file!");
                }
            }catch (FileNotFoundException f)
            {
                System.out.print("Couldn't load file!");
            }
        }catch (IOException e)
        {
            System.out.print("Couldn't load file!");
        }
    }

    public static void reload()
    {
        customFile = YamlConfiguration.loadConfiguration(file);
    }

    public boolean exists()
    {
        if(file.exists()){
            return true;
        }else
            {
                return false;
            }
    }
}

  1. 主类的方法
public void FileCreate()
    {
        CustomConfig.setup(this);
        CustomConfig.reload();
        {
            CustomConfig.get().addDefault("Name1", "§dHis");
            CustomConfig.get().addDefault("Place", 1);

            CustomConfig.get().createSection("Archive");
            CustomConfig.get().getConfigurationSection("Archive").addDefault("Material", itemStack);
            CustomConfig.get().getConfigurationSection("Archive").addDefault("ArchiveAtStart", false);
            CustomConfig.get().getConfigurationSection("Archive").addDefault("Name", "Archive");

            CustomConfig.get().createSection("noob").addDefault("help", false);
        }
        CustomConfig.get().options().copyHeader(true);
        CustomConfig.get().options().copyDefaults(true);
        CustomConfig.save();
        //saveResource("config.yml", false);
        this.saveDefaultConfig();
    }

最后,这是我的插件主要方法:

@Override
public void onEnable() {
     FileCreate();
     this.getConfig().options().copyDefaults(true);
     this.saveConfig();
}

【问题讨论】:

  • 您是否在插件关闭时保存配置(即从存储在内存中的配置)?这可能会覆盖您在重新启动时所做的更改。还有一种更简单的方法来获取主配置(config.yml),它也将处理加载默认值,在你的情况下使用JavaPlugin.#getConfig(),即plugin.getConfig()
  • 好的,你的第一个问题,这与问题有什么关系吗? @Override public void onEnable() { FileCreate(); this.getConfig().options().copyDefaults(true); this.saveConfig(); } @Override public void onDisable() { getServer().getConsoleSender().sendMessage(ChatColor.GRAY + "[" + getName() + "] " + ChatColor.RED + "Plugin is disabled"); }这是启用和禁用方法
  • 啊,不,看起来不错。不过,对于未来的读者,我会将其添加到问题详细信息中。可能也值得在 bukkit/spigot 论坛上转发。

标签: java plugins server minecraft


【解决方案1】:

出现此问题是因为您可以在启动时覆盖行。更准确地说,在添加默认行时。

这只会添加一个默认值,但不会覆盖:

CustomConfig.get().addDefault("Name1", "§dHis");

这改变了一切:

CustomConfig.get().createSection("Archive");

您应该继续使用addDefault()。对于部分,只需使用addDefault("mysection.key", "myval"),因此,在您的情况下:

CustomConfig.get().addDefault("Archive.Name", "Archive");

【讨论】:

  • 是的,我迟到了,但它永远不会太旧! :)
猜你喜欢
  • 2022-08-06
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多