Solved YAML - Storing keys w/o values

Discussion in 'Plugin Development' started by Unica, Jan 28, 2015.

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

    Unica

    Helloo.

    Is it possible to store 'single worded' data in a YamlConfiguration?
    I want to store just a list of names. Like;

    Code:
    Notch
    Unica
    Jeb_
    Someone
    PlayerA
    Etcetera
    If so, how would I 'get' the data / remove the data?
    Or should I use .txt files? I am open for different options :)
     
  2. Offline

    PreFiXAUT

    @Unica You can. Simply set a List in the "set(path, object)" of your Config and get the List with "getStringList(path)" again
     
  3. Offline

    mythbusterma

    @Unica

    Just put a tag in front of them like "names:" it's really not that much more difficult.
     
  4. Offline

    Unica

    @mythbusterma

    I know.
    But I was wondering if it was possible to have it plain under eachother due
    inserting names outgame (copy - pasting into the config)
     
  5. Offline

    WinX64

    I'm almost sure this is the closest you will get to what you want(unless you use something else other than yaml):
    [​IMG]
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Unica You can ditch the YML for this and use streams instead
     
    DemKazoo likes this.
  7. Offline

    Unica

    @timtower

    I have to admit I didn't really do something with streams in the past.
    Any links for tutorials / directions?



    @WinX64
    I am choosing for that option :p
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    Unica

    @timtower

    Thanks, I've tried some things, and nearly everything works, except the proper removing of Player names.
    Like, the player name does get removed, but the .txt file doesn't get 're-ordered'.
    I've tried googling some answers, but I didn't get the correct answer I was looking for. Any thoughts?

    Example
    Code:
    playera
    playerb
    playerc
    playerd
    playere
    notch
    If I were to remove player a, the config would be;

    Code:
    playerb
    playerc
    playerd
    playere
    notch
    Problem:
    I can't use 'list.get(0)' because the first element got removed.
    Is there a way to either;
    - Get the first String in a .txt file
    - Re-order the .txt file so the 'get(2)' becomes 'get(1)' -> 'get(1)' becomes 'get(0)' etc.



    Code
    Code:
    public static void removeFromFile(String name, File mainFolder, String aFilename) throws IOException{
            File file = new File(mainFolder+"/"+aFilename.toLowerCase()+".txt");
            File temp = File.createTempFile(aFilename.toLowerCase(), ".txt", file.getParentFile());
            String charset = "UTF-8";
            String delete = name;
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
            for (String line; (line = reader.readLine()) != null;) {
                line = line.replace(delete, "");
                writer.println(line);
            }
            reader.close();
            writer.close();
            file.delete();
            temp.renameTo(file);
        }
    Like,
    if I set a value to null in a YamlConfiguration, the entire file just get's moved one up. With
    this it will become a blank line.

    EDIT:

    I currently did
    Code:
    for(int i = 0; i < list.size(); i++){
           if(list.get(i) != null && !list.get(i).equals("")){
                  string= list.get(i);
                  break;
           }
    }
    And that will give me the first actual String of the .txt file.
    But I am not sure if that is a efficient way.

    SOLVED:

    Code:
    [/B]list.removeAll(Collections.singleton(""));[B]


    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 28, 2015
  10. Offline

    timtower Administrator Administrator Moderator

    @Unica If you remove the first element in a list then it will be filled with the item at 1, 1 will be replaced by 2 etc.
    And the order is something that you need to do yourself.
     
Thread Status:
Not open for further replies.

Share This Page