Why won't my config.yml load the data it should?

Discussion in 'Plugin Development' started by PluginStudios, Mar 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    PluginStudios

    Hey bukkit forums. A One in the Chamber plugin I am making does not load it's config on enable. I don't know why, and because of this when I create an arena it does not save. I have the copy defaults but it won't work. Help me??
    Main.java http://www.pastebin.com/SAWAtN88
    Config.yml: http://www.pastebin.com/U0831Bq7
     
  2. Offline

    Sharkfang17

    Try, instead of
    1. getConfig().options().copyDefaults();
    2. saveConfig();
    , do saveDefaultConfig();
     
  3. Offline

    PluginStudios

    Sharkfang17 that didn't work. Still does not save.
     
  4. Offline

    Sharkfang17

    Sorry then :/ I was having problems with a config in one of my plugins and I changed it to that and it fixed it, thought it might work for you.
     
  5. Offline

    PluginStudios

    @Sharlfang17 thank you for trying :) Does anyone else have some suggestions?
     
  6. Offline

    Mattkx4

    Well I have a more advanced way to do configuration files so here goes nothing :)

    Make a new class and call it something like LocConfig
    Put this inside it:
    Code:java
    1. public class LocConfig {
    2.  
    3. <MainClassFile> main;
    4. public static FileConfiguration configFile;
    5. private static File fConfig;
    6.  
    7. public LocConfig(<MainClassFile> main) {
    8.  
    9. this.main = main;
    10. fConfig = new File(main.getDataFolder(), "LocConfig.yml");
    11. configFile = YamlConfiguration.loadConfiguration(fConfig);
    12. try {
    13. configFile.save(fConfig);
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. }
    17. }
    18.  
    19. public static void saveSpawn(String name, String world, int x, int y, int z) {
    20. configFile.set("Spawns." + name, name);
    21. configFile.set("Spawns." + name + ".world", world.toString());
    22. configFile.set("Spawns." + name + ".x", x);
    23. configFile.set("Spawns." + name + ".y", y);
    24. configFile.set("Spawns." + name + ".z", z);
    25.  
    26. try {
    27. configFile.save(fConfig);
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. }
    31. }
    32.  
    33. public static Location getSpawn(String name) {
    34. String world = configFile.getString("Spawns." + name + ".world");
    35. int x = configFile.getInt("Spawns." + name + ".x");
    36. int y = configFile.getInt("Spawns." + name + ".y");
    37. int z = configFile.getInt("Spawns." + name + ".z");
    38. Location loc = new Location(Bukkit.getWorld(world), x, y, z);
    39. return loc;
    40. }
    41. }


    Let me explain this step-by-step:

    Code:java
    1. <MainClassFile> main;
    2. public static FileConfiguration configFile;
    3. private static File fConfig;

    That's just variable declaration, don't worry about that.

    Code:java
    1. public LocConfig(PacManMain main) {
    2.  
    3. this.main = main;
    4. fConfig = new File(main.getDataFolder(), "LocConfig.yml");
    5. configFile = YamlConfiguration.loadConfiguration(fConfig);
    6. try {
    7. configFile.save(fConfig);
    8. } catch (IOException e) {
    9. e.printStackTrace();
    10. }
    11. }

    This constructor pretty much creates the class.
    And because this is a constructor, in your Main Class, put the following code in the onEnable() Method:
    Code:java
    1. new LocConfig(this);



    Code:java
    1. public static void saveSpawn(String name, String world, int x, int y, int z) {
    2. configFile.set("Spawns." + name, name);
    3. configFile.set("Spawns." + name + ".world", world.toString());
    4. configFile.set("Spawns." + name + ".x", x);
    5. configFile.set("Spawns." + name + ".y", y);
    6. configFile.set("Spawns." + name + ".z", z);
    7.  
    8. try {
    9. configFile.save(fConfig);
    10. } catch (IOException e) {
    11. e.printStackTrace();
    12. }
    13. }

    This code is a method that can be called to save a spawn location! Fairly straight forward if I do say so myself.

    Code:java
    1. public static Location getSpawn(String name) {
    2. String world = configFile.getString("Spawns." + name + ".world");
    3. int x = configFile.getInt("Spawns." + name + ".x");
    4. int y = configFile.getInt("Spawns." + name + ".y");
    5. int z = configFile.getInt("Spawns." + name + ".z");
    6. Location loc = new Location(Bukkit.getWorld(world), x, y, z);
    7. return loc;
    8. }

    This method gets a spawn location. So if I have a spawn point saved as "Arena" I would say:
    Code:java
    1. Location arenaLoc = LocConfig.getSpawn("Arena"):

    This code is assuming you are trying to get the spawn from another class file ;)

    I hoped this helped you out! If you have any questions just ask!
     
    Sharkfang17 likes this.
  7. Offline

    AoH_Ruthless

    PluginStudios
    It doesn't work because you are checking if a string list is null...

    You need to be doing:

    Code:java
    1. if (getConfig().getStringList("LIST").isEmpty()) {
    Code:java
    1.  
    2. [S] // CODE![/S]
    3. [S]}[/S]


    If you don't believe me, use that same null check on say, a configuration section and it will load the thing you are trying to set.

    You also have to call getConfig().options().copyDefaults() before trying to set anything. I recommend using saveDefaultConfig() but each works.

    Edit: The reason it isn't returning null is because the string list isn't defined.

    So you would want to do something like this:

    Code:java
    1. List<String> config = getConfig().getStringList("list");
    2. if (config == null || config.isEmpty()) {
    3. //code
    4. }
     
  8. Offline

    PluginStudios

    AoH_Ruthless I dont understand you completely. Can somebody explain why that helps?
     
  9. Offline

    AoH_Ruthless

    PluginStudios
    An undefined string cant return null. Look it up
     
    Mattkx4 likes this.
Thread Status:
Not open for further replies.

Share This Page