Solved Stop/Reload new yml file problem.

Discussion in 'Spigot Plugin Development' started by Nosmakos, May 29, 2016.

Thread Status:
Not open for further replies.
  1. hey guys i tried a few stuff before posting this so i'm really tired i'm trying for hours to create new file and stuff.
    so my problem is that when i do for example /reloadgui this will reload in game the changes and save them (no problem with that) after that when i close the server and i open the market.yml file the changes i made are still there but when i open the server the changes goes to the default ones i have set before. Same is happening when reloading the server. Anyone help?
    this is how market.yml looks
    Code (Text):
    ArmorShop[GUI]:
      Slot[1]:
        Lore: '&7Cost is: &e25.000 &9Points'
     
    and this is the whole code in the main class.
    [
    Code (Text):
        public File MarketFile= new File(getDataFolder(), "market.yml");
        public FileConfiguration MarketConfig= YamlConfiguration.loadConfiguration(MarketFile);

        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            onGUI(); //the market stuff
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "----------------------------------------------------");
            if (!setupEconomy() ) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
       
            if(!MarketFile.exists()){
                try {
                    MarketFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
       
                try {
                    MarketConfig.save(MarketFile);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

        private void reloadMarket(){ //this is for the reloading and saving command
            try {
                MarketConfig.load(MarketFile);
                MarketConfig.save(MarketFile);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvalidConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

         // this might be the problem that cause this because it has a default set and when i turn off my server or reload it might adding back the default  "&7Cost is: &e25.000 &9Points");
           private void onGUI(){
            MarketConfig.set("ArmorShop[GUI].Slot[1].Lore", "&7Cost is: &e25.000 &9Points");
            try {
                MarketConfig.save(MarketFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

       //economy
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
        //disable nothing added here about market.yml
        public void onDisable() {
            saveConfig();
            Bukkit.getConsoleSender().sendMessage(org.bukkit.ChatColor.GREEN + "------------------------------ " + getDescription().getVersion());
        }
     
  2. I don't have that much experience with config files but
    Code (Text):
    getConfig().options().copyDefaults(true);
    this seems like the problem, wouldn't that just replace the config with the config you compile the plugin with?
     
  3. i think so hmm but where i have to add this?
     
  4. It's part of your code in the onEnable, I recommend just loading the config file instead.
     
    • Winner Winner x 1
  5. // SOLVED - LOCKED i had to move this MarketConfig.set("ArmorShop[GUI].Slot[1].Lore", "&7Cost is: &e25.000 &9Points"); here
    Code (Text):
    if(!MarketFile.exists()){
                try {
                    MarketFile.createNewFile();
                    MarketConfig.set("ArmorShop[GUI].Slot[1].Lore", "&7Cost is: &e25.000 &9Points");
    than creating new called GUI and save it.
    also i had to add in onEnable this MarketConfig.options().copyDefaults(true); thanks to @SoftSilk
     
Thread Status:
Not open for further replies.