Solved Rewriting a string in a file

Discussion in 'Plugin Development' started by EdenCampo, Sep 8, 2013.

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

    EdenCampo

    Hey there,

    I'm trying to remove a string from a line, meaning if I have

    Code:
    ArenaNames: dev | bukkit
    and I enter the command /delete bukkit I get the new string:

    Code:
    ArenaNames: dev
    I have this code:

    Show Spoiler

    Code:java
    1. public void CFGRemoveArena(String GameArena)
    2. {
    3. String ArenaNames = plugin.SimonCFGM.getArenaConfig().getString("ArenaNames");
    4. String ArenaLocs = plugin.SimonCFGM.getArenaConfig().getString("ArenaLocations");
    5. String ArenaTypes = plugin.SimonCFGM.getArenaConfig().getString("ArenaTypes");
    6. String RelatedArenas = plugin.SimonCFGM.getArenaConfig().getString("RelatedArena");
    7.  
    8. if(ArenaNames == null || ArenaLocs == null || ArenaTypes == null || RelatedArenas == null)
    9. {
    10. plugin.SimonLog.logWarning("Attempted to delete an arena from an empty cfg! Canceled!");
    11. }
    12. else
    13. {
    14. String[] Names = ArenaNames.split(" | ");
    15. String[] Locs = ArenaLocs.split(" | ");
    16. String[] Types = ArenaTypes.split(" | ");
    17. String[] Relateds = RelatedArenas.split(" | ");
    18.  
    19. int id = 0;
    20. int correctid = 0;
    21. while(id < Names.length)
    22. {
    23. if(!ArenaNames.contains(GameArena))
    24. {
    25. plugin.SimonLog.logWarning("Attempted to delete an non-existing arena! Canceled!");
    26. return;
    27. }
    28.  
    29. if(Names[id].contains(GameArena))
    30. {
    31. correctid = id+2;
    32. break;
    33. }
    34.  
    35. id++;
    36. }
    37.  
    38. id = -1;
    39. while(id < correctid)
    40. {
    41. try
    42. {
    43. if(correctid == 2 && id == -1 && Names.length == 1)
    44. {
    45. plugin.SimonCFGM.getArenaConfig().set("ArenaNames", null);
    46. plugin.SimonCFGM.getArenaConfig().set("ArenaLocations", null);
    47. plugin.SimonCFGM.getArenaConfig().set("ArenaTypes", null);
    48. plugin.SimonCFGM.getArenaConfig().set("RelatedArena", null);
    49.  
    50. File signFile = new File(plugin.getDataFolder(), "SavedArenas.yml");
    51. signFile.delete();
    52.  
    53. plugin.SimonCFGM.saveDefaultArenaConfig();
    54. plugin.SimonCFGM.reloadArenaConfig();
    55.  
    56. break;
    57. }
    58.  
    59. if(id == correctid-1)
    60. {
    61. String NewArenaNames = ArenaNames.replaceFirst(Names[correctid], "DELETED");
    62. String NewArenaLocs = ArenaLocs.replaceFirst(Locs[correctid], "DELETED");
    63. String NewRelatedArenas = RelatedArenas.replaceFirst(Relateds[correctid], "DELETED");
    64.  
    65. plugin.SimonCFGM.getArenaConfig().set("ArenaNames", NewArenaNames);
    66. plugin.SimonCFGM.getArenaConfig().set("ArenaLocations", NewArenaLocs);
    67. plugin.SimonCFGM.getArenaConfig().set("RelatedArena", NewRelatedArenas);
    68.  
    69. plugin.SimonLog.logWarning("Finished, found the skip place and made it 'DELETED'");
    70.  
    71. id++;
    72. break;
    73. }
    74.  
    75. if(id == -1)
    76. {
    77. id++;
    78. continue;
    79. }
    80.  
    81. id++;
    82. }
    83. {
    84. break;
    85. }
    86. }
    87.  
    88. plugin.SimonCFGM.saveArenaConfig();
    89. plugin.SimonCFGM.reloadArenaConfig();
    90.  
    91. plugin.SimonCFGM.CFGLoadGameArenas();
    92.  
    93. plugin.SimonLog.logInfo("Deleting process finished for arena: " + GameArena);
    94. }
    95. }


    http://pastebin.com/SgqLgxGh



    and this is the config I am trying to rewrite:

    Show Spoiler

    Code:
    # Default Arena Configuration for 'SimonSays' - Eden.Campo
     
    ArenaNames: dev | bukkit
    ArenaLocations: world,107,86,192 | world,107,86,191
    ArenaTypes: 0 | 0
    RelatedArena: test1 | test2
    


    CorrectID is the index of the string I'm trying to delete in the file.

    I tried to hardcode the locations but I doesn't seem to work, I can't delete anything without everything getting messed up.

    Any ideas?

    Thanks, Eden.
     
  2. Offline

    Chinwe

    Are you using Bukkits YML API? You should use List<String>/List<Integer> to store these, meaning your config would look like this:

    Code:
    ArenaNames:
      - dev
      - bukkit
    ArenaLocations:
      - world,107,86,192
      - world,107,86,191
    ArenaTypes:
    - 0
    - 0
    RelatedArena:
    - test1
    - test2
    Then you can use List operations to remove and add items to them, and set them to the config using getConfig().set("ArenaNames", list);
     
  3. Offline

    EdenCampo

  4. Offline

    Chinwe

    EdenCampo
    In that case, when you use .split(" | "), you use StringBuilder to build the string again using this method:

    Code:
        public static String removeFromList(String list, String toRemove)
        {
            String[] split = list.split(" | ");
            StringBuilder sb = new StringBuilder();
     
            for (String s : split)
            {
                if (!s.equals("|") && !s.equals(toRemove))
                    sb.append(s + " | ");
            }
     
            return sb.toString().contains("|") ? sb.toString().substring(0, sb.lastIndexOf(" | ")) : sb.toString();
        }
     
  5. Offline

    EdenCampo

    Chinwe


    Thanks again, made it fit into the cfg and if GameArena equals "bukkit" it deletes everything but "another"

    Show Spoiler

    Code:java
    1. String ArenaNames = plugin.SimonCFGM.getArenaConfig().getString("ArenaNames");
    2.  
    3. String toRemove = GameArena;
    4.  
    5. String[] Names = ArenaNames.split(" | ");
    6. StringBuilder sb = new StringBuilder();
    7.  
    8. for (String s : Names)
    9. {
    10. if (!s.equals("|") && !s.equals(toRemove))
    11. {
    12. sb.append(s + (s.equals(Names[Names.length - 1]) ? "" : " | "));
    13. plugin.SimonCFGM.getArenaConfig().set("ArenaNames", s);
    14. }
    15. }
    16.  
    17. plugin.SimonCFGM.saveArenaConfig();
    18. plugin.SimonCFGM.reloadArenaConfig();
    19.  
    20. plugin.SimonCFGM.CFGLoadGameArenas();
    21.  
    22. plugin.SimonLog.logInfo("Deleting process finished for arena: " + GameArena);



    Code:
    # Default Arena Configuration for 'SimonSays' - Eden.Campo
     
    ArenaNames: dev | bukkit | another
    ArenaLocations: world,107,86,192 | world,107,86,191 | world,107,86,191
    ArenaTypes: 0 | 0 | 0
    RelatedArena: odf | asd | dasd
    
    turns into:
    Code:
    # Default Arena Configuration for 'SimonSays' - Eden.Campo
    ArenaNames: another
    ArenaLocations: world,107,86,192 | world,107,86,191 | world,107,86,191
    ArenaTypes: 0 | 0 | 0
    RelatedArena: odf | asd | dasd
    
     
  6. Offline

    Chinwe

    You need to move line 13 out of the for loop, and set ArenaNames to sb.toString() instead :)

    I changed it to its own method, now you can just change it to plugin.SimonCFGM.getArenaConfig().set("ArenaNames", removeFromList(Names, GameArena);

    By the way, its conventional for variables to start lowercase, with following words starting withACapitalLetter :)
     
    EdenCampo likes this.
  7. Offline

    EdenCampo

    Chinwe

    Show Spoiler

    Code:java
    1. public void CFGRemoveArena(String GameArena, String location, String type, String related)
    2. {
    3. String ArenaNames = plugin.SimonCFGM.getArenaConfig().getString("ArenaNames");
    4. String ArenaLocs = plugin.SimonCFGM.getArenaConfig().getString("ArenaLocations");
    5. String ArenaTypes = plugin.SimonCFGM.getArenaConfig().getString("ArenaTypes");
    6. String RelatedArenas = plugin.SimonCFGM.getArenaConfig().getString("RelatedArena");
    7.  
    8. if(ArenaNames == null || ArenaLocs == null || ArenaTypes == null || RelatedArenas == null)
    9. {
    10. plugin.SimonLog.logWarning("Attempted to delete an arena from an empty cfg! Canceled!");
    11. }
    12. else
    13. {
    14. CFGremoveFromList("ArenaNames", GameArena);
    15. CFGremoveFromList("ArenaLocations", location);
    16. CFGremoveFromList("ArenaTypes", type);
    17. CFGremoveFromList("RelatedArena", related);
    18.  
    19. plugin.SimonLog.logInfo("Deleting process finished for arena: " + GameArena);
    20. }
    21. }
    22.  
    23.  
    24. public void CFGremoveFromList(String list, String remove)
    25. {
    26. String[] split = list.split(" | ");
    27. StringBuilder sb = new StringBuilder();
    28.  
    29. for (String s : split)
    30. {
    31. if (!s.equals("|") && !s.equals(remove))
    32. {
    33. sb.append(s + (s.equals(split[split.length - 1]) ? "" : " | "));
    34. }
    35.  
    36. plugin.SimonCFGM.getArenaConfig().set(s.toString(), s);
    37. }
    38.  
    39. plugin.SimonCFGM.saveArenaConfig();
    40. plugin.SimonCFGM.reloadArenaConfig();
    41.  
    42. plugin.SimonCFGM.CFGLoadGameArenas();
    43. }



    Thanks again, I got messed up with this :S

    Code above doesn't work either, just resets everything to:
    Code:
    # Default Arena Configuration for 'SimonSays' - Eden.Campo
     
    ArenaNames: ArenaNames
    ArenaLocations: ArenaLocations
    ArenaTypes: ArenaTypes
    RelatedArena: RelatedArena
    
     
  8. Offline

    Chinwe

    It's still in the loop (and the arguments are wrong) :3
    Paste this to replace CFGremoveFromList:
    Code:
        public void CFGremoveFromList(String list, String remove)
        {
            String[] split = list.split(" | ");
            StringBuilder sb = new StringBuilder();
     
            for (String s : split)
            {
                if (!s.equals("|") && !s.equals(remove))
                {
                    sb.append(s + (s.equals(split[split.length - 1]) ? "" : " | "));
                }
     
            }
                plugin.SimonCFGM.getArenaConfig().set(list, sb.toString());
     
            plugin.SimonCFGM.saveArenaConfig();
            plugin.SimonCFGM.reloadArenaConfig();
     
            plugin.SimonCFGM.CFGLoadGameArenas();
        }
     
  9. Offline

    EdenCampo

    Chinwe

    Got it working, thanks!
     
Thread Status:
Not open for further replies.

Share This Page