Cant save to custom config file?

Discussion in 'Spigot Plugin Development' started by Mhalllz, Aug 13, 2018.

  1. I want to to create boolean sections in the config when the player joins, what am i doing wrong?

    Code (Java):
    package me.MC_Hoxen.Secrets;

    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    import java.util.logging.Logger;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {

        Logger log = this.getLogger();

        File dataFile = new File(this.getDataFolder(), "data.yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dataFile);

        public void onEnable() {

            log.info("Advancius Secrets has been enabled!");
            try {
                createFiles();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void onDisable() {

            log.info("Advancius Secets has been disabled!");
            saveConfig();

        }

        public void createFiles() throws IOException {

            getConfig().options().copyDefaults(true);
            saveConfig();

            if (!dataFile.exists()) {
                data.options().copyDefaults(true);
                data.save(dataFile);
            }
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent event) {

            Player player = (Player) event.getPlayer();
            UUID pUUID = player.getUniqueId();

            for (int x = 0; x > getConfig().getInt("Ammound-Of-Rewards"); x++) {

                data.set("Status." + pUUID + "." + x, false);

            }

        }

    }
     
  2. Just updated the class abit


    Code (Java):
    package me.MC_Hoxen.Secrets;

    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    import java.util.logging.Logger;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {

        Logger log = this.getLogger();

        File dataFile = new File(this.getDataFolder(), "data.yml");
        FileConfiguration data = YamlConfiguration.loadConfiguration(dataFile);

        public void onEnable() {
            log.info("Advancius Secrets has been enabled!");
            createFiles();
        }

        public void onDisable() {
            log.info("Advancius Secets has been disabled!");
            saveFiles();
        }

        public void createFiles() {

            getConfig().options().copyDefaults(true);
            saveConfig();

            if (!dataFile.exists()) {
                data.options().copyDefaults(true);
                try {
                    data.save(dataFile);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        public void saveFiles() {
            saveConfig();
            try {
                data.save(dataFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            saveConfig();
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent event) throws IOException {
            Player player = (Player) event.getPlayer();
            UUID pUUID = player.getUniqueId();
            for (int x = 0; x > getConfig().getInt("Ammound-Of-Rewards"); x++) {
                data.set("Status." + pUUID + "." + x, false);
                data.save(dataFile);
            }
        }
    }