Set<String> companies = companiesFile.getConfig().getConfigurationSection("companies").getKeys(false);
sender.sendMessage("§2List of companies:");
for (String s : companies)
{
sender.sendMessage("§2" + companiesFile.getConfig().getString(s + ".name"));
}
Above is the code I have so far. I am coding a bukkit plugin and i am trying to figure out how to get the value "name" from all of the companies. Do you know how to get the value "name" from all the companies? Here is the config:
companies:
simpleco:
expenses: 3000
revenue: 6000
name: SimpleCo
projectempireco:
expenses: 5000
revenue: 5500
name: ProjectEmpireCo
To get the actual name value of each company you could first get all direct children keys in the companies
section (like you've already done) so that if you later add more top level sections to your config file you won't have to traverse those.
If you are certain that every company will have a name value assigned to it, you can then simply either use the direct path (now that we have the name of each company section) with something along the lines of companies.get(key + ".name")
, where key
is the name of the company section (for example simpleco
) and companies is the ConfigurationSection
for all the companies or you could create a new ConfigurationSection
one level deeper (one for each company) and retrieve the value for the key "name"
by calling getValues(false).get("name")
on that specific section. It would look something like this:
// Get config file, here called "fileConfig"
ConfigurationSection companies = fileConfig.getConfigurationSection("companies"); // The "companies" section of the config file
for (String companyKey : companies.getKeys(false)) { // For each company key in the set
String name = (String) companies.get(companyKey + ".name"); // Either use the path to retrieve name value
// OR
ConfigurationSection companySection = companies.getConfigurationSection(companyKey); // Create a new section one level deeper
name = (String) companySection.getValues(false).get("name"); // Retrieve name by getting value of key named "name"
}