reading/writing and getting a list from a custom file

Discussion in 'Plugin Development' started by thehutch, Nov 5, 2011.

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

    thehutch

    Ok so i'm having some extreme difficulty on obtaining a list (idk which list to use Array or standard list) from a file (No idea which method to use (scanner, objectStream ...etc)) and then adding a String to this list, atm my method is using the YamlConfiguration which does generate the files but for some reason cannot obtain the list from it here is what I have:
    Getting the list:
    Code:
        @SuppressWarnings("unchecked")
        public List<String> getFriendList() {
            try {
                YamlConfiguration file = new YamlConfiguration();
                file.load(new File(plugin.getDataFolder() + File.separator + "friends.yml"));
                return file.getList("friends");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    saving the list:
    Code:
        public void saveFriends(YamlConfiguration file) {
            try {
                file.save(new File(this.getDataFolder() + File.separator + "friends.yml"));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    and a use of where this is used:
    Code:
        /**
         * Updates your friends insert true
         * or false to add/remove friends
         * @param friend
         * @param user
         * @param remove
         * @return
         */
        public boolean updateFriends(String friend, String user, boolean remove) {
    
            YamlConfiguration file = new YamlConfiguration();
            List<String> friends = utils.getFriendList();
     
            if (remove == false) {
                friends.add(friend);
            } else if (friends.contains(friend)){
                friends.remove(friend);
            }
            plugin.saveFriends(file);
            return false;
        }
    I did test the saving file and that does work. Also there are no errors when I run this plugin but no of the commands work and this updateFriends() method is used in that
    All help is appreciated thank you
     
  2. I think you might have to pay attention to that warning.

    Replace String with ? in the return type of the getFriendsList() method:
    Code:
    public List<?> getFriendList()
    Do the same for the friends variable in method updateFriends().

    All you have to do is convert to String when you get a friend.
    Code:
    friends.get(1).toString();
     
Thread Status:
Not open for further replies.

Share This Page