从变量中获取变量列表

Getting a variable list from a variable

我正在使用 Bukkit 1.8 API。我已经制作了一个配置文件来编辑代码。我在配置中有一个等级列表,我正在循环使用这个

for(String ranks : plugin.getConfig().getStringList("selllallranks"))

配置中的等级列表如下所示

selllallranks:
- 'a'
- 'b'
- 'c'
- 'd'
- 'e'
- 'f'
# etc...

然后我继续检查存货,以某个价格出售的积木,可在配置中编辑。这是我拥有的其余代码

for(String sellallsell : plugin.getConfig().getStringList("sellall" + ranks))
{
    if(p.getInventory().contains(Material.valueOf(sellallsell)))
    {

    }
}

我尝试将其作为字符串列表进行循环。唯一的问题是我不是循环遍历字符串列表,而是循环变量列表。 Bukkit API 没有这个方法。 编辑时配置文件看起来像这样

sellalla:
    cobblestone: 10
    dirt: 1
    diamond_block: 1000

此处的每个变量都代表 Material 名称,后跟价格。

我的问题是,将配置中的这些变量作为列表获取。在此列表中,我想遍历它并检查它是否在玩家的清单中。在此之后,我想获得每个 material 的数量,并将其乘以定价。然后我会往玩家的账户里加钱。

我唯一需要修复的是 如何 从 sellalla: 变量中获取变量列表。我也想从中得到整数。

我建议使用 HashMap(您可以使用 HashMap.getEntry() 遍历它们)

这里有一些有用的链接:

Read a HashMap from Config

Write a HashMap to Config

如果你想获取路径中的键列表,你可以使用:

plugin.getConfig().getConfigurationSection("path").getKeys(false);

因此,例如,如果上述代码在此配置上是 运行:

path:
    key0: 55
    key1: 72
    key2: 8

您将获得包含值 key0key1key2List<String>。然后,要获取这些键的值,您可以简单地使用:

plugin().getConfig().get("path." + key);

getConfigurationSection(String) 选择 arg0 的配置部分(此方法仅用于获取 API 对象)

getKeys(false) 获取上一节中的所有键。使用 false 使它只获得第一个键,而不是下一个。例如,getKeys(true) 会 return key0subkey0key1subkey1,而 getKeys(false) 只会 return key0key1:

 path:
     key0:
         subkey0: 10
     key1:
         subkey1: 6

因此,您的代码可能如下所示:

for(String key : plugin.getConfig().getConfigurationSection("sellall")){
    Material material = Material.valueOf(key); //the material
    int value = plugin().getConfig().getInt("sellall." + key); //the sell price of the material

    //the rest of your code here
}