Sections in config.yml

Discussion in 'Plugin Development' started by ShotCraft3x, Dec 28, 2018.

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

    ShotCraft3x

    Hello everyone, I hope you are having a good day, well, today I came to know how to create new sections in a file.yml.

    To contextualize I am creating a protection plugin with wool, then I want to save this data:

    user-own: Name of the owner of the region
    reg-perm: Number of regions allowed
    value: Name of the regions per user


    Here is the file.yml called database.yml

    Code:
    # Plugin hecho por el servidor AventuraCraftLatin
    # AvC
    # ReloadConfig
    
    
    
    user-own: ShotCraft3x
    reg-perm: 2
    value:
    - d33a58a8-ff63-4b88-bc5d-ef1c761219a21
    - d33a58a8-ff63-4b88-bc5d-ef1c761219a22

    and that's how I want it to look.

    Code:
    # Plugin hecho por el servidor AventuraCraftLatin
    # AvC
    # ReloadConfig
    
    
    
    user-own: ShotCraft3x
    reg-perm: 2
    value:
    - d33a58a8-ff63-4b88-bc5d-ef1c761219a21
    - d33a58a8-ff63-4b88-bc5d-ef1c761219a22
    
    user-own: OtherUser
    reg-perm: 1
    value:
    - 6cb4e888-47d6-3022-a956-8dcc7f8804ee2

    Here is the code that stores the data in the database.yml file

    Code:
    public void SaveData(String nameregion, String player, int cant,Player p) {
            try {
                FileConfiguration database = m.getData();
    
                List<String> list = null;
    
                String nombre = "";
                for(String nom : database.getStringList("user-own")) {
                    nombre = nom;
                }
              
              
                if (!nombre.equalsIgnoreCase(player)) {
                    p.sendMessage("Paso por aqui");
                    database.createSection("user-own");
                    database.createSection("reg-perm");
                    database.set("user-own", player);
                    database.set("reg-perm", cant);
                    if(database.getStringList("value")!=null) {
                        list = database.getStringList("value");
                    }else {
                        list = new ArrayList<String>();
                    }
                    list.add(nameregion + cant);
                    for (int i = 0; i <= database.getInt("reg-perm"); i++) {
                        database.createSection("value");
                        database.set("value", list);
                    }
                    m.saveData();
                    m.ReloadData();
                } else {
                    database.set("reg-perm", cant);
                    if(database.getStringList("value")!=null) {
                        list = database.getStringList("value");
                    }else {
                        list = new ArrayList<String>();
                    }
                  
                    list.add(nameregion + cant);
                  
                    for (int i = 0; i <= database.getInt("reg-perm"); i++) {
                        database.set("value", list);
                    }
                  
                    m.saveData();
                    m.ReloadData();
    
                }
            } catch (Exception e) {
                e.printStackTrace();
    
            }
        }

    I do not know if it is necessary to show more code, if it is, they tell me, I remain attentive to your comments, thank you very much for your help, sorry for my bad english.
     
  2. First of all, no need to create a ConfigurationSection if you're already going to set the path of that section to a certain value afterwards. It's unneseccary code.

    Now, for the part as in how you'd like your YML file to look is impossible. You can not have two fields that share the same name in the same list (in this case, the root). What you should be going for, is something along the lines of this:

    Code:
    # Plugin hecho por el servidor AventuraCraftLatin
    # AvC
    # ReloadConfig
    
    
    ShotCraft3x:
        reg-perm: 2
        value:
        - d33a58a8-ff63-4b88-bc5d-ef1c761219a21
        - d33a58a8-ff63-4b88-bc5d-ef1c761219a22
    
    OtherUser:
        reg-perm: 1
        value:
        - 6cb4e888-47d6-3022-a956-8dcc7f8804ee2
     
    MightyOne likes this.
  3. Offline

    MightyOne

    I feel like I should point out that players' names are no longer unique. Maybe you should think about using uuids in the config instead of names. I guess otherwise there could occur problems when somebody changes their Minecraft name.
     
    Kevinzuman22 likes this.
  4. If you are using normal YAML-Configuration, you should be able to just set the value of "[uuid].reg-perm" to a value and the section should be automaticly created. And as MightyOne already said, you should really use UUID. Just do

    Player.getUniqueId.soString();

    to get the uuid from a Player Object and use

    Bukkit.getPlayer(uuid);

    to get the Player Object from a UUID String object.
     
Thread Status:
Not open for further replies.

Share This Page