Reputation: 397
I have a bukkit config file that looks like this:
effected mobs:
skeleton:
lower hp limit: 0
upper hp limit: 0
zombie:
lower hp limit: 0
upper hp limit: 0
spider:
lower hp limit: 0
upper hp limit: 0
I'm trying to get a set that contains [skeleton, zombie, spider] and any others that may be added by giving it the key "effected mobs". I have looked at this similar question and tried the following code:
public class Main extends JavaPlugin implements Listener{
public FileConfiguration config;
public Set<String> mobsEffected;
public void onEnable(){
config = getConfig();
mobsEffected = config.getConfigurationSection("effected mobs").getKeys(false);
Bukkit.getLogger().info(String.valueOf(mobsEffected.size() ));
}
}
but the size is logged as 0, when it should be 3. Any advice?
Upvotes: 3
Views: 7862
Reputation: 51
your configuration section effected mobs
contains a space, where spaces aren't permitted in a configuration section name.
Personally, i use a hyphen (-
) where a space would be used.
Upvotes: 0
Reputation: 86
Try using the code below to make sure that the file is created before information is gotten from it. Make sure that your default config.yml is inside the plugin.jar file in the main directory. (Where to put it inside of your IDE really depends. I put it in the main plugin directory, but depending on how you compile the plugin, it might need to go in your src folder. (I use a Maven Project, not a Java Project in Eclipse.)
public class Main extends JavaPlugin implements Listener{
public FileConfiguration config;
public Set<String> mobsEffected;
public void onEnable(){
config = Bukkit.getConfig();
config.saveDefaultConfig();
mobsEffected = config.getConfigurationSection("effected mobs").getKeys(false);
Bukkit.getLogger().info(String.valueOf(mobsEffected.size()));
}
}
Upvotes: 0
Reputation: 105
So you actually want to get a Set of Strings based on your Configuration Entry. This should work:
public class Main extends JavaPlugin implements Listener {
public FileConfiguration config;
public Set<String> mobsEffected;
public void onEnable() {
// you have to initialize your set!
mobsEffected = new HashSet<String>();
// this creates the Config
File Cfgpath = new File("plugins/yourpluginnamehere");
File Cfg = new File("plugins/yourpluginnamehere/config.yml");
try {
Cfg.createNewFile();
} catch (IOException e) {
Cfgpath.mkdirs();
try {
Cfg.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}
config = YamlConfiguration.loadConfiguration(Cfg);
// Now it loops through all the Keys in the Configuration Section
// "effected mobs" and adds every key to your set!
for (String key : config.getConfigurationSection("effected mobs").getKeys(false)) {
mobsEffected.add(key);
}
Bukkit.getLogger().info(String.valueOf(mobsEffected.size()));
}
}
Please Make Sure that you replace yourpluginnamehere with the Name of your Plugin. You have to take the same String at both of the times where you have to replace it(Case Sensitive!).
Then it will automatically Create a Config Directory with a blank config.yml for you at first run.
At first run you will also get a NullPointerexception
because the ConfigurationSection doesn't exist yet. So just go to your plugin's Folder and insert your config content to the newly created config.yml.
Upvotes: 3
Reputation: 21
getConfig().getConfigurationSection("effected mobs").getKeys(false).toArray();
You should read the wiki more carefully xD
Upvotes: 0