1.16.5 Custom config not saving.

Discussion in 'Spigot Plugin Development' started by Creatos137, Jan 21, 2021.

  1. Hi

    I created a custom config and am trying to use it but when I call my save method it doesnt save to the config file. My file is just empty. What have I done wrong? I also get an error when I use loadBuilds(player) because the file is empty.

    My main class:
    Code (Java):
    private static final HashMap<Player, ArrayList<Build>> BUILDS = new HashMap<>();
       
        private static Core instance;
       
        public BuildManager buildManager;
       
        public void onEnable() {
            instance = this;
            System.out.println(Text.CONSOLE + "Succesfully enabled.");
            this.buildManager = new BuildManager(this);
            this.buildManager.loadBuilds();
            initTickEvent();
            registerCommands();
            registerEvents();
           
        }
    public static ArrayList<Build> getBuilds(Player player) {
            return BUILDS.get(player);
        }
       
        public static ArrayList<Build> getBuilds(Player player, ClassType classType) {
            ArrayList<Build> output = new ArrayList<Build>();
            getBuilds(player);
           
            for(Build build : getBuilds(player)) {
                if(build.getClassType().equals(classType))
                    output.add(build);
            }
           
            return output;
        }
       
        public static HashMap<Player, ArrayList<Build>> getBuilds(){
            return BUILDS;
        }


        public static Core getInstance() {
            return instance;
        }

    My custom config class
    Code (Java):
    package me.creatos.champions.files;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map.Entry;
    import java.util.UUID;
    import java.util.logging.Level;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    import me.creatos.champions.Core;
    import me.creatos.champions.api.Text;
    import me.creatos.champions.build.Build;
    import me.creatos.champions.build.ClassType;
    import me.creatos.champions.build.item.ChampionItem;
    import me.creatos.champions.build.item.ItemId;
    import me.creatos.champions.build.skill.Skill;
    import me.creatos.champions.build.skill.SkillId;
    import me.creatos.champions.build.skill.SkillType;

    public class BuildManager {

        private final String FILE_NAME = "build.yml";

        private Core plugin;
        private FileConfiguration buildConfig = null;
        private File configFile = null;

        public BuildManager(Core plugin) {
            this.plugin = plugin;
            saveDefaultConfig();
        }

        // Create the config if there is no config, check if everything is correct
        public void reloadConfig() {
            if (this.buildConfig == null)
                this.configFile = new File(this.plugin.getDataFolder(), FILE_NAME);

            this.buildConfig = YamlConfiguration.loadConfiguration(this.configFile);

            InputStream defaultStream = this.plugin.getResource(FILE_NAME);

            if (defaultStream != null) {
                YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultStream));
                this.buildConfig.setDefaults(defaultConfig);
            }
        }

        // Get the config file
        public FileConfiguration getConfig() {
            if (this.buildConfig == null)
                reloadConfig();
            return this.buildConfig;
        }

        // You are going to use this after you add stuff to the config file
        public void saveConfig() {
            if (this.buildConfig == null || this.configFile == null)
                return;

            try {
                this.getConfig().save(this.configFile);
            } catch (IOException e) {
                this.plugin.getLogger().log(Level.SEVERE, "Could not save config to " + this.configFile, e);
            }
        }

        // Initialize and save the file
        public void saveDefaultConfig() {
            if (this.configFile == null)
                this.configFile = new File(this.plugin.getDataFolder(), FILE_NAME);

            if (!this.configFile.exists()) {
                this.plugin.saveResource(FILE_NAME, false);
            }
        }

        // METHODS SPECIFIC TO THIS FILE

        public void saveBuilds() {
            for (Entry<Player, ArrayList<Build>> entry : Core.getBuilds().entrySet()) {
                Player player = entry.getKey();
                saveBuilds(player);
            }
            saveConfig();
            System.out.println(Text.DEBUG + "All builds saved to build.yml");
        }

        public void saveBuilds(Player player) {
            ArrayList<Build> builds = Core.getBuilds(player);
            if(builds == null) {
                System.out.println(1);
                return;
            }
               
            for (Build build : builds) {

                getConfig().set("builds." + player.getUniqueId().toString(), null);

                String buildPath = "builds." + player.getUniqueId().toString() + "." + build.getDisplayName();

                // Save ClassType
                getConfig().set(buildPath + "classtype", build.getClassType().toString());

                // Save Skills
                if (build.getSkill(SkillType.SWORD) != null) {
                    getConfig().set(buildPath + ".skills.sword.skillid",
                            build.getSkill(SkillType.SWORD).getSkillId().toString());
                    getConfig().set(buildPath + ".skills.sword.level", build.getSkill(SkillType.SWORD).getSkillLevel());
                } else {
                    getConfig().set(buildPath + ".skills.sword", null);
                }

                if (build.getSkill(SkillType.AXE) != null) {
                    getConfig().set(buildPath + ".skills.axe.skillid",
                            build.getSkill(SkillType.AXE).getSkillId().toString());
                    getConfig().set(buildPath + ".skills.axe.level", build.getSkill(SkillType.AXE).getSkillLevel());
                } else {
                    getConfig().set(buildPath + ".skills.axe", null);
                }

                if (build.getSkill(SkillType.BOW) != null) {
                    getConfig().set(buildPath + ".skills.bow.skillid",
                            build.getSkill(SkillType.BOW).getSkillId().toString());
                    getConfig().set(buildPath + ".skills.bow.level", build.getSkill(SkillType.BOW).getSkillLevel());
                } else {
                    getConfig().set(buildPath + ".skills.bow", null);
                }

                if (build.getSkill(SkillType.PASSIVE_A) != null) {
                    getConfig().set(buildPath + ".skills.passive_a.skillid",
                            build.getSkill(SkillType.PASSIVE_A).getSkillId().toString());
                    getConfig().set(buildPath + ".skills.passive_a.level",
                            build.getSkill(SkillType.PASSIVE_A).getSkillLevel());
                } else {
                    getConfig().set(buildPath + ".skills.passive_a", null);
                }

                if (build.getSkill(SkillType.PASSIVE_B) != null) {
                    getConfig().set(buildPath + ".skills.passive_b.skillid",
                            build.getSkill(SkillType.PASSIVE_B).getSkillId().toString());
                    getConfig().set(buildPath + ".skills.passive_b.level",
                            build.getSkill(SkillType.PASSIVE_B).getSkillLevel());
                } else {
                    getConfig().set(buildPath + ".skills.passive_b", null);
                }

                if (build.getSkill(SkillType.PASSIVE_C) != null) {
                    getConfig().set(buildPath + ".skills.passive_c.skillid",
                            build.getSkill(SkillType.PASSIVE_C).getSkillId().toString());
                    getConfig().set(buildPath + ".skills.passive_c.level",
                            build.getSkill(SkillType.PASSIVE_C).getSkillLevel());
                } else {
                    getConfig().set(buildPath + ".skills.passive_c", null);
                }

                // Save Items
                for (int index = 0; index < 9; index++) {
                    if (build.getItem(index) != null) {
                        getConfig().set(buildPath + ".items." + index, build.getItem(index).getId());
                    } else {
                        getConfig().set(buildPath + ".items." + index, null);
                    }

                }
               
               
            }
            saveConfig();
        }

        public void loadBuilds() {
            if(getConfig().getList("") == null) {
                System.out.println(Text.DEBUG + "Builds were empty");
                return;
            }
               
            for (String string : getConfig().getConfigurationSection("builds").getKeys(false)) {
                Player player = Bukkit.getPlayer(UUID.fromString(string));
                if (player == null)
                    return;

                if (!Bukkit.getOnlinePlayers().contains(player))
                    return;

                if (Core.getBuilds().containsKey(player))
                    return;

                loadBuilds(player);
               
            }
            System.out.println(Text.DEBUG + "All builds loaded from build.yml");
        }
       
        public void loadBuilds(Player player) {
            if(getConfig().contains("builds." + player.getUniqueId())) {
                System.out.println(Text.DEBUG + player.getDisplayName() + " has no saved builds.");
                return;
            }
               
            for (String displayName : getConfig().getConfigurationSection("builds." + player.getUniqueId().toString())
                    .getKeys(false)) {

                String buildPath = "build." + player.getUniqueId().toString() + "." + displayName;

                ClassType classType = ClassType.valueOf(getConfig().getString(buildPath + ".classtype"));
                Material material = null;
                Skill sword = null;
                Skill axe = null;
                Skill bow = null;
                Skill passiveA = null;
                Skill passiveB = null;
                Skill passiveC = null;
                HashMap<Integer, ChampionItem> items = new HashMap<>();

                // Get all skills
                if (getConfig().get(buildPath + ".skills") != null) {

                    for (String skill : getConfig().getConfigurationSection(buildPath + ".skills").getKeys(false)) {
                        SkillId skillId = SkillId.get(getConfig().getString(buildPath + ".skills." + skill + ".skillid"));
                        int skillLevel = getConfig().getInt(buildPath + ".skills." + skill + ".level");

                        switch (skill) {
                        case "sword":
                            sword = Skill.skillFromId(player, skillId, skillLevel);
                            break;
                        case "axe":
                            axe = Skill.skillFromId(player, skillId, skillLevel);
                            break;
                        case "bow":
                            bow = Skill.skillFromId(player, skillId, skillLevel);
                            break;
                        case "passive_a":
                            passiveA = Skill.skillFromId(player, skillId, skillLevel);
                            break;
                        case "passive_b":
                            passiveB = Skill.skillFromId(player, skillId, skillLevel);
                            break;
                        case "passive_c":
                            passiveC = Skill.skillFromId(player, skillId, skillLevel);
                            break;

                        default:
                            break;
                        }
                    }
                }

                // Get all items;
                for (int index = 0; index < 9; index++) {
                    ChampionItem item = null;
                    if (getConfig().getString(buildPath + ".items." + index) != null) {
                        item = ChampionItem.fromId(ItemId.valueOf(getConfig().getString(buildPath + ".items." + index)), player);
                    }
                    items.put(index, item == null ? null : item);

                }

                // ADD BUILD TO BUILDS LIST
                if (!Core.getBuilds().containsKey(player))
                    Core.getBuilds().put(player, new ArrayList<>());

                if (Core.getBuilds().get(player) == null)
                    Core.getBuilds().put(player, new ArrayList<>());

                Core.getBuilds().get(player).add(new Build(player, classType, displayName, material, sword, axe, bow,
                        passiveA, passiveB, passiveC, items));
            }

        }

    }
     
     
  2. I think in the "saveDefaultConfig" method you need to add a backslash between "this.plugin.getDataFolder()" and "FILE_NAME", for example:
    Code (Java):
    this.configFile = new File(this.plugin.getDataFolder()+"\", FILE_NAME);
    Tell me if that works.
     
  3. No, that is incorrect. Also, this is a post from almost 4 months ago, why are you replying to it?
     
    • Agree Agree x 1