Getting a list from a config?

Discussion in 'Plugin Development' started by CraftCreeper6, Jul 26, 2014.

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

    CraftCreeper6

    Hi. How would I get a list from a config?
    Say my config was:

    Code:
    Amount:
      - 1
      - 2
      - 3
      - 4
    
    How would I get the amounts (1, 2, 3 and 4) and send them to a player?

    One more question. How would I create the list :p
    Thanks!
     
  2. Offline

    xTigerRebornx

  3. Offline

    k9rosie

    Code:java
    1. List<Integer> list = config.getIntegerList("amount");
    2. //get list
    3.  
    4. for(int i : list){
    5. player.sendMessage(list.get(i)); //send it to player
    6. }


    is this what you mean?
     
  4. Offline

    CraftCreeper6

    xTigerRebornx
    Okay. I got how to list the string but I do not understand what you mean by creating a loop?

    k9rosie
    Would
    Code:java
    1. List<?> gangs = getConfig().getList("Gangs");
    2. p.sendMessage(t + "The current Gangs are: " + gangs);

    work?

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

    xTigerRebornx

  6. Offline

    k9rosie


    no. you can't just print a List like that to the player.
    you need to loop through the List and print everything the loop finds. for example:

    Code:java
    1. List<String> strings = new ArrayList<String>(); //create an empty list for the String object
    2. //set strings
    3. strings.add("I");
    4. strings.add("Like");
    5. strings.add("Pie");
    6.  
    7. //loop through the list and print every string to the player
    8. for(String string : strings){
    9. player.sendMessage(string);
    10. }


    this all depends on what your list consists of
     
  7. Offline

    CraftCreeper6

    k9rosie
    My list is:
    Code:
    Gangs:
      - 1
      - 2
      - 3
    EDIT: It wont let me add a list to an ArrayList
    This is my code:
    Code:java
    1. List<?> gangs = getConfig().getList("Gangs");
    2. ArrayList<String> gangz = new ArrayList<String>();
    3. gangz.add(gangs);
    4. p.sendMessage(t + " The current Gangs are: ");
    5. for(String g : gangz){
    6. p.sendMessage(g);
    7. }
     
  8. Offline

    k9rosie


    since it seems like you're only using integers in your list this is what your code would look like:

    Code:java
    1. List<Integer> gangs = getConfig().getIntegerList("Gangs");
    2. p.sendMessage(t + "The current Gangs are: " + gangs);
    3.  
    4. for(Integer i : gangs){
    5. player.sendMessage(i.toString());
    6. }


    edit: made a mistake in my code sorry x2
     
  9. Offline

    CraftCreeper6

    k9rosie
    And if I was using Strings...? (Sorry, I am noob at configs :p)

    k9rosie
    I get this: Type mismatch: cannot convert from element type capture#2-of ? to String
    When using:
    Code:java
    1. List<?> gangs = getConfig().getList("Gangs");
    2. p.sendMessage(t + " The current Gangs are: ");
    3. for(String g : gangs){ // <<<<<<<<<<<<<<<<< ERROR ON GANGS
    4. p.sendMessage(g);
    5. }


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

    k9rosie


    you would have to change the List parameter to String instead of Integer, change getConfig().getIntegerList() to getConfig.getStringList(), and also change the for loop object as well.
    Code:java
    1. List<String> gangs = getConfig().getStringList("Gangs");
    2. p.sendMessage(t + "The current Gangs are: " + gangs);
    3.  
    4. for(String i : gangs){
    5. player.sendMessage(i);
    6. }
     
  11. Offline

    CraftCreeper6

    k9rosie
    Thanks! I am pretty sure I looked for a getStringList :eek:
     
Thread Status:
Not open for further replies.

Share This Page