Edit item in config list

Discussion in 'Plugin Development' started by Jordymt, Nov 23, 2014.

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

    Jordymt

    Hi,

    For a plugin I need to change some value's from my stringlist in the configuration section. This is the config;
    [​IMG]

    What I want is to change Umpalumpa/Open to Umpalumpa/Closed.
    This is some of the code I've created;

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if(cmd.getName().equalsIgnoreCase("ride")) {
    3. if(args.length == 0) {
    4. Bukkit.broadcastMessage(".");
    5. }
    6.  
    7. else if(args.length >= 1) {
    8. if(args[0].equals("close")) {
    9. String varAttr = args[1].toString();
    10. Bukkit.broadcastMessage(varAttr);
    11.  
    12. int size = plugin.getConfig().getStringList("Attracties").size();
    13. String[] attractie = plugin.getConfig().getStringList("Attracties").toArray(new String[0]);
    14. for(int i = 0; i < size ; i++) {
    15. String[] split = attractie[i].split("/");
    16. String attractienaam = split[0];
    17. String attractiestatus = split[1];
    18.  
    19. Bukkit.broadcastMessage(attractienaam);
    20. if(attractienaam.equalsIgnoreCase(varAttr.toLowerCase())) {
    21.  
    22.  
    23. if(attractiestatus.equalsIgnoreCase("Closed")) {
    24. //Don't do anything, it's already open
    25. }
    26.  
    27. else {
    28. //Open it
    29.  
    30.  
    31. }
    32.  
    33. }
    34.  
    35. }
    36. }[/i]
     
  2. Offline

    Skionz

    use FileConfiguration#getList() to get the List from the config. Then use the remove() method to remove a certain line from the list. Then you can add a new line by using the add() method. After adding the line to the list put the list back in the configuration file using the FileConfiguration#set() method.
     
  3. Offline

    Jordymt

    That could be possible, but that would mess up the order of the items, the order is rather important
     
  4. Offline

    Skionz

    Jordymt Remove then both then.
     
  5. Offline

    Jordymt

    I'll be a list of around 36 items
     
  6. Offline

    Skionz

    You could just use booleans. config.set("Umpalumpa", true);
     
  7. Offline

    Jordymt

    Tried this;
    Code:java
    1. plugin.getConfig().getStringList("Attracties").remove(i);
    2. plugin.getConfig().getStringList("Attracties").add(attractienaam + "/Open");
    3. plugin.saveDefaultConfig();
    4. plugin.saveConfig();
    5. plugin.reloadConfig();


    Won't add it to the config
     
  8. Offline

    Skionz

    Jordymt saveDefaultConfig() will save the default values. Just use booleans.
     
  9. Offline

    Rocoty

    Have a look into the methods List#indexOf(Object) and List#set(int, E). They should be all you need. But I would advise you to drop this design completely, and use a completely different setup than a simple list. Remember keys in the config can have subkeys.
     
  10. Offline

    Jordymt

    Can't just use booleans, I need to loop the list trough to make a menu

    Same thing as I say to Skionz, don't think this will work, or it will be to complicated

    Edit; I'm talking about the keys and subkeys, indexOf and set I will look into, thanks!
     
  11. Offline

    Rocoty

    Jordymt You can loop through all subkeys of a key in the config. YamlConfiguration#getConfigurationSection and YamlConfiguration#getKeys should help you there. Please consider it.
     
    Jordymt likes this.
  12. Offline

    Jordymt

    Thanks, I tried it out and it works perfectly and much easier.
     
    Rocoty likes this.
Thread Status:
Not open for further replies.

Share This Page