Solved Need help with getconfig().set() to set a list

Discussion in 'Spigot Plugin Development' started by Dori_mon, Jun 16, 2016.

  1. Code (Text):
    package me.Dori_mon.RebulionReport;

    import java.util.Arrays;
    import java.util.List;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
       
        public void onEnable() {
            System.out.println("");
            System.out.println("");
        }
       
        public void onDisable() {
            System.out.println("");
            System.out.println("");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                if (label.equalsIgnoreCase("report")) {
                    if (args.length == 0) {
                        sender.sendMessage("/report <player> <reason>");
                    } else if (args.length == 1) {
                        sender.sendMessage("/report <player> <reason>");
                    } else if (args.length == 2) {
                        List<String> reports = Arrays.asList();
                        reports.add("Player: " + args[0] + " Reason: " + args[1] + " Reporter: " + sender.getName());
                        getConfig().set("reports", reports);
                        saveConfig();
                        sender.sendMessage(getConfig().getString("successful-report"));
                    }
                }
            }
           
            return false;
        }

    }
     

    The problem is that in the config, instead of creating more lines. it changing the same line. How can I fix that?
     
  2. Choco

    Moderator

    Useless :p You can remove these

    You're creating a new list every time. Use getConfig().get("PathToYourList"), modify and set that instead to add to it.
     
  3. It's because you're overwriting the list each time with a new list. Instead try something like:

    Code (Text):

    ArrayList<String> reports = (ArrayList<String>)getConfig().getList("reports");
    if(reports == null) reports = new ArrayList<String>();
    reports.add("Player: " + args[0] + " Reason: " + args[1] + " Reporter: " + sender.getName());
    getConfig().set("reports", reports);
    saveConfig();
     
     
    • Like Like x 2
  4. Thank you <3 solved it!
     
  5. You can just use getStringList(path) instead of casting...

    And also, dont think the getStringList returns null, it will just return an empty list, (I believe)
     
    • Like Like x 1
    • Agree Agree x 1
  6. Very possible that it does return an empty list. I wasn't sure so better safe than sorry.
     
  7. Ahh it looks like getStringList() will return an empty list, but getList() will return null.
    [​IMG]
     
  8. You should use generics anyway :p