Solved String List Unknown Issue

Discussion in 'Plugin Development' started by rohan576, Mar 25, 2014.

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


    I'll start off simply with the following code. It attempts to set the rank of a player by adding their name to a list in a YamlConfiguration.
    1. public static void setRank(String player, String rank) {
    2. YamlConfiguration yml = FileUtil.loadFile("ranks.yml");
    3. if (!yml.contains("mage")) {
    4. yml.createSection("mage");
    5. }
    6. if (!yml.contains("wizard")) {
    7. yml.createSection("wizard");
    8. }
    9. if (!yml.contains("warlock")) {
    10. yml.createSection("warlock");
    11. }
    12. List<String> mageList = yml.getStringList("mage");
    13. List<String> wizardList = yml.getStringList("wizard");
    14. List<String> warlockList = yml.getStringList("warlock");
    15. if (rank == "mage") {
    16. mageList.add(player);
    17. yml.set("mage", mageList);
    18. }
    19. else if (rank == "wizard") {
    20. wizardList.add(player);
    21. yml.set("wizard", wizardList);
    22. }
    23. else if (rank == "warlock") {
    24. warlockList.add(player);
    25. yml.set("warlock", warlockList);
    26. }
    27. else {
    28. System.out.println("[FreezeTag Ranks]: Unknown rank: " + rank);
    29. }
    30. FileUtil.saveFile(yml, "ranks.yml");
    31. }

    The only issue is that... it doesn't ever set the rank.
    Ex: setRank("rohan576", "mage")

    System.out.println Does indeed work.
    I am most certain that FileUtil.loadFile() and FileUtil.saveFile() work correctly.
    The sections are getting created successfully.
  2. Offline


    I'm fairly sure that the yaml.getStringList returns a List, not an ArrayList. Since you can't add to straight Lists, that code shouldn't even work... Also you're comparing Strings with == while you should be using .equals. Finally, you're making your ranks configuration sections, not string lists.

    Change (rank == "something") to ("something".equals(rank)).
    Change (yml.createSection("something")) to (yml.set("something", new ArrayList<String>())).
    Change (yml.getStringList("something")) to (new ArrayList<String>(yml.getStringList("something"))) (you may have to do a null check there or an isempty check.
  3. Offline


    Not really understanding your logic on the ArrayList part. I don't see any mention of ArrayList anywhere in the OP's code...
  4. Offline


    When did I ever mention ArrayLists? I've been using Lists.
    Actually, you can.
    This was the solution to my problem.
    If you weren't aware, it doesn't matter what it's set to before you set it.

    Problem solved! Thread marked accordingly.
Thread Status:
Not open for further replies.

Share This Page