1.13.2 Config Manager Default Values

Discussion in 'Spigot Plugin Development' started by Lysander12, Jan 18, 2020.

  1. Hey, I've been trying to set default values to something I want. The problem is it doens't work and the file remains empty. Here's the code:

    onEnable:
    Code (Java):
    ItemsFile.setup();
            ItemsFile.get().addDefault("Healthorb.Value", 20.0);
            ItemsFile.get().addDefault("Rottenchunk.Value", 10.0);
            ItemsFile.setup();
            ItemsFile.get().options().copyDefaults(true);
            ItemsFile.save();
    ItemsFile:
    Code (Java):
    package me.xlysander12.mmorpg.configfiles;

    import me.xlysander12.mmorpg.Main;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import java.io.File;
    import java.io.IOException;

    public class ItemsFile {
        static Main plugin = Main.getPlugin(Main.class);
        static FileConfiguration itemssconfig;
        static File itemsfile;

        public static void setup() {
            itemsfile = new File(plugin.getDataFolder(), "Items.yml");

            if (!itemsfile.exists()) {
                try {
                    itemsfile.createNewFile();
                } catch (IOException e) {
                }
            }

            itemssconfig = YamlConfiguration.loadConfiguration(itemsfile);
        }

        public static FileConfiguration get() {
            return itemssconfig;
        }

        public static void save() {
            try {
                itemssconfig.save(itemsfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save items.yml!");
            }
        }

        public static void reload() {
            itemssconfig = YamlConfiguration.loadConfiguration(itemsfile);
        }
    }
     
    Thanks in advance
     
  2. Try to put the copyDefaults part under the setup! You dont must use setDefault(); you can simply use set("path", value);
     
  3. The second ItemsFile.setup() is useless.
    Call ItemsFile.get().set(String, Object)
    instead of
    ItemsFile.get().addDefault()


    cat.png
     
  4. Yea, that initialize a new config object a d the filled one gets overwridden.
     
  5. But won't doing ItemsFile.get().set() overwrite everything that was previously in there?
     
  6. Huh thats true.
    So addDefault should work better in this case.
     
  7. Yeah but that's what I was doing and didn't work. That's why I'm kinda lost