Config defaults

Discussion in 'Plugin Development' started by TerroDoor, Mar 31, 2020.

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

    TerroDoor

    Hello, I want to create 2 config paths on default whenever the config is first created/empty.
    I want a “spawn-loc” and “player-stats” section in the same file, however I’m unsure how to go about this.

    Do I create the defaults in my onEnable? Would it override all the info stored in these sections by doing so? How can I create these sections only if they don’t exist, if not, then keep all current data in my sections, Thankyou!

    Code:
     public void onEnable() {
     util.registerCommands();
     util.registerListeners();
     
     if (this.getConfig().getConfigurationSection("player-data") == null) {
     if (this.getConfig().getConfigurationSection("spawn-loc") == null) {
     
     this.getConfig().createSection("player-data");
     this.getConfig().createSection("spawn-loc");
     
     }
     }
     
     this.getConfig().options().copyDefaults(true);
     this.saveConfig();
     
     }
    
    this creates my defauts, im unsure why they have a '{}' next to them either.. When the player joins it wont put them into the created section?

    Code:
    
     if (!(pl.getConfig().getConfigurationSection("player-data").contains(p.getName().toLowerCase()))) {
     
     pl.getConfig().set(pl.getConfig().getConfigurationSection("player-data") + p.getName().toLowerCase() + ".uuid", uuid);
     pl.getConfig().set(pl.getConfig().getConfigurationSection("player-data") + p.getName().toLowerCase() + ".kills", 0);
     pl.getConfig().set(pl.getConfig().getConfigurationSection("player-data") + p.getName().toLowerCase() + ".deaths", 0);
     pl.getConfig().set(pl.getConfig().getConfigurationSection("player-data") + p.getName().toLowerCase() + ".balance", 100);
     pl.saveConfig();
     } else {
     
     p.sendMessage("welcome back!");
     
     }
    
    Sent from my iPhone using Tapatalk
     
    Last edited: Mar 31, 2020
  2. Offline

    NukerFall

    {} means section has no data.
    Just use getConfig()#set(key, value);


    Code:
        getConfig().set("player-data", pdata);
        getConfig().set("spawn-loc", sloc);
    
     
Thread Status:
Not open for further replies.

Share This Page