Map configuration not saving

Discussion in 'Plugin Development' started by sebasju1234, Jan 29, 2014.

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

    sebasju1234

    Hello guys,

    For some reason, my map configurations don't save. This is the code:
    Code:java
    1. package com.github.sebasju1234.kaboom.maps;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7.  
    8. import org.bukkit.configuration.InvalidConfigurationException;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11.  
    12. import com.github.sebasju1234.kaboom.core.Kaboom;
    13.  
    14. public class MapConfig {
    15.  
    16. private Kaboom plugin;
    17. private File mapFolder;
    18.  
    19. public MapConfig(Kaboom instance) {
    20. this.plugin = instance;
    21. initializeFiles();
    22. }
    23.  
    24. private void initializeFiles() {
    25. setMapFolder(new File(plugin.getDataFolder() + File.separator + "maps"));
    26.  
    27. if (!(mapFolder.exists())) { mapFolder.mkdirs(); }
    28. }
    29.  
    30. public File getMapFolder() {
    31. return mapFolder;
    32. }
    33.  
    34. public void setMapFolder(File mapFolder) {
    35. this.mapFolder = mapFolder;
    36. }
    37.  
    38. public FileConfiguration createMapFile(String mapName){
    39. File mapFile = new File(getMapFolder(), mapName + ".yml");
    40. FileConfiguration mapConfig = YamlConfiguration.loadConfiguration(mapFile);
    41.  
    42. if (!(mapFile.exists())) {
    43. try {
    44. mapFile.createNewFile();
    45. mapConfig.load(mapFile);
    46. } catch(IOException e){
    47. e.printStackTrace();
    48. } catch(InvalidConfigurationException e){
    49. e.printStackTrace();
    50. }
    51. }
    52.  
    53. setPaths(mapConfig);
    54. saveMapFile(mapName);
    55.  
    56. return mapConfig;
    57. }
    58.  
    59. public void setPaths(FileConfiguration config) {
    60. if (!(config.contains("spawnpoints"))) {
    61. List<String> spawns = new ArrayList<String>();
    62. config.set("spawnpoints", spawns);
    63. }
    64. }
    65.  
    66. public File getMapFile(String mapName){
    67. File mapFile = new File(getMapFolder(), mapName + ".yml");
    68.  
    69. if (mapFile.exists()){
    70. return mapFile;
    71. } else {
    72. return null;
    73. }
    74. }
    75.  
    76. public static FileConfiguration getMapConfig(File file){
    77. return YamlConfiguration.loadConfiguration(file);
    78. }
    79.  
    80. public void saveMapFile(String mapName) {
    81. try {
    82. getMapConfig(getMapFile(mapName)).save(getMapFile(mapName));
    83. } catch(IOException e) {
    84. e.printStackTrace();
    85. }
    86. }
    87.  
    88. }
    89.  


    There are no errors, it just doesn't save the paths (setPaths())

    Thanks in advance,
    Sebastiaan

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. Offline

    bigteddy98

    I think I know what's going wrong, the saveMapFile(); method saves the return from getMapFile(), but getMapFile(); does return a none excisting file.

    You can fix it like this:
    Code:java
    1. public static FileConfiguration getMapConfig(File file){
    2. if(file.excists(){
    3. return YamlConfiguration.loadConfiguration(file);
    4. }else{
    5. YamlConfiguration yaml = new YamlConfiguration();
    6. yaml.save(file);
    7. return yaml;
    8. //don't forget catching the exception
    9. }
    10. }

    Correct me if I'm wrong.

    - BigTeddy98, Sander.
     
  3. Offline

    sebasju1234

    Hi Sander,
    I've already fixed it, it was indeed the saveMapFile method. Anyways, thank you.
     
Thread Status:
Not open for further replies.

Share This Page