【发布时间】:2021-04-06 03:35:12
【问题描述】:
我正在编写一个基于 spigot 编码的 Minecraft 插件。
但是我有以下问题,我正在尝试通过插件编辑配置文件,但是当我尝试存储信息时,没有存储数据。例如,当我将 name 从 Archive 更改为 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;
}
}
}
- 主类的方法
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