Changing member id of skyblock-config member

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

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

    chief654

    Hello, I have a function where an owner sets a region and it gets saved in a config file. He can add members to this region, the maximum are 4.
    Following, I have an example of a config with 2 members:

    Config (open)

    Code:
    bot1:
      owner: 05f673da-66b5-37e3-b03b-09bbf434403e
      x1: -48969
      x2: -49029
      z1: -49975
      z2: -50015
      memberamount: 2
      member:
        '1':
          name: bot2
          uuid: 5de77789-baf9-30ea-9aaa-90555cd97647
        '2':
          name: bot3
          uuid: 899ccf99-e41a-3b48-b478-79fb5a961430
    


    At the very top, bot1 is the name of the region and its owner.
    But now to my question. If I delete member '1' with the name bot2, it works well and only bot3 with id '2' is left. Even memberamount is set to 1. The problem is, that bot3 still has the id '2' and I dont know how to fix it to '1', because it's now the first member in this config.
    It looks like this:

    Config (open)

    Code:
    bot1:
      owner: 05f673da-66b5-37e3-b03b-09bbf434403e
      x1: -48969
      x2: -49029
      z1: -49975
      z2: -50015
      memberamount: 1
      member:
        '2':
          name: bot3
          uuid: 899ccf99-e41a-3b48-b478-79fb5a961430
    


    I thought of changing the number sections directly to the name and the uuid:

    Config (open)

    Code:
    bot1:
      owner: 05f673da-66b5-37e3-b03b-09bbf434403e
      x1: -48969
      x2: -49029
      z1: -49975
      z2: -50015
      memberamount: 1
      member:
          name: bot3
          uuid: 899ccf99-e41a-3b48-b478-79fb5a961430
    


    But I had the problem that this name and uuid got overwritten if a new member has been added to the config, so I guess numbering is the solution here.

    Finally, the basic code to create these config entries:

    Show Spoiler

    Code:
        public static File f = new File("plugins/Regions", "regions.yml");
        public static FileConfiguration cfg = YamlConfiguration.loadConfiguration(f);
    
        //Creates a region in the config with the owners name as regionName without members
        public static void setRegion(String regionName, UUID owner, int x1, int x2, int z1, int z2) {
            cfg.set(regionName + ".owner", owner.toString());
            cfg.set(regionName + ".x1", x1);
            cfg.set(regionName + ".x2", x2);
            cfg.set(regionName + ".z1", z1);
            cfg.set(regionName + ".z2", z2);
            cfg.set(regionName + ".memberamount", 0);
            saveCfg();
        }
        //Adds specific member to a region
        public static void addMember(String regionName, Player member) {
            cfg.set(regionName + ".memberamount", getMembersAmount(regionName) + 1);
            cfg.set(regionName + ".member" + "." + getMembersAmount(regionName) + ".name", member.getName());
            cfg.set(regionName + ".member" + "." + getMembersAmount(regionName) + ".uuid", member.getUniqueId().toString());
            saveCfg();
        }
    
        public static Integer getMembersAmount(String regionName) {
            Integer amount = cfg.getInt(regionName + ".memberamount");
            return amount;
        }
    
        public static String getMember(String regionName) {
            for (String name : cfg.getConfigurationSection(regionName).getKeys(false)) {
                String memberString = cfg.getString(name + ".member");
                return memberString;
            }
            return null;
        }
        //Removes a specific member from its region
        public static void removeMember(String regionName, Player member) {
            if (getMembersAmount(regionName) > 0) {
                // for (String name : cfg.getConfigurationSection(regionName +
                // ".member").getKeys(false)) {
    
                cfg.set(regionName + ".memberamount", getMembersAmount(regionName) - 1);
                cfg.set(regionName + ".member" + "." + getMembersAmount(regionName), null);
                cfg.set(regionName + ".member" + "." + getMembersAmount(regionName), null);
                saveCfg();
        
            } else {
               //TODO
            }
            // }
            return;
        }
    
        public static UUID getMemberUUID(String regionName) {
            // for (String name : cfg.getConfigurationSection(regionName).getKeys(false)) {
            String memberUUIDString = cfg.getString(regionName + ".member" + "." + getMembersAmount(regionName) + ".uuid");
            return UUID.fromString(memberUUIDString);
          //}
            // return null;
        }

    I'd be happy, if someone can help me how to set the members id correctly.

    EDIT: I don't know if storing the name in general makes sense, because the config wouldn't be updated if the owner/member changes his name; I could get his UUID but I would need to update the config too.
     
    Last edited: Mar 31, 2020
  2. Offline

    NukerFall

    Code:
    List<ConfigurationSection> list = new ArrayList<ConfigurationSection>();
    for (String s : cfg.getConfigurationSection(owner + ".member").getKeys(false)) {
        list.add(cfg.getConfigurationSection(owner + ".member." + s));
    }
    for (Integer i = 0; i < list.size(); i++;) {
        cfg.set(owner + ".member." + i.toString(), list.get(i));
    }
    cfg.save(f);
    This thing will sort your members automatically and put it from 0 to the end.
     
  3. Offline

    chief654

    Thank you very much, but now I use only the uuid and the name instead of ids, thank you anyway.
     
Thread Status:
Not open for further replies.

Share This Page