如何在不删除注释的情况下保存 Bukkit 的默认配置?
How would I save Bukkit's default config without removing the notes?
我正在使用 Bukkit API 1.7.9,我遇到了一个问题。
我正在创建一个带有 HashMap
的经济系统,所以显然为了不在每次服务器重新启动时都重置经济系统,我需要将其存储在一个文件中。但是,如果我将它存储在默认配置中,我无法在不删除 #notes
.
的情况下保存它
这是我用于 save/load 经济系统的代码 HashMap
:
public static final void saveTokensAmount()
{
for(String playerName : getTokensMap().keySet())
{
main.getConfig().set("tokens." + playerName, getTokensMap().get(playerName));
}
main.saveConfig();
}
public static final void loadTokensAmount()
{
if(!main.getConfig().contains("tokens")) return;
for(String s : main.getConfig().getConfigurationSection("tokens").getKeys(false))
{
setTokensBalance(s, main.getConfig().getInt("tokens." + s));
}
}
这很好用,但是 main.saveConfig();
删除了 #notes
。
我知道 saveDefaultConfig();
保存笔记,但我不能在这里这样做,因为用户可能已经编辑了我放入其中的其他变量。
我试过重新加载配置reloadConfig();
认为它会重新加载它保存这个,但它没有。
我的问题:如何在不删除 #notes
的情况下保存 Bukkit 的默认配置?
你可能认为这个问题是重复的,但通常的答案是saveDefaultConfig();
,我在这里做不到。
我认为在这种情况下你唯一的选择是创建一个自定义 YAML 文件来存储数据(如果你想以 YAML 格式存储这种数据),我认为你应该这样做无论如何。
我认为这种播放器数据不应该保存在配置文件中。顾名思义,配置文件用于插件的初始设置或规则,允许用户更好地控制插件的行为方式。许多较新的开发人员使用配置文件来保存他们想要的任何东西,因为这是他们知道如何做的唯一方法(Bukkit API 使它变得如此容易)and/or 因为它是将信息保存到磁盘的第一种方法。
下面是一些代码,可帮助您开始创建用于存储玩家代币数量的自定义 YAML 文件。您还应该使用玩家的唯一 ID 而不是他们的名字来存储他们的代币数量,因为名字将来可能会改变。
//Assuming you have a HashMap somewhere that stores the values
HashMap<String, Integer> tokens = new HashMap<String, Integer>();
//Note that the string is not the player name, but the UUID of the player (as a String)
我会在您的 onEnable() 方法中创建 tokens.yml 文件,并创建一个方法以便于访问该文件。这是 saveTokensAmount() 方法(已测试,似乎有效)。
void saveTokensAmount() {
File tokenFile = getTokenFile(); //Get the file (make sure it exists)
FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(tokenFile); //Load configuration
for (String id : tokens.keySet()) {
fileConfig.createSection(id); //Create a section
fileConfig.set(id, tokens.get(id)); //Set the value
}
try {
fileConfig.save(tokenFile); //Save the file
} catch (IOException ex) {
ex.printStackTrace();
//Handle error
}
}
//Not sure if creating new sections is the most efficient way of storing this data in a YAML file
这是 loadTokensAmount() 方法:
void loadTokensAmount() {
File tokenFile = getTokenFile(); //Make sure it exists
FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(tokenFile); //Load configuration
try {
fileConfig.load(tokenFile); //Load contents of file
for (String id : fileConfig.getKeys(false)) { //Get the keys
tokens.put(id, fileConfig.getInt(id)); //Add values to map
}
} catch (Exception ex) {
ex.printStackTrace();;
}
}
输入玩家的初始信息,例如加入服务器时(您也可以写入文件):
tokens.put(player.getUniqueId().toString(), amount);
最终这个 list/file 可能会变得太大,以至于您可能想要使用更好的数据库。您可能还希望仅针对当前在线的玩家在 map/memory 中存储代币数量。希望这对您有所帮助!
我正在使用 Bukkit API 1.7.9,我遇到了一个问题。
我正在创建一个带有 HashMap
的经济系统,所以显然为了不在每次服务器重新启动时都重置经济系统,我需要将其存储在一个文件中。但是,如果我将它存储在默认配置中,我无法在不删除 #notes
.
这是我用于 save/load 经济系统的代码 HashMap
:
public static final void saveTokensAmount()
{
for(String playerName : getTokensMap().keySet())
{
main.getConfig().set("tokens." + playerName, getTokensMap().get(playerName));
}
main.saveConfig();
}
public static final void loadTokensAmount()
{
if(!main.getConfig().contains("tokens")) return;
for(String s : main.getConfig().getConfigurationSection("tokens").getKeys(false))
{
setTokensBalance(s, main.getConfig().getInt("tokens." + s));
}
}
这很好用,但是 main.saveConfig();
删除了 #notes
。
我知道 saveDefaultConfig();
保存笔记,但我不能在这里这样做,因为用户可能已经编辑了我放入其中的其他变量。
我试过重新加载配置reloadConfig();
认为它会重新加载它保存这个,但它没有。
我的问题:如何在不删除 #notes
的情况下保存 Bukkit 的默认配置?
你可能认为这个问题是重复的,但通常的答案是saveDefaultConfig();
,我在这里做不到。
我认为在这种情况下你唯一的选择是创建一个自定义 YAML 文件来存储数据(如果你想以 YAML 格式存储这种数据),我认为你应该这样做无论如何。
我认为这种播放器数据不应该保存在配置文件中。顾名思义,配置文件用于插件的初始设置或规则,允许用户更好地控制插件的行为方式。许多较新的开发人员使用配置文件来保存他们想要的任何东西,因为这是他们知道如何做的唯一方法(Bukkit API 使它变得如此容易)and/or 因为它是将信息保存到磁盘的第一种方法。
下面是一些代码,可帮助您开始创建用于存储玩家代币数量的自定义 YAML 文件。您还应该使用玩家的唯一 ID 而不是他们的名字来存储他们的代币数量,因为名字将来可能会改变。
//Assuming you have a HashMap somewhere that stores the values
HashMap<String, Integer> tokens = new HashMap<String, Integer>();
//Note that the string is not the player name, but the UUID of the player (as a String)
我会在您的 onEnable() 方法中创建 tokens.yml 文件,并创建一个方法以便于访问该文件。这是 saveTokensAmount() 方法(已测试,似乎有效)。
void saveTokensAmount() {
File tokenFile = getTokenFile(); //Get the file (make sure it exists)
FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(tokenFile); //Load configuration
for (String id : tokens.keySet()) {
fileConfig.createSection(id); //Create a section
fileConfig.set(id, tokens.get(id)); //Set the value
}
try {
fileConfig.save(tokenFile); //Save the file
} catch (IOException ex) {
ex.printStackTrace();
//Handle error
}
}
//Not sure if creating new sections is the most efficient way of storing this data in a YAML file
这是 loadTokensAmount() 方法:
void loadTokensAmount() {
File tokenFile = getTokenFile(); //Make sure it exists
FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(tokenFile); //Load configuration
try {
fileConfig.load(tokenFile); //Load contents of file
for (String id : fileConfig.getKeys(false)) { //Get the keys
tokens.put(id, fileConfig.getInt(id)); //Add values to map
}
} catch (Exception ex) {
ex.printStackTrace();;
}
}
输入玩家的初始信息,例如加入服务器时(您也可以写入文件):
tokens.put(player.getUniqueId().toString(), amount);
最终这个 list/file 可能会变得太大,以至于您可能想要使用更好的数据库。您可能还希望仅针对当前在线的玩家在 map/memory 中存储代币数量。希望这对您有所帮助!