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

时间:2018-09-02 08:12:44

标签: java plugins config bukkit

我正在开发一个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)

也不起作用。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您可以使用以下方法简单地删除路径中的值:

config.set(pathToRemove, null);

答案 1 :(得分:0)

ConfigurationSection的set方法可以根据需要更改值,但是在保存配置时它将覆盖所有当前值。这意味着如果要保留每个值,则必须对其进行编辑/指定,否则它们将丢失。 为了代替只需要编辑一个值,我使用了一个数组。

该数组用于保存临时数据,您似乎需要一个包含String nameint 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();
    }
}

现在,当您需要使用插件中的数据时,请使用数组而不是配置。而且,当您要删除某些内容时,只需从数组中删除即可。