Solved How do you save Hashmaps to a file

Discussion in 'Plugin Development' started by fireboyev, Sep 4, 2016.

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

    fireboyev

    Hey all, How would i go about saving a hashmap like this:
    Code:
    public static HashMap<UUID, Boolean> isFrozen = new HashMap<UUID, Boolean>(); 
    to a file like players.yml, I already know how to create the file, I just want to know how I would save the Hashmap.

    I Only need to save the UUID and then when I load the hashmap, put the uuid in the hashmap with the boolean: true
     
  2. Offline

    kameronn

    Easy, just do
    Code:
    config.set(player.getUnqiueID() + ".isFrozen", isFrozen.get(player.getName()));
    
    //That would save the players boolean to the config, and on load up or when you need to get the boolean do
    
    isFrozen.put(player.getUniqueID(), config.getBoolean(player.getUniqueID() + ".isFrozen"));
    
    // This puts a the boolean that you saved into the config into the actual hashmap. You shouldnt save the actually hashmap but you should save the data in the hashmap
    Also it really wouldnt recommend saving UUIDs, instead save strings. From personal experience, it will cause servers to lag much more then it is needed to, also make sure to remove them from the hashmap when they leave, you can also save the data to the config there too. Also on server disable you also might wanna do isFrozen.clear(); just incase you're doing a reload.

    Yeah, good luck
     
  3. Offline

    fireboyev

    I don't really care about the getting the boolean, I care about getting and saving the uuid (and yes I meant as String) and I only want to save onDisable() and load onEnable()
     
  4. Offline

    kameronn

    @fireboyev
    then just save their uuid into the config, check for it and then put it into the hashmap
     
  5. Offline

    DoggyCode™

    UUID is a object. String is a object. They are not bigger or smaller than each other. Point? All he has to do is save it to the file as a string, then use UUID.fromString(...) to get it back to UUID. Simple and I actually recommend it. And to get from UUID to string just use the toString() method

    Sent from my SM-N9005 using Tapatalk
     
  6. Offline

    fireboyev

    Okay saving and getting works now, but how do I remove the string from the string list?
    Code:
    List<String> thing =
    StaffGui.plugin.getConfig().getStringList("players");
                        thing.remove(savedplayer.getUniqueId().toString());
    that is what I am using to remove but it doesn't want to remove the uuid from the list in the config

    my config looks like this:
    Code:
    players:
    - f0fac4c7-1a9e-43bd-9df8-40ac766042fa
    -EDIT-

    Nevermind I got help from a friend

    -SOLVED-
     
    Last edited: Sep 5, 2016
  7. Offline

    Zombie_Striker

    @fireboyev
    You have to re-set the list after you remove it. When you get the list from the config, you are getting a clone of the values. use Config.set() to update the list.
     
  8. Offline

    kameronn

    @DoggyCode™
    Reason I say this is because im pretty sure storing a UUID stores more data then you may actually need, in his case he can just get the player and compare it, so the function "UUID.fromString". I dont see much of a point of saving a converting a UUID to string then when you're getting it from string to UUID when you can just save it as a string and then compare it

    Also, from personal experience it seemed to lag more when I did use a UUID
     
  9. Offline

    DoggyCode™

    Simply because the UUID object is a thing.

    Sent from my SM-N9005 using Tapatalk
     
  10. @kameronn You sure it was from UUIDS? Almost all the plugins on my server use UUIDs, and do not lagg
     
  11. Offline

    DoggyCode™

    I'm 100% it wasn't. It's a reason you operate with the Player object and not the player name. Same with UUID, you should operate with the UUID object, not the String itself (only when you save it to files and stuff you should use the String, but the moment you're gonna use it in your code, take the String from the file and only use the UUID object.). Afterall, it's a reason they made it.
     
    TheEnderCrafter9 likes this.
Thread Status:
Not open for further replies.

Share This Page