从变量获取变量列表

时间:2022-06-01 18:05:41

I am using the Bukkit 1.8 API. I have made a configuration file to edit the code. I have a list of ranks within the config, which I am looping through with this

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

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

The list of ranks within the config looks like this

配置中的排名列表如下所示

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

Then I head on over to checking the inventory with the blocks to sell at a price, editable within the config. Here is the rest of the code that I have

然后我继续检查库存,然后以价格出售,在配置中可编辑。这是我的其余代码

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

    }
}

I tried looping through it as a string list. The only problem is that I am not looping through a string list, but a variable list. Bukkit API does not have a method for this. The configuration file would look something like this when editting

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

sellalla:
    cobblestone: 10
    dirt: 1
    diamond_block: 1000

Each one of the variables under here would represent the Material name, followed along with the price of it.

这里的每个变量都代表材料名称,后面跟着它的价格。

What my problem is, is getting these variables within the config as a list. In this list I would want to loop through it and check if it is in the player's inventory. After this I would want to get the amount of each material, and multiply it by the pricing. Then I would add money into the player's account.

我的问题是,在配置中将这些变量作为列表。在这个列表中,我想循环浏览它并检查它是否在播放器的库存中。在此之后,我想获得每种材料的数量,并将其乘以定价。然后我会在玩家的账户中加钱。

The only thing I would need for fixing would be how to get the variable list from the sellalla: variable. I would also want to get the integers from this.

我需要修复的唯一方法是如何从sellalla:variable获取变量列表。我也希望得到这个整数。

2 个解决方案

#1


If you want to get the list of keys in a path, you could use:

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

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

So, for example, if the above code was run on this configuration:

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

path:
    key0: 55
    key1: 72
    key2: 8

You would get the List<String> containing the values key0, key1, and key2. Then, to get the values for those keys, you could simply use:

您将获得包含值key0,key1和key2的List 。然后,要获取这些键的值,您可以简单地使用:

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

getConfigurationSection(String) selects the section of the config at arg0 (this method is just used to get an API Object)

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

getKeys(false) gets all of the keys in the above section. Using false makes it only get the first keys, and not the next ones. For example, getKeys(true) would return key0, subkey0, key1, and subkey1, whereas getKeys(false) would only return key0 and key1:

getKeys(false)获取上一节中的所有键。使用false使它只获得第一个键,而不是下一个键。例如,getKeys(true)将返回key0,subkey0,key1和subkey1,而getKeys(false)只返回key0和key1:

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

So, your code could look something like this:

所以,你的代码看起来像这样:

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
}

#2


I'd suggest using a HashMap ( you can iterate through them using HashMap.getEntry() )

我建议使用HashMap(你可以使用HashMap.getEntry()迭代它们)

Here are a few useful links:

以下是一些有用的链接:

Read a HashMap from Config

从Config中读取HashMap

Write a HashMap to Config

写一个HashMap到Config

#1


If you want to get the list of keys in a path, you could use:

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

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

So, for example, if the above code was run on this configuration:

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

path:
    key0: 55
    key1: 72
    key2: 8

You would get the List<String> containing the values key0, key1, and key2. Then, to get the values for those keys, you could simply use:

您将获得包含值key0,key1和key2的List 。然后,要获取这些键的值,您可以简单地使用:

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

getConfigurationSection(String) selects the section of the config at arg0 (this method is just used to get an API Object)

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

getKeys(false) gets all of the keys in the above section. Using false makes it only get the first keys, and not the next ones. For example, getKeys(true) would return key0, subkey0, key1, and subkey1, whereas getKeys(false) would only return key0 and key1:

getKeys(false)获取上一节中的所有键。使用false使它只获得第一个键,而不是下一个键。例如,getKeys(true)将返回key0,subkey0,key1和subkey1,而getKeys(false)只返回key0和key1:

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

So, your code could look something like this:

所以,你的代码看起来像这样:

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
}

#2


I'd suggest using a HashMap ( you can iterate through them using HashMap.getEntry() )

我建议使用HashMap(你可以使用HashMap.getEntry()迭代它们)

Here are a few useful links:

以下是一些有用的链接:

Read a HashMap from Config

从Config中读取HashMap

Write a HashMap to Config

写一个HashMap到Config