Plugin Configuration - Get sections/paths

Discussion in 'Plugin Development' started by Bl4ckEmp1re, May 8, 2014.

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

    Bl4ckEmp1re

    Hi,
    my configuraion looks like this:
    Code:
    one:
      key:
        - values
        - values
        - values
      key2:
        - values
        - values
        - values
    two:
      key3:
        - values
        - values
        - values
      key4:
        - values
        - values
        - values
    three:
      key5:
        - values
        - values
        - values
      key6:
        - values
        - values
        - values
    How can I get all sections inside another sections?
    Example: I have the section "two", how can I get all sections inside "two". So I need to get "key3" and "key4".
    Oh and also I need to know the order.

    Sincerely,
    Bl4ckEmp1re
     
  2. Offline

    HungerCraftNL

    PHP:
    List<Stringkey getConfig().getStringList("one.key");
     
  3. Offline

    Bl4ckEmp1re

    No, I need to get "key3" and "key4" not the content of them ;)
     
  4. Offline

    nlthijs48

    Bl4ckEmp1re Then you need to get the configurationsection of the parent of those, in this case 'one' and then call .getKeys() on it, the 'false' as argument indicates that you want only the top level and not all keys that are lower. Code:
    Code:java
    1. List<String> keys = getConfig().getConfigurationSection("one").getKeys(false);
     
  5. Offline

    Bl4ckEmp1re

    If I use it, it say that I have to cast it, but if I cast it I got in error (in console of minecraft), that it can't cast it.
     
  6. Offline

    nlthijs48

    Made a mistake, my bad, use a Set instead of a List and it should work.
     
  7. Offline

    Bl4ckEmp1re

    ok, and how I get the (let us say) first string?
     
  8. Offline

    nlthijs48

    You can loop through all the values with an enhanced for loop (using the iterator basically), code example:
    Code:java
    1. for(String key : keys) {
    2. // Do something with 'key' that is the variable containing 1 string
    3. }

    If you really want only the first one you could use the .toArray() function on the set and then get the first element of that with [0], but the .toArray() gives you an object array so then you need to cast before you can use the contents. Also you need to check if the array actually has an element.
     
  9. Offline

    Bl4ckEmp1re

    nlthijs48
    Could I also use .addAll?
    Like this:
    Code:
    ArrayList<String> list = new ArrayList<String>();
    list.addAll(keys);
     
  10. Offline

    nlthijs48

    Bl4ckEmp1re Yes, if you want to have methods to get a certain item you can add all items to a list. You can write it in one line, ArrayList has a constructor that takes a set:
    Code:java
    1. ArrayList<String> list = new ArrayList<String>(keys);
     
    Bl4ckEmp1re likes this.
Thread Status:
Not open for further replies.

Share This Page