Looping through config not working.

Discussion in 'Plugin Development' started by TerraKGaming, Jul 9, 2016.

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

    TerraKGaming

    I have a little problem trying to make a plugin. This loop does not work to check if the player who the command sender is trying to add is the command senders friend.

    Bukkit.getPlayerExact((args[0])).getName()) is the player that the command sender is trying to add to their friends list.

    player is the player who is trying to become the player aboves friend.

    the config stores friends like this. Here is the config of the plugin:
    Config ---------------------------------------------------------------------------
    8e289159-2034-3a16-96b9-9fa637848b3b:
    friends: 3
    friend:
    '1': 34e35a31-7033-3708-8a74-49134019c5e9
    '2': 34e35a31-7033-3708-8a74-49134019c5e9
    '3': 34e35a31-7033-3708-8a74-49134019c5e9
    friendrequest:
    to: 34e35a31-7033-3708-8a74-49134019c5e9
    34e35a31-7033-3708-8a74-49134019c5e9:
    friends: 3
    friend:
    '1': 8e289159-2034-3a16-96b9-9fa637848b3b
    '2': 8e289159-2034-3a16-96b9-9fa637848b3b
    '3': 8e289159-2034-3a16-96b9-9fa637848b3b
    friendrequest:
    from: 8e289159-2034-3a16-96b9-9fa637848b3b
    --------------------------------------------------------------------------------------
    Code -------------------------------------------------------------------------------------------------------------------------
    if (args.length == 1) {
    ConfigurationSection inventorySection = getConfig().getConfigurationSection(player.getUniqueId().toString() + ".friend");
    for (String key : inventorySection.getKeys(false)) {
    player.sendMessage(Bukkit.getServer().getOfflinePlayer(UUID.fromString(inventorySection.get(key).toString())).getName());
    if (Bukkit.getServer().getOfflinePlayer(UUID.fromString(inventorySection.get(key).toString())).getName() == Bukkit.getPlayerExact((args[0])).getName()) {
    player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You are already friends with " + Bukkit.getPlayerExact((args[0])).getName());
    return false;
    }
    }
    -----------------------------------------------------------------------------------------

    This loop is basically trying to check if a uuid under the friend part of the config is equal to the player the person who typed the command is trying to add to their friends list.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 9, 2016
  2. Offline

    mine-care

    @TerraKGaming Please format your code and surround it by [cοde][/cοde] tags, at the moment it is reaaally hard to read.
     
  3. Offline

    SuperSniper

    Took me a while to read this because of the way it's NOT formatted. Anyway.
    Code:
    ConfigurationSection inventorySection = getConfig().getConfigurationSection(player.getUniqueId().toString() + ".friend");
    
    This is looking for a whole new ConfigurationSection in .friend. The thing you're looking for is getStringList(params);

    Code:
      if (Bukkit.getServer().getOfflinePlayer(UUID.fromString(inventorySection.get(key).toString())).getName() == Bukkit.getPlayerExact((args[0])).getName()) {
    
    Never use == for strings, use .equals or .equalsIgnoreCase

    Going back to the first thing I said
    Code:
    player.sendMessage(Bukkit.getServer().getOfflinePlayer(UUID.fromString(inventorySection.get(key).toString())).getName());
    
    This is completely unneeded, just send the STRING LIST as a message by using a for loop.

    Code:
    for(String friends : getConfig().getStringList(uuid + ".friends")) {
    player.sendMessage(friends);
    
     
  4. Offline

    TerraKGaming

    Thanks for the reply and thanks for your help next time ill do that

    Im sort of a noob at the bukkit api so yeah

    Config ---------------------------------------------------------------------------
    Code:
    8e289159-2034-3a16-96b9-9fa637848b3b:
    friends: 3
    friend:
    '1': 34e35a31-7033-3708-8a74-49134019c5e9
    '2': 34e35a31-7033-3708-8a74-49134019c5e9
    '3': 34e35a31-7033-3708-8a74-49134019c5e9
    friendrequest:
    to: 34e35a31-7033-3708-8a74-49134019c5e9
    34e35a31-7033-3708-8a74-49134019c5e9:
    friends: 3
    friend:
    '1': 8e289159-2034-3a16-96b9-9fa637848b3b
    '2': 8e289159-2034-3a16-96b9-9fa637848b3b
    '3': 8e289159-2034-3a16-96b9-9fa637848b3b
    friendrequest:
    from: 8e289159-2034-3a16-96b9-9fa637848b3b
    
    --------------------------------------------------------------------------------------
    Code -------------------------------------------------------------------------------------------------------------------------
    Code:
    if (args.length == 1) {
    ConfigurationSection inventorySection = getConfig().getConfigurationSection(player.getUniqueId().toString() + ".friend");
    for (String key : inventorySection.getKeys(false)) {
    player.sendMessage(Bukkit.getServer().getOfflinePlayer(UUID.fromString(inventorySection.get(key).toString())).getName());
    if (Bukkit.getServer().getOfflinePlayer(UUID.fromString(inventorySection.get(key).toString())).getName() == Bukkit.getPlayerExact((args[0])).getName()) {
    player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You are already friends with " + Bukkit.getPlayerExact((args[0])).getName());
    return false;
    }
    }
    
    -----------------------------------------------------------------------------------------

    I checked that earlier in teh code btw

    im gonna rewrite the code because its wayy to messy to read in eclipse lol

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 10, 2016
Thread Status:
Not open for further replies.

Share This Page