1.8.8 Check if a string exists in a config but not in a specific config-section

Discussion in 'Spigot Plugin Development' started by chief654, Apr 3, 2020.

  1. Hello, now I have following situation:

    I got a config looking like this:

    Code (YAML):

    bot1
    :
      owner
    : 05f673da-66b5-37e3-b03b-09bbf434403e
      x1
    : -48969
      x2
    : -49029
      z1
    : -49975
      z2
    : -50015
      memberamount
    : 1
      members
    :
        899ccf99-e41a-3b48-b478-79fb5a961430
    :
          name
    : bot3
    bot2
    :
      owner
    : 5de77789-baf9-30ea-9aaa-90555cd97647
      x1
    : -49969
      x2
    : -50029
      z1
    : -50975
      z2
    : -51015
      memberamount
    : 1
      members
    :
        0e6c55c0-0373-3d9d-bd19-7bb34e6fff34
    :
          name
    : bot4
     

    bot1 and bot2 represent the owner names of regions set in this config. At the same time, the owner names are the names of the regions itself.
    These owners got members in their regions (bot3 is a member of bot1 and bot4 a member of bot2).

    Now I need to check, if such an owner tries to remove a member of another owner/region (and of course this shouldn't work).
    E.g.: bot1 tries to remove bot4 but bot4 is member of bot2.

    I thought about looping through the whole config and search for the member bot4, but how can I check he's not a member/child of the section bot1?
    And what if bot1 tries to remove bot5? It even doesn't exist in this config, so there would be an error.

    If you don't understand anything or need further details, feel free to ask. Thank you!
     
  2. Please explain what you are trying to do. What are the members exactly?
     
  3. Ok, its for a skyblock plugin. If someone creates an island there will be a region to protect this island. And this region is set in the config (e.g. bot1 is the owner of such an island/region).
    The owner can invite members to this island so they are allowed to build, break blocks etc. there. And such a member of e.g. bot1 is bot3.
     
  4. In my opinion you need a specific island id which the user who tries to remove a member need to adress first.
     
  5. What are you actually trying to do? A command like /region removeMember <player>?
    As a player can only be present in a region, you have to check if the region contains the player. If yes, he is in the same region with the owner, if not, he is not in any region (or maybe in another region)
     
  6. This is a snippet of removing a member of an island/region in the config:

    Code (Java):
     
    public static void kickMember(Player owner, Player member) {
            ConfigurationSection sec = cfg.getConfigurationSection(owner.getName() + ".members");
                for (String s : cfg.getConfigurationSection(owner.getName() + ".members").getKeys(false)) {
                    if (member.getUniqueId().toString().equals(s)) {
                        cfg.set(owner.getName() + ".members." + member.getUniqueId().toString(), null);
                        cfg.set(owner.getName() + ".memberamount", getMembersAmount(owner.getName()));
                    }
                }
                saveCfg();
        }

    And this snippet returns the amount of members of an island:

    Code (Java):
     
    public static int getMembersAmount(Player owner) {
            ConfigurationSection sec = cfg.getConfigurationSection(owner.getName() + ".members");
            int members = sec.getKeys(false).size();
            return members;
        }

    Finally the approach to kick a player as command:

    Code (Java):
                    // Player player is the owner of the island!
                    // Player target is the member to be kicked!

                    if (args[0].equalsIgnoreCase("kick")) {
                        if (args.length == 2) {
                            if (im.hasIsland(player)) {
                                Player target = Bukkit.getPlayer(args[1]);

                                if (target.getName().equals(player.getName())) {

                                    player.sendMessage("§cYou can't kick yourself!");
                                    return false;
                                }
                                if (im.hasIsland(target)) {
                                    player.sendMessage("§cThis player has an existing island!");
                                    return false;
                                }
                                // TODO: Check if target exists AS MEMBER OF ANY ISLAND in config
                                if () {

                                    // Check if player's island has members
                                    if (SkyBlockRegionFileManager.getMembersAmount(player.getName()) > 0) {

                                        // TODO: Check if target is member of player's island
                                        if () {
                                            SkyBlockRegionFileManager.kickMember(player, target);
                                        } else {
                                            player.sendMessage("§cThis member doesn't belong to your island!");
                                        }

                                    } else {
                                        player.sendMessage("§cYour island has no members to kick!");
                                    }
                                }

                            } else {
                                player.sendMessage("§cYou have no island!");
                            }

                        } else {
                            player.sendMessage("§cUsage: /island kick <player>");
                        }
                    }

    The empty if-clauses are the approach.
     
    #6 chief654, Apr 3, 2020
    Last edited: Apr 3, 2020