【问题标题】:Bukkit / Spigotplugin - Remove entry from configBukkit / Spigotplugin - 从配置中删除条目
【发布时间】:2018-09-02 08:12:43
【问题描述】:

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

也不行。

我做错了什么?

【问题讨论】:

  • 我相信你得到了你想要的配置和实际的配置错误。他们没有什么不同

标签: java plugins config bukkit


【解决方案1】:

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

config.set(pathToRemove, null);

【讨论】:

    【解决方案2】:

    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();
        }
    }
    

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

    【讨论】:

    • 但是如果我想删除从文件加载时配置中已经准备好的东西怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多