Solved How to store arrays in a .yml file?

Discussion in 'Plugin Development' started by austinv11, Apr 15, 2014.

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

    austinv11

    As the title states, I'm looking into doing this, if it's not possible are there any alternatives?

    Thanks!
     
  2. Offline

    Hsflaxz

    Loop through the array and store all the elements under your selected YAML path.
     
  3. Offline

    austinv11

    How so? Won't it overwrite the previous values?
     
  4. Offline

    Hsflaxz

    Code:java
    1. int elementAmount = array.size();
    2.  
    3. for (int i = 0; i < elementAmount; i++) {
    4. String element = array.get(i);
    5. this.getConfig().set("array." + i, element);
    6. }


    Yes, it will. But if they existed before the loop, and are still in the array, the value will still be there.
     
  5. Offline

    austinv11

    That wont really work for me, I want the user to just input into the array
     
  6. Offline

    Hsflaxz

    When the command is ran, just do arrary.add("value")?
     
  7. Offline

    austinv11

    i mean, within the conifg

    Sorry for the bump, but my issue hasn't been solved, I want the user to edit the array in the .yml file, and if it's not possible, are there any alternatives?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    MCForger

    austinv11
    So you probably want to do something were your plugin saves the config with default value and then you want it to simply load the values from the config and allow the user to only edit the array from the config correct?
     
  9. Offline

    austinv11


    Yeah, I get that, but I want the config to look something like this for example:
    Code:
    Options
      randomConfigOption: [x, y, z]
    rather than something like Hsfaxz suggested:
    Code:
    Options
      randomConfigOption:
          1: x
          2: y
          3: z
    And I don't know how to do this
     
  10. Offline

    MCForger

    austinv11
    Alright so...load it as a string the split the string apart by commas and replace [] with nothing.
     
  11. Offline

    austinv11

    I guess that would work, but I'm still relatively new to Java, so how would I split the string up? Like what methods do I use?
     
  12. Offline

    MCForger

    austinv11
    So you would use the string.replace("[", "") and string.replace("]", ""). Then you would break the string up into String[] by doing string.split(",").
     
  13. Offline

    austinv11

    Thanks!
     
  14. Offline

    MCForger

    austinv11
    Here is an example for you
    Code:java
    1. public String[] loadString(String fromConfig)
    2. {
    3. fromConfig = fromConfig.replace("[", "").replace("]", "");
    4. return fromConfig.split(",");
    5. }
     
  15. Offline

    austinv11


    Oh wow thanks so much!
     
Thread Status:
Not open for further replies.

Share This Page