Settings Manager not working right

Discussion in 'Plugin Development' started by JD_Guy17, Mar 19, 2014.

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

    JD_Guy17

    My settings manager is not working properly as when i insert something it adds it to all 3 file, could some one help please :).​
    SettingsManager:​
    Code:java
    1. private SettingsManager() { }
    2.  
    3. static SettingsManager instance = new SettingsManager();
    4.  
    5. public static SettingsManager getInstance() {
    6. return instance;
    7. }
    8.  
    9. Plugin p;
    10. FileConfiguration config;
    11. File cfile;
    12.  
    13. FileConfiguration gangs;
    14. File gfile;
    15.  
    16. FileConfiguration players;
    17. File pfile;
    18.  
    19. public void setup(Plugin p) {
    20. config = p.getConfig();
    21. config.options().copyDefaults(true);
    22. cfile = new File(p.getDataFolder(), "config.yml");
    23.  
    24. gangs = p.getConfig();
    25. gangs.options().copyDefaults(true);
    26. gfile = new File(p.getDataFolder(), "gangs.yml");
    27.  
    28. players = p.getConfig();
    29. players.options().copyDefaults(true);
    30. pfile = new File(p.getDataFolder(), "players.yml");
    31. saveConfig();
    32. }
    33.  
    34. public FileConfiguration getConfig() {
    35. return config;
    36. }
    37. public FileConfiguration getGangs() {
    38. return gangs;
    39. }
    40. public FileConfiguration getPlayers() { return players; }
    41.  
    42. public void saveConfig() {
    43. try {
    44. config.save(cfile);
    45. gangs.save(gfile);
    46. players.save(pfile);
    47. } catch (IOException e) {
    48. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Couldn't save config.yml");
    49. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Couldn't save gangs.yml");
    50. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Couldn't save players.yml");
    51. }
    52. }


    And here is how i am inserting it:
    Code:java
    1. SettingsManager.getInstance().getGangs().createSection(gangName);
    2. SettingsManager.getInstance().getGangs().set(gangName + ".owner", p.getName());
    3. SettingsManager.getInstance().saveConfig();


    Thanks in advance,
    - JD
     
  2. Offline

    AtomSponge

    JD_Guy17 Look at your setup-Method. JavaPlugin.getConfig() will always return the same FileConfiguration, so your three variables point to the same object.
     
  3. Offline

    JD_Guy17

    AtomSponge When I try to change it doesn't work. Shouldn't this work?
    Code:java
    1. public FileConfiguration getGangs() {
    2. return gangs;
    3. }
     
  4. Offline

    AtomSponge

    JD_Guy17 I ment those three lines:
    Code:java
    1. config = p.getConfig();
    2. gangs = p.getConfig();
    3. players = p.getConfig();


    Take a look at this.
     
  5. Offline

    JD_Guy17

    AtomSponge Still having issues with it :/.
    I have no idea whats wrong :(
    PHP:
    private SettingsManager() { }
     
        static 
    SettingsManager instance = new SettingsManager();
     
        public static 
    SettingsManager getInstance() {
            return 
    instance;
        }
     
        
    Plugin p;
        
    FileConfiguration config;
        
    File cfile;
     
        
    FileConfiguration players;
        
    File pfile;
     
        
    FileConfiguration gangs;
        
    File gfile;
     
        public 
    void setup(Plugin p) {
            
    config p.getConfig();
            
    config.options().copyDefaults(true);
            
    cfile = new File(p.getDataFolder(), "config.yml");
     
            
    players YamlConfiguration.loadConfiguration(pfile);
            
    pfile = new File(p.getDataFolder(), "player.yml");
     
            
    gangs YamlConfiguration.loadConfiguration(gfile);
            
    gfile = new File(p.getDataFolder(), "gangs.yml");
     
            
    saveConfig();
        }
     
        public 
    FileConfiguration getConfig() {
            return 
    config;
        }
     
        public 
    FileConfiguration getPlayers() {
            return 
    players;
        }
     
        public 
    FileConfiguration getGangs() {
            return 
    players;
        }
     
        public 
    void saveConfig() {
            try {
                
    config.save(cfile);
                
    players.save(pfile);
                
    gangs.save(gfile);
            }
            catch (
    IOException e) {
                
    Bukkit.getServer().getLogger().severe(ChatColor.RED "Could not save config.yml!");
            }
        }
     
        public 
    void reloadConfig() {
            
    config YamlConfiguration.loadConfiguration(cfile);
            
    players YamlConfiguration.loadConfiguration(pfile);
            
    gangs YamlConfiguration.loadConfiguration(gfile);
        }
     
        public 
    PluginDescriptionFile getDesc() {
            return 
    p.getDescription();
        }
    }
     
  6. Offline

    AtomSponge

    JD_Guy17 Try putting
    Code:java
    1. pfile = new File(p.getDataFolder(), "player.yml");
    before
    Code:java
    1. players = YamlConfiguration.loadConfiguration(pfile);
    The same goes for "gangs".
     
  7. Offline

    JD_Guy17

  8. Offline

    AtomSponge

Thread Status:
Not open for further replies.

Share This Page