Solved YamlConfiguration, saving List<UUID>

Discussion in 'Plugin Development' started by Kassestral, May 17, 2015.

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

    Kassestral

    I am trying to save a list of UUID's however unless I do
    'configuration.set("members", String.valueOf(tribe.getMembers()));'
    then it saves !!Java.Util.UUID
    using the String.ValueOf, It presents the information as
    Code:
    leader: 0c9a2d2d-d3fe-4029-a069-411215841397
    members: '[0c9a2d2d-d3fe-4029-a069-411215841397]'
    Whereas I want it to save it in the format of
    Code:
    leader: 0c9a2d2d-d3fe-4029-a069-411215841397
    Members:
      - 0c9a2d2d-d3fe-4029-a069-411215841397
    
    How would I go about doing this?
     
  2. Offline

    ForsakenRealmz

    Kassestral likes this.
  3. 1) You have to use uuid.toString();
    2) If you have a uuid list you have to convert it to a string list :
    Code:
    //To save it
    List<String> strings = new ArrayList<>();
    for(UUID uuid : uuids) {
       strings.add(uuid.toString());
    }
    config.set("Members", strings);
    //To load it
    List<UUID> uuids = new ArrayList<>();
    for(String string : config.getStringList("Members")) {
       uuids.add(UUID.fromString(string));
    }
    
     
    Kassestral likes this.
  4. Offline

    1Rogue

    Or in java 8:

    Code:java
    1. config.set("members", uuids.stream().map(UUID::toString).collect(Collectors.toList()));
     
  5. Yeah something that a new dev could easy understand .
     
  6. Offline

    1Rogue

    Just the stream api. Takes about 10 minutes to learn.
     
  7. @MaTaMoR_ People who are new to Java shouldn't be posting here.
     
  8. "shouldn't" you said it .
     
  9. Offline

    Kassestral

    I should probably point out I am not new to Java, I came across the error because it was around 3-4am and I couldn't think clearly, when I woke up I immediately solved it with
    Code:
                List<String> members = new ArrayList<String>();
                for(UUID uuid : tribe.members){
                    members.add(uuid.toString());
                }
    And I only just got around to checking the forums again, but thanks for your time and help anyway guys

    @ForsakenRealmz @MaTaMoR_
    I only just got around to reading your comments, but thanks for the help, I solved it when I woke up (if you read my comment above)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page