Solved Save/Load strings to/from file?

Discussion in 'Plugin Development' started by recon88, Nov 23, 2012.

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

    recon88

    Alright. I had a long pause in plugin development...
    Trying to figure out how I can handle this stuff for a working file-based friendlist-system.
    Already wrote a plugin with a SQL-based friendlist but that's not what I need now.

    I got:
    - Playername (string)
    - Newfriend (string)
    - 2 commands (add and remove)

    Code:java
    1.  
    2. if(command.getName().equalsIgnoreCase("add") && (args.length == 1)) {
    3. String playername = player.getName();
    4. String newfriend = args[0].toLowerCase();
    5.  
    6. // Check if "newfriend" exist on the friendlist of "playername".
    7. // If exist -> sendmessage
    8. // Else -> add "newfriend"
    9. // Save that stuff to a file (immediately)
    10. }
    11.  

    I want to save that stuff immediately to the friendlist file (one file for everything would be nice).
    Example:
    Code:
    recon;friend1,friend2,friend3
    peter;friend1,friend2
    How can I turn that into a working command?
     
  2. Offline

    Jogy34

    Something like this might work:
    Code:
    if(this.getConfig().contains(playername + ".Friends"))
    {
        List<String> friends = this.getConfig().getStringList(playername + ".Friends")
        if(friends.contains(newfriend)
        {
            //send Message
        }
        else
        {
            friends.add(newfriend);
            this.getConfig().set(playername  + ".Friends", friends);
            this.saveConfig();
        }
    }
    else
    {
        String[] friends = {newfriend};
        this.getConfig().set(playername  + ".Friends", Arrays.asList(friends));
        this.saveConfig();
    }
     
  3. Offline

    recon88

    Wow... I didn't thought about using the yaml config *facepalm*.
    I was about to use the BufferedWriter of Java itself and stuff like that.
    Thanks for rescuing me =)
     
Thread Status:
Not open for further replies.

Share This Page