Solved Best way to save/load HashMap?

Discussion in 'Plugin Development' started by Banjer_HD, Feb 10, 2018.

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

    Banjer_HD

    Hey there!

    I am making a plugin where I save information about a player in a class, that class will be held in a HashMap<UUID, MyPlayer> in the main class. I want this information to be saved when stoping the server, and loaded in my onEnable(), Is this the best way to do it? Should I load them and save them in onEnable and onDisable? Is it better to use SQL? How can you load and save the HashMap to a .yml file?

    MyPlayer.java:
    Code:java
    1. public class MyPlayer {
    2.  
    3. private UUID uuid;
    4. private String name;
    5.  
    6. public MyPlayer(UUID uuid) {
    7. this.uuid = uuid;
    8.  
    9. this.name = Bukkit.getOfflinePlayer(uuid).getName();
    10. }
    11.  
    12.  
    13.  
    14. public UUID getUUID() {
    15. return uuid;
    16. }
    17. public void setUUID(UUID uuid) {
    18. this.uuid = uuid;
    19. }
    20.  
    21.  
    22. public String getName() {
    23. return name;
    24. }
    25. public void setName(String name) {
    26. this.name = name;
    27. }
    28.  
    29. }


    Main.java:
    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. public HashMap<UUID, MyPlayer> MyPlayers = new HashMap<UUID, MyPlayer>();
    4.  
    5.  
    6. private static Main instance;
    7.  
    8.  
    9. public void onEnable() {
    10. instance = this;
    11.  
    12. }
    13.  
    14. public void onDisable() {
    15. instance = null;
    16. }
    17.  
    18.  
    19.  
    20. public static Main getInstance() {
    21. return instance;
    22. }
    23.  
    24.  
    25.  
    26. }


    Thanks for reading!
     
  2. Offline

    johnny_boy

    I guess you'd need to use a config, but I don't know how to do that (well enough), though there is this thread here. It explains how to make a config and add to it and get values from it. It's really useful. By reading the entirety of that you should be able to easily load and save your data when you start and stop the plugin.
     
  3. Offline

    Nostos

    why would you ever need to do this O.O
     
  4. Offline

    Banjer_HD

    I know how to make a custom yml file and ervery thing, but how to load and save hashmaps? I currently have it working using serializables, but dont know if this is the fastest/smartest thing to do. Thanks for your reply though!
     
    Last edited: Feb 10, 2018
  5. Offline

    johnny_boy

    Just convert the hashmap to a string then convert it back to a hashmap? I personally don't know how you'd do that but.. It seems logical.
     
  6. Offline

    Banjer_HD

    You would have to change the code every time you add a new feuture in the MyPlayer class, that is not so smart to do... (its worse then what I have working now) ill post my code asap when I get on a PC
     
  7. Offline

    timtower Moderator Moderator

  8. Offline

    Banjer_HD

    timtower likes this.
  9. Offline

    Machine Maker

    Please mark this thread as Solved if your issue has been resolved.
     
Thread Status:
Not open for further replies.

Share This Page