Solved Add item to list in Config

Discussion in 'Plugin Development' started by Domi381, Jan 28, 2013.

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

    Domi381

    Hey,
    I want a list in my config, where all players are listed. All of them are saved in the arraylist "freiwillige_spieler". The method getstats() calls my second config file, that should work. Here ist my code so far:

    Code:
    List<?> users = this.getstats().getList("Nutzer.Liste");
            for(Player p : freiwillige_spieler) {
                if (!users.contains(p.getName())) {
                    users.add(p.getName());
                }
            }
    What am i doing wrong? Eclipse shows an error in the line i want to add the name to the list. Pls help me!
     
  2. Offline

    xXSniperzzXx_SD

    Why not loop through the strings of the list?

    Code:
    for(String string : getConfig().getStringList(""){
        } 
    Then to get the list:
    Code:
    ArrayList<String> list = new ArrayList<String>();
    list = getConfig().getStringList("");
    Then to add to it:
    Code:
    list.remove("");
    getConfig().set("", list);
    Then to add:
    Code:
    list.add("");
    getConfig().set("", list);
    Domi381
    but now that i re-read your question i notice i completly misread it...

    To fix what your trying to do:
    Code:
    List<String> users = this.getstats().getList("Nutzer.Liste");
            for(String s : freiwillige_spieler) {
                if (!users.contains(s)) {
                    users.add(s);
                }
            }
    Just make sure you save your players as their name, not as the Player data. I forget the reason why, but some guy has it in their signature, just look for that :p

    Domi381
    Code:
    ArrayList<String> users = this.getstats().getList("Nutzer.Liste");
            for(String s : freiwillige_spieler) {
                if (!users.contains(s)) {
                    users.add(s);
                }
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  3. Offline

    Domi381

    Still not working.

    Code:
    List<String> users = this.getstats().getList("Nutzer.Liste");
            for(Player p : freiwillige_spieler) {
                if (!users.contains(p.getName())) {
                    users.add(p.getName());
                }
            }
    Error: "Type mismatch: cannot convert from List<capture#1-of ?> to List<String>"
     
  4. Offline

    xXSniperzzXx_SD

    Domi381

    Code:
    ArrayList<String> users = this.getstats().getList("Nutzer.Liste");
    for(String s : freiwillige_spieler) {
    if (!users.contains(s)) {
    users.add(s);
    }
    }
    ArrayList<String>
     
  5. Offline

    Domi381

    Sry, but still same error

    Code:
    ArrayList<String> users = this.getstats().getList("Nutzer.Liste");
            for(Player p : freiwillige_spieler) {
                if (!users.contains(p.getName())) {
                    users.add(p.getName());
                }
            }
     
  6. Offline

    xXSniperzzXx_SD

    Domi381
    Change the type of list that is freiwillige_spieler to ArrayList<String>
     
  7. Offline

    Domi381

    But the error is in the first line, the thing with the (Player p : freiwillige_spieler) works, but not the getting from config

    Here ist the problem:

    Code:
    ArrayList<String> users = this.getstats().getList("Nutzer.Liste");
     
  8. Offline

    xXSniperzzXx_SD

    If you can use Player player : list
    then your saving the player data not the player's name
    and change this.getstats().getList
    to
    this.getstats().getStringList
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    a List<?> is not a List<String> and getList returns a List<?>. you may want to switch to getStringList if it is causing you too much trouble.
     
  10. Offline

    Domi381

    After a little bit of trying arround I got this:

    Code:
    List<?> users_list = this.getstats().getList("Nutzer.Liste");
         
            ArrayList<String> users = new ArrayList<String>();
            for(Object obj : users_list) {
                String s = (String) obj;
                users.add(s);
            }
         
            for(Player p : freiwillige_spieler) {
                String name = p.getName();
                if(!users.contains(name)) {
                    users.add(name);
                }
            }
            List<?> new_users_list = users;
            this.getstats().set("Nutzer.Liste", new_users_list);
    Im sure, it would have worked much easier, but now no errors are displayed anymore.
     
  11. Offline

    KillerOfPie

    Sorry for digging this up everyone, but i felt the need to make sure that the correct way to do this is posted on the solved page.

    Code:java
    1. List<String> users_list = this.getstats().getStringList("Nutzer.Liste");
    2.  
    3. ArrayList<String> users = new ArrayList<String>();
    4. for(String str: users_list) {
    5. users.add(str);
    6. }
    7.  
    8. for(Player p : freiwillige_spieler) {
    9. if(!users.contains(p.getName())) {
    10. users.add(p.getName());
    11. }
    12. }
    13.  
    14. this.getstats().set("Nutzer.Liste", users);


    If you are getting a List of strings from a config you want to use the getStringList() method which will reurn the type List<String> rather the List<?>. This was said a few times above and i don't know if you missed the posts or ignored them.
    Now the last post is the actual solution, and again I'm sorry for digging this up.

    *i know there are even more compressed ways of doing this, however this would get the point across
    **more compressed way:
    Code:java
    1. List<String> players = getConfig().getStringList("players");
    2.  
    3. if(!players.contains(player.getName()))
    4. players.add(player.getName());
    5. else
    6. players.remove(player.getName());
    7.  
    8. getConfig().set("players", players);
     
Thread Status:
Not open for further replies.

Share This Page