Removing a string from a string list

Discussion in 'Plugin Development' started by MCCoding, Jun 9, 2014.

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

    MCCoding

    I'm trying to remove a specific string from all the string lists in a config file but every way i have tried it does not work from some reason that i cannot wrap my head around. Here is how my config looks

    Code:
      Group1:
        Members:
        - signal23
        Friends:
        - Group2 | NO_PVP
      Group2:
        Members:
        - alphab76
        Friends:
        - Group1 | NO_PVP
    What i'm trying to do is when a group gets deleted it will remove any friend groups it may have, so if i delete Group2, Group1 would have - Group2 | NO_PVP removed from it as that group no longer exists but i can't get it to be removed. it also has to be able to remove - Group2 | NO_PVP from every Friends: path if it is there here is my method that i have now that i can't seem to get working

    Code:java
    1. public void removeGroupsFriends(String removedGroupName) {
    2.  
    3. if(Core.getInstance().cm.getGroupsConfig().getConfigurationSection("Groups.") != null) {
    4.  
    5. for(String group : Core.getInstance().cm.getGroupsConfig().getConfigurationSection("Groups.").getKeys(false)) {
    6.  
    7. List<String> values = Core.getInstance().cm.getGroupsConfig().getStringList("Groups." + removedGroupName + ".Friends");
    8.  
    9. for(String members : values) {
    10.  
    11. String name = members.split(" | ")[0];
    12.  
    13. values.remove(name);
    14.  
    15. Core.getInstance().cm.getGroupsConfig().set("Groups." + removedGroupName + ".Friends", " ");
    16.  
    17. Core.getInstance().cm.getGroupsConfig().set("Groups." + removedGroupName + ".Friends", values);
    18.  
    19. Core.getInstance().cm.saveGroupsConfig();
    20. }
    21. }
    22. }
    23. }
     
  2. Offline

    MCCoding

    Anyone know why?
     
  3. Offline

    Boomer

    It appears that if you removed Group1 , your latter code is looking IN Groups.Group1.Friends to see if there are any instances of Group1 to remove ... you dont appear to be checking the OTHER groups friends list.
     
  4. Offline

    1Rogue

    Why are you implementing a pseudo-list when you can already use List<String> directly from a FileConfiguration?
     
  5. Offline

    MCCoding

    1Rogue
    I couldn't get it working that way how would you do it?
     
  6. Offline

    1Rogue

    Code:
    yourList:
    - this
    - is
    - an
    - example
    - potato
    Code:java
    1. String rem = "example";
    2. List<String> lis = /*config*/.getStringList("yourList");
    3. if (!lis.isEmpty()) { lis.remove(rem); }
    4. /*config*/.set("yourList", lis);
    5. /*config*/.save();

    Code:
    yourList:
    - this
    - is
    - an
    - potato
     
Thread Status:
Not open for further replies.

Share This Page