Bukkit / Spigotplugin - 从配置中删除条目

我正在开发一个 Bukkit / Spigotplugin,它允许用户对事物进行投票。该插件使用配置。我想删除一个条目,但它不起作用。我在谷歌上发现的:


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)

也不起作用。


我做错了什么?


拉莫斯之舞
浏览 256回答 2
2回答

德玛西亚99

您可以使用以下方法简单地删除路径中的值:config.set(pathToRemove, null);

慕神8447489

ConfigurationSection的 set 方法根据您的需要更改了值,但是在保存配置时,它将覆盖所有当前值。这意味着如果要保留它们,则必须编辑/指定每个值,否则它们将丢失。为了只需要编辑一个值,我使用了一个数组。该阵列是有保留的临时数据,你似乎需要一个物体保持String name和int votes,我调用这个对象VoteData。当服务器启动时,加载配置并用结果填充数组。这允许我们在运行时删除对象或修改数组,而无需触及配置文件,从而提高性能。private void loadConfig() {&nbsp; &nbsp; voteDataArray = new ArrayList<VoteData>();&nbsp; &nbsp; YamlConfiguration config = YamlConfiguration.loadConfiguration(yourConfigFile);&nbsp; &nbsp; ConfigurableSection section = config.getConfigurableSection("ThingsYouCanVoteFor");&nbsp; &nbsp; if(section == null) {&nbsp; &nbsp; &nbsp; &nbsp; Bukkit.getLogger().warn("ConfigSection ThingsYouCanVoteFor doesn't exist");&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; for(String num : section.getKeys(false)) {&nbsp; &nbsp; &nbsp; &nbsp; String name = section.getString(num + ".name");&nbsp; &nbsp; &nbsp; &nbsp; int votes = section.getDouble(num + ".votes");&nbsp; &nbsp; &nbsp; &nbsp; voteDataArray.add(new VoteData(name, votes));&nbsp; &nbsp; }}当服务器关闭或任何时候你想要的时候,通过循环config.getKeys(false)并将节点设置为你的数据值来保存包含数组内容的配置。private void saveConfig() {&nbsp; &nbsp; YamlConfiguration config = new YamlConfiguration();&nbsp; &nbsp; int num = 1;&nbsp; &nbsp; for(VoteData data : voteDataArray) {&nbsp; &nbsp; &nbsp; &nbsp; config.set("ThingsYouCanVoteFor." + num + ".name", data.getName());&nbsp; &nbsp; &nbsp; &nbsp; config.set("ThingsYouCanVoteFor." + num + ".votes", data.getVotes());&nbsp; &nbsp; &nbsp; &nbsp; num++;&nbsp; &nbsp; }&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; config.save(yourConfigFile);&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}现在当你需要使用插件中的数据时,使用数组而不是配置。当您想删除某些内容时,只需从数组中删除即可。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java