Bukkit / Spigotplugin - 从配置中删除条目
Bukkit / Spigotplugin - Remove entry from config
我正在开发 Bukkit / Spigotplugin,它允许用户投票。
该插件使用配置。我想删除一个条目,但它不起作用。
我在 Google 上发现的内容:
getConfig().set("your.value", null);
我实现了什么:
int i = id;
while(getConfig().contains("ThingsYouCanVoteFor." + (i + 1)))
{
getConfig().set("ThingsYouCanVoteFor." + Integer.toString(i) + ".name", getConfig().getString("ThingsYouCanVoteFor." + Integer.toString(i + 1) + ".name"));
getConfig().set("ThingsYouCanVoteFor." + Integer.toString(i) + ".votes", getConfig().getInt("ThingsYouCanVoteFor." + Integer.toString(i + 1) + ".votes"));
i++;
}
getConfig().set("ThingsYouCanVoteFor." + Integer.toString(i + 1), null);
saveConfig();
sender.sendMessage("§aRemoved ID " + id);
配置的外观:
ThingsYouCanVoteFor:
0:
name: Build a bridge
votes: 0
1:
name: Kill the owner
votes: 2
2:
name: Vote for something other
votes: 1
3:
name: Remove all banned peoples inventory
votes: 0
4:
name: Teleport to others home
votes: 0
5:
name: Dig a hole in the air
votes: 0
6:
name: Shutdown the internet
votes: 0
PeopleWhoVoted:
- Gamingwelle
- BDevGWAdmin
- Sllikson
OpenForVoting: false
当我使用“/voteadmin remove 3”时应该是什么样子:
ThingsYouCanVoteFor:
0:
name: Build a bridge
votes: 0
1:
name: Kill the owner
votes: 2
2:
name: Vote for something other
votes: 1
3:
name: Teleport to others home
votes: 0
4:
name: Dig a hole in the air
votes: 0
5:
name: Shutdown the internet
votes: 0
PeopleWhoVoted:
- Gamingwelle
- BDevGWAdmin
- Sllikson
OpenForVoting: false
外观:
ThingsYouCanVoteFor:
0:
name: Build a bridge
votes: 0
1:
name: Kill the owner
votes: 2
2:
name: Vote for something other
votes: 1
3:
name: Teleport to others home
votes: 0
4:
name: Dig a hole in the air
votes: 0
5:
name: Shutdown the internet
votes: 0
6:
name: Shutdown the internet
votes: 0
PeopleWhoVoted:
- Gamingwelle
- BDevGWAdmin
- Sllikson
OpenForVoting: false
使用
getConfig().getConfigurationSection("ThingsYouCanVoteFor").getKeys(false).remove(i + 1)
也不行。
我做错了什么?
ConfigurationSection 的设置方法更改了您想要的值,但是在保存配置时它将覆盖所有当前值。这意味着如果你想保留它们,你必须 edit/specify 每个值,否则它们将会丢失。
为了只需要编辑单个值,我使用了一个数组。
数组是用来存放临时数据的,你好像需要一个包含String name
和int votes
的对象,我称这个对象为VoteData
.
服务器启动时,加载配置并用结果填充数组。这允许我们在运行时删除对象或修改数组,而无需触及配置文件,从而提高性能。
private void loadConfig() {
voteDataArray = new ArrayList<VoteData>();
YamlConfiguration config = YamlConfiguration.loadConfiguration(yourConfigFile);
ConfigurableSection section = config.getConfigurableSection("ThingsYouCanVoteFor");
if(section == null) {
Bukkit.getLogger().warn("ConfigSection ThingsYouCanVoteFor doesn't exist");
return;
}
for(String num : section.getKeys(false)) {
String name = section.getString(num + ".name");
int votes = section.getDouble(num + ".votes");
voteDataArray.add(new VoteData(name, votes));
}
}
当服务器关闭或您需要时,通过循环 config.getKeys(false)
并将节点设置为您的数据值来保存包含数组内容的配置。
private void saveConfig() {
YamlConfiguration config = new YamlConfiguration();
int num = 1;
for(VoteData data : voteDataArray) {
config.set("ThingsYouCanVoteFor." + num + ".name", data.getName());
config.set("ThingsYouCanVoteFor." + num + ".votes", data.getVotes());
num++;
}
try {
config.save(yourConfigFile);
} catch (IOException e) {
e.printStackTrace();
}
}
现在当你需要使用插件中的数据时,使用数组而不是配置。当你想删除一些东西时,只需从数组中删除即可。
您可以使用以下方法简单地删除路径中的值:
config.set(pathToRemove, null);
我正在开发 Bukkit / Spigotplugin,它允许用户投票。 该插件使用配置。我想删除一个条目,但它不起作用。 我在 Google 上发现的内容:
getConfig().set("your.value", null);
我实现了什么:
int i = id;
while(getConfig().contains("ThingsYouCanVoteFor." + (i + 1)))
{
getConfig().set("ThingsYouCanVoteFor." + Integer.toString(i) + ".name", getConfig().getString("ThingsYouCanVoteFor." + Integer.toString(i + 1) + ".name"));
getConfig().set("ThingsYouCanVoteFor." + Integer.toString(i) + ".votes", getConfig().getInt("ThingsYouCanVoteFor." + Integer.toString(i + 1) + ".votes"));
i++;
}
getConfig().set("ThingsYouCanVoteFor." + Integer.toString(i + 1), null);
saveConfig();
sender.sendMessage("§aRemoved ID " + id);
配置的外观:
ThingsYouCanVoteFor:
0:
name: Build a bridge
votes: 0
1:
name: Kill the owner
votes: 2
2:
name: Vote for something other
votes: 1
3:
name: Remove all banned peoples inventory
votes: 0
4:
name: Teleport to others home
votes: 0
5:
name: Dig a hole in the air
votes: 0
6:
name: Shutdown the internet
votes: 0
PeopleWhoVoted:
- Gamingwelle
- BDevGWAdmin
- Sllikson
OpenForVoting: false
当我使用“/voteadmin remove 3”时应该是什么样子:
ThingsYouCanVoteFor:
0:
name: Build a bridge
votes: 0
1:
name: Kill the owner
votes: 2
2:
name: Vote for something other
votes: 1
3:
name: Teleport to others home
votes: 0
4:
name: Dig a hole in the air
votes: 0
5:
name: Shutdown the internet
votes: 0
PeopleWhoVoted:
- Gamingwelle
- BDevGWAdmin
- Sllikson
OpenForVoting: false
外观:
ThingsYouCanVoteFor:
0:
name: Build a bridge
votes: 0
1:
name: Kill the owner
votes: 2
2:
name: Vote for something other
votes: 1
3:
name: Teleport to others home
votes: 0
4:
name: Dig a hole in the air
votes: 0
5:
name: Shutdown the internet
votes: 0
6:
name: Shutdown the internet
votes: 0
PeopleWhoVoted:
- Gamingwelle
- BDevGWAdmin
- Sllikson
OpenForVoting: false
使用
getConfig().getConfigurationSection("ThingsYouCanVoteFor").getKeys(false).remove(i + 1)
也不行。
我做错了什么?
ConfigurationSection 的设置方法更改了您想要的值,但是在保存配置时它将覆盖所有当前值。这意味着如果你想保留它们,你必须 edit/specify 每个值,否则它们将会丢失。 为了只需要编辑单个值,我使用了一个数组。
数组是用来存放临时数据的,你好像需要一个包含String name
和int votes
的对象,我称这个对象为VoteData
.
服务器启动时,加载配置并用结果填充数组。这允许我们在运行时删除对象或修改数组,而无需触及配置文件,从而提高性能。
private void loadConfig() {
voteDataArray = new ArrayList<VoteData>();
YamlConfiguration config = YamlConfiguration.loadConfiguration(yourConfigFile);
ConfigurableSection section = config.getConfigurableSection("ThingsYouCanVoteFor");
if(section == null) {
Bukkit.getLogger().warn("ConfigSection ThingsYouCanVoteFor doesn't exist");
return;
}
for(String num : section.getKeys(false)) {
String name = section.getString(num + ".name");
int votes = section.getDouble(num + ".votes");
voteDataArray.add(new VoteData(name, votes));
}
}
当服务器关闭或您需要时,通过循环 config.getKeys(false)
并将节点设置为您的数据值来保存包含数组内容的配置。
private void saveConfig() {
YamlConfiguration config = new YamlConfiguration();
int num = 1;
for(VoteData data : voteDataArray) {
config.set("ThingsYouCanVoteFor." + num + ".name", data.getName());
config.set("ThingsYouCanVoteFor." + num + ".votes", data.getVotes());
num++;
}
try {
config.save(yourConfigFile);
} catch (IOException e) {
e.printStackTrace();
}
}
现在当你需要使用插件中的数据时,使用数组而不是配置。当你想删除一些东西时,只需从数组中删除即可。
您可以使用以下方法简单地删除路径中的值:
config.set(pathToRemove, null);