Solved Config file resetting on server reload

Discussion in 'Plugin Development' started by srspore, May 21, 2016.

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

    srspore

    I tried to make a simple plugin basically to keep track of a player's balance, which is just a double. I can set a get the balance just fine, but when I reload the server the balance gets set back to 0.0. I'm very new to config files and I've probably done something wrong, but I can't figure out what. Thank you!

    This is the code that deals with the config file.
    Code:
    // Followed pogostick29dev tutorial on settings managers.
    public class BankManager {
        private BankManager() {
        }
    
        static BankManager instance = new BankManager();
    
        public static BankManager getInstance() {
            return instance;
        }
    
        private Plugin p;
        private FileConfiguration config;
        private File cfile;
    
        public void setup(Plugin p) {
            config = p.getConfig();
            config.options().copyDefaults(true);
            cfile = new File(p.getDataFolder(), "bank.yml");
            saveConfig();
        }
    
        public FileConfiguration getConfig() {
            return config;
        }
    
        public void saveConfig() {
            try {
                config.save(cfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not creat bank.yml!");
            }
        }
    
        public void reloadConfig() {
            config = YamlConfiguration.loadConfiguration(cfile);
        }
    
        public void setBalance(String player, double amount) {
            player = player.toLowerCase();
            config.set(player, amount);
        }
       
        public double getBalance(String player) {
            if (!config.contains(player)) {
                config.set(player, 0.0);
            }
            return config.getDouble(player);
        }
    }
    This is the code that deals with the commands
    Code:
    public class Main extends JavaPlugin {
       
        BankManager bank;
       
        public void onEnable() {
            bank = BankManager.getInstance();
            bank.setup(this);
        }
       
        public void onDisable() {
            bank.saveConfig();
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (commandLabel.equalsIgnoreCase("setBal")) {
                bank.setBalance(sender.getName(), Double.parseDouble(args[0]));
                bank.saveConfig();
                sender.sendMessage("Balance set to: " + bank.getBalance(sender.getName()));
            } else if (commandLabel.equalsIgnoreCase("getBal")) {
                bank.reloadConfig();
                sender.sendMessage("" + bank.getBalance(sender.getName()));
            }
            return true;
        }
    }
    Thanks again!

    I figured it out, I just needed to add "p.saveConfig()" to the saveConfig() method in the Bank Manager class...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
Thread Status:
Not open for further replies.

Share This Page