Solved How can I load an arraylist from a .yml?

Discussion in 'Spigot Plugin Development' started by Jabi_, Jul 30, 2021.

  1. The title says all.

    I've tried doing this but it didnt works
    Code (Java):
    ArrayList<String> nicks = new ArrayList<String>();
    public void LoadList() {
            final FileConfiguration players = this.plugin.getPlayers();
            nicks.addAll(players.getStringList("Nicks."));
        }
     
        public void OnEnable() {
            this.LoadList();
        }
     
    #1 Jabi_, Jul 30, 2021
    Last edited: Jul 30, 2021
  2. try changing
    Code (Java):
    nicks.addAll(players.getStringList("Nicks."));
    to
    Code (Java):
    nicks.addAll(players.getStringList("Nicks"));
    If that doesn't work, please also provide what the config looks like.
     
  3. Nicks(dot) is not going to be valid.

    Whats your config look like?
     
  4. Looks like:
    Code (YAML):
    Nicks:
      - Jabi_
      - Anothernick...
     
  5. You could try this solution

    Code (Java):
        ArrayList<String> nicks = new ArrayList<String>();

        public void LoadList() {
            final FileConfiguration players = this.plugin.getPlayers();
            List<String> list = players.getStringList("Nicks");

            for (int i = 0; i < list.size(); i++) {
                String name = (String) list.get(i);
                nicks.add(name);
            }
        }
     
  6. Try doing:
    Code (Java):
        List<String> nicks = new ArrayList<>();

        public void loadList() {
            final FileConfiguration configuration = this.plugin.getPlayers();
            List<String> list = configuration.getStringList("Nicks");
            nicks.addAll(list);
        }
    If you are getting a NullPointerException, then please post your main class.
     
  7. Thats good!
     
  8. didn't work... ill post the class. Im sure that i did something wrong
    Code (Java):
    public class Ip implements Listener
    {

        private Alts plugin;
     
        public Ip(final Alts plugin) {
            this.plugin = plugin;
        }
        List<String> nicks = new ArrayList<>();

        public void loadList() {
            final FileConfiguration configuration = this.plugin.getPlayers();
            List<String> list = configuration.getStringList("Nicks");
            nicks.addAll(list);
        }
     
        public void OnEnable() {
            this.loadList();
        }
     
        @EventHandler
        public void OnPlayerJoin(final PlayerJoinEvent event) {
            final Player player = event.getPlayer();
            final FileConfiguration players = this.plugin.getPlayers();
            if(!nicks.contains(player.getName())) {
                players.set("Nicks", nicks);
                nicks.add(player.getName());
                this.plugin.savePlayers();
                if(players.getString("Alts."+player.getAddress().getAddress().getHostAddress()+".Account") == null) {
                    players.set("Alts."+player.getAddress().getAddress().getHostAddress()+".Account", player.getName());
                    this.plugin.savePlayers();
                    }else if(players.getString("Alts."+player.getAddress().getAddress().getHostAddress()+".Account2") == null){
                    players.set("Alts."+player.getAddress().getAddress().getHostAddress()+".Account2", player.getName());
                    this.plugin.savePlayers();
                    }else if(players.getString("Alts."+player.getAddress().getAddress().getHostAddress()+".Account3") == null) {
                        players.set("Alts."+player.getAddress().getAddress().getHostAddress()+".Account3", player.getName());
                        this.plugin.savePlayers();
                    }else if(players.getString("Alts."+player.getAddress().getAddress().getHostAddress()+".Account4") == null) {
                        players.set("Alts."+player.getAddress().getAddress().getHostAddress()+".Account4", player.getName());
                        this.plugin.savePlayers();
                    }else if(players.getString("Alts."+player.getAddress().getAddress().getHostAddress()+".Account5") == null) {
                        players.set("Alts."+player.getAddress().getAddress().getHostAddress()+".Account5", player.getName());
                        this.plugin.savePlayers();
                    }else {
                        player.kickPlayer("Too many alts");
                    }
            }
        }
    }
     
  9. also didnt works
     
  10. Your enable method doesn't match the correct name which is "onEnable"
     
  11. fixed but isnt working
     
  12. i have found a solution but not for the load, i have changed the
    Code (Java):
    if(!nicks.contains(player.getName()))
    to
    Code (Java):
    if(!players.getStringList("Nicks").contains(player.getName()))

    should I mark the thread as solved?
     
  13. is that onEnable method executed?
     
  14. First, can you describe what "getPlayers()" is ? I don't find this method on spigot javadoc and you didn't send the class "Alts"... Well I'm not sure the issue comes from that.


    Why do you use onEnable method in a class that doesn't extends JavaPlugin ? Please read tutorials to know how to initialize plugins and listener in your plugin. onEnable method won't be executed at all except if you execute it in another class (where you register the listener). If you want to load the list when the class is registered, call loadList() in your constructor like that :
    Code (Java):
    public class Ip implements Listener {

        private Alts plugin;
        List<String> nicks = new ArrayList<>(); // convention : put fields above constructors and methods

        public Ip(final Alts plugin) {
            this.plugin = plugin;
            loadList();
        }

        public void loadList() {
            final FileConfiguration configuration = this.plugin.getPlayers();
            nicks.addAll(configuration.getStringList("Nicks"));
        }

        @EventHandler
        public void OnPlayerJoin(final PlayerJoinEvent event) {
            // ...
        }
    }

    It's not really beautiful to see : a lot of copy paste, you can easily make a loop instead of this spaghetti code.
     
  15. here the alts class, getPlayers() is in
    Code (Java):

    public class Alts extends JavaPlugin
    {
        public String rutaplayers;
        PluginDescriptionFile pdffile;
        public String version;
        public String nombre;
        private FileConfiguration players;
        private File playersFile;
       
        public Alts() {
            this.pdffile = this.getDescription();
            this.version = this.pdffile.getVersion();
            this.nombre = this.pdffile.getName();
            this.players = null;
            this.playersFile = null;
        }

        public void onEnable() {
            this.registerConfig();
            this.registerEvents();
            this.registrarComando();
            this.registerPlayers();
        }
       
        public void registrarComando() {
            this.getCommand("alts").setExecutor((CommandExecutor)new ComandoPrincipal(this));
        }
       
        public void registerConfig() {
            final File Config = new File(this.getDataFolder(), "config.yml");
            this.rutaplayers = Config.getPath();
            if (!Config.exists()) {
                this.getConfig().options().copyDefaults(true);
                this.saveConfig();
            }
        }
       
        public void registerEvents() {
            final PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents((Listener)new Ip(this), (Plugin)this);
        }
       
        public FileConfiguration getPlayers() {
            if (this.players == null) {
                this.reloadPlayers();
            }
            return this.players;
        }
       
        public void reloadPlayers() {
            if (this.players == null) {
                this.playersFile = new File(this.getDataFolder(), "players.yml");
            }
            this.players = (FileConfiguration)YamlConfiguration.loadConfiguration(this.playersFile);
            try {
                final Reader defConfigStream = new InputStreamReader(this.getResource("players.yml"), "UTF8");
                if (defConfigStream != null) {
                    final YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    this.players.setDefaults((Configuration)defConfig);
                }
            }
            catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
       
        public void savePlayers() {
            try {
                this.players.save(this.playersFile);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
       
        public void registerPlayers() {
            this.playersFile = new File(this.getDataFolder(), "players.yml");
            if (!this.playersFile.exists()) {
                this.getPlayers().options().copyDefaults(true);
                this.savePlayers();
            }
        }
    }

     
     
  16. Seems to work fine. Check my second paragraph !
     
  17. This isn't a good idea, you should be saving StringList to a variable rather than accessing it from the config every time.
     
    • Agree Agree x 1
  18. This is my first plugin, and my first time using java therefore its posible to see that things in my code.

    idk how to do this im a begginer
     
  19. Code (Java):
    List<String> nicks = players.getStringList("Nicks");
     

  20. Code (Java):

    List<String> nicks;
        public void LoadList() {
            final FileConfiguration players = this.plugin.getPlayers();
            nicks = players.getStringList("Nicks");
        }

        public void OnEnable() {
            this.LoadList();
        }
    should work