Save ArrayList

Discussion in 'Spigot Plugin Development' started by ThePabloDev, May 15, 2016.

  1. Hello spigot users, i'm writing this post because i want to say how can i save ArrayList in a config file or MySql.. And after load it.

    ArrayList -
    Code (Text):
    public static ArrayList<String> IT = new ArrayList<String>();
    Item to give on join
    Code (Text):
    public void onJoin(PlayerJoinEvent e) {
    if(ArrayLists.IT.contains(p.getName())) {
        ItemMeta BackpackMeta = Item.Backpack.getItemMeta();

        p.getInventory().setItem(8, Item.Backpack);
       } else {
        p.getInventory().setItem(8, Item.Error);
    Code work, but when i restart or reload the server the player exits from the array.. And It isn't longer given the item
    #1 ThePabloDev, May 15, 2016
    Last edited: May 15, 2016
  2. You need to save the ArrayList to file... -_-
  3. Yeah but i ask before "how" not "What i have to do"..
  4. Creeoer


    Serializing an array list is dirt easy with the configuration api, literally all you have to do is config.set("" , list);

    And to get it back, config.getList(""); Couldn't you have googled that??
    • Agree Agree x 1
  5. Pretty sad because over a dozen people answered this on Bukkit's forums, on threads from years ago.
  6. Hi Pablo,
    You could save it over an ObjectOut/InputStream
    Could be an alternative to the config.
    Code (Text):
    public static <T extends Object> T load(File path) throws Exception
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
            T result = (T)ois.readObject();
            return result;
    Code (Text):
    public static <T extends Object> void save(T obj, File path) throws Exception
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));