How to store HashMap into config.yml

Discussion in 'Plugin Development' started by wooethan, Nov 8, 2015.

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

    wooethan

    As the title says, How do I store my in progressing HashMap to my config.yml.

    Don't know bout putting those getConfig(); at onEnable() and onDisable(). But how do I store my progress 1 HashMap to the config.yml?

    Such as:
    Code:
    HashMap<String,String> progress1 = new HashMap<String, String>();
    
    if(player.getItemInHand().getType == Material.DIAMOND_SWORD){
    progress1.put(  player.getDisplayName(), "done");
    }
    Please lend me a helping hand.
     
  2. Offline

    Scimiguy

    You can't put a HashMap Directly into a config.yml file

    You can either serialize em and store the whole object in binary form, or store each key with their pair in your config

    So:
    for (String s : progress1.keySet()) {
    config.set("somepath.s", progress1.get(s)
    }
     
  3. Offline

    wooethan

    May I ask how to like I'm storing the hashmap to the config.yml if certain event done by the player?
     
  4. Offline

    Scimiguy

  5. Offline

    wooethan

    I get this in my config.yml
    Progress: 'no'

    It did the store the hashmap. But I think its not what I want for the hashmap.

    When the player in a certain circumstance typed no as it is added in my AsyncPlayerChatEvent. it will show no right here.

    How do I know what's the String s is ?

    And I got an error code.
    -Cannot load configuration from jar.
    -Top Level is not a Map.

    Code:
    HashMap <String, String> Redo = new HashMap<String, String>();
    
    //@EventHandler constructor{
    Redo.put(player.getDisplayName(), "no");
    for (String s : Redo.keySet()) {
                                getConfig().set("Progress", Redo.get(s));
                                }
    }
                            saveConfig();
     
    Last edited: Nov 8, 2015
  6. Offline

    Chloe-chan

    You can try to use my class I provided here, since both of your data types are serialisable. Also, it's beneficial to store the player's UUID instead of the name, as they can change their names over time. If you are wondering, UUID is indeed serialisable too.
     
  7. Offline

    wooethan

    @Chloe-chan
    I like the idea of using UUID but using utils is not preferably
     
  8. Offline

    Chloe-chan

    You can just copy the class I've provided, create a new class in your workspace, paste the code and edit the workspace. It's relatively easy to use anyways, so why not ? :D

    Ultimately, it's your code so I can't force you to use it, only give recommendations. :p
     
  9. Offline

    wooethan

    Then how check the config HashMap. If get certain config hashMap then cancel the event.

    like maybe getConfig()(check HashMap){
    evt.setCancelled(true);
    }

    And the previous code can't seems to work. It fixed the Top level is not a Map error. But everything goes back usual

    #Okay problem solved, The only thing is when I reload the plugin. All the HashMap data gone?
    Any idea?

    I want the config.yml be like

    Progress:
    playerUUID : (HashMap)

    Late update info:
    My config is already looked like on top. But I havent load the HashMap from configuration onEnable. Anyone know how?
     
    Last edited: Nov 9, 2015
  10. Offline

    Chloe-chan

    May I know how are you storing and loading your HashMap? If you are using my class, you can call the loadFromFile() method. Otherwise, you need to load from your custom config file under the onEnable() method.
     
  11. Offline

    mcdorli

    Please read my signature.
     
    Gonmarte and Zombie_Striker like this.
  12. Offline

    Scimiguy

    @mcdorli
    That implies they know Java.
    Given he couldn't figure out what "String s" is, in:
    for (String s : List<String>)

    I'd say it's unlikely
     
  13. Offline

    mcdorli

    For his excuse: I was first confused with this too, because in javascript it only was there, to not count nulls. So if the second place was a null, then it would be 0 1 3 4...
     
  14. Offline

    wooethan

    After all these config.yml

    Progress:
    1:
    UUID: 'done'

    how do I load the config files in onEnable?
    I already save the HashMap already but don't know how to load it.

    Anyone please help.

    BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 10, 2015
  15. Offline

    Scimiguy

    Use config#getKeys()

    Loop through that and add them to your hashmap, much the same way you stored them in the first place
     
  16. Offline

    wooethan

    @Scimiguy
    Like this?

    for(Player player : Bukkit.getOnlinePlayers()){
    for (UUID s : Redo.keySet()) {
    getConfig().get("Progress.1." + player.getUniqueId(), getConfig().getKeys(true));
    }
    }

    But I'm not using UUID s?
     
  17. Offline

    Scimiguy

    Uuid.fromstring

    Its easier for you to try it than it is for us to read your code every step, give it s try
     
  18. Offline

    wooethan

    @Scimiguy

    Can you explain it briefly? please

    Where to put the UUID.fromString(string)?

    I really need help (lack knowledge on Config)
     
  19. Offline

    Scimiguy

    Get the string from the config, and put it in the uuid,fromString()

    Typing on my phone, hard to detail
    Its not config stuff, this is java stuff
     
  20. Offline

    wooethan

    @Scimiguy
    How's this but I don't know how to get the String

    Code:
                for(UUID s:getConfig().getKeys(true)) {
                    String p = getConfig().getString(s);
                    Redo.put(s, p);
                    }
    Your help would be appreciated.
     
  21. Offline

    Scimiguy

    You're not storing the UUIDs as UUIDs in the config, you're storing them as strings.

    So you should be able to do what you're doing, but change UUID to String, and take out the true in getKeys().
    Then you need to put inside that loop a UUID.fromString() to turn your key from a String to a UUID, which you can then put inside your "Redo" map with Redo.put(UUID, String);
     
  22. Offline

    wooethan

    @Scimiguy
    take out the true in getKeys(). What should it put false or cancel the getKeys()?

    My code now is:
    Code:
    for(Player player : Bukkit.getOnlinePlayers()){
                for(String s:getConfig().getKeys()) {
                    String p = getConfig().getString(UUID.fromString(s));
                    Redo.put(s, p);
                    }
            }
     
  23. Offline

    Scimiguy

  24. Offline

    wooethan

    @Scimiguy

    The last thing that I don't understand is how to loop the UUID.fromString(s);
    Where should I put it?
    I can't put it in getConfig().getString(UUID.fromString(s));

    #Edit
    The codes in eclispe looks no error but after I load the plugin it cames out error with this code
    Code:
    for(String s:getConfig().getKeys(false)) {
                    String p = getConfig().getString(s);
                    Redo.put(UUID.fromString(s), p);
            }
     
    Last edited: Nov 10, 2015
  25. Offline

    Scimiguy

    By loop, I meant loop through your config Keys, which you are now doing.

    What's the error?
     
  26. Offline

    wooethan

    @Scimiguy
    The error code:
    Code:
    [15:35:20] [Server thread/ERROR]: Error occurred while enabling Quest v1.0 (Is it up to date?)
    java.lang.IllegalArgumentException: Invalid UUID string: Quest
        at java.util.UUID.fromString(Unknown Source) ~[?:1.7.0_76]
        at me.quest.Quest.onEnable(Quest.java:44) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at com.rylinaux.plugman.util.PluginUtil.load(PluginUtil.java:285) [PlugMan.jar:?]
        at com.rylinaux.plugman.command.LoadCommand.execute(LoadCommand.java:114) [PlugMan.jar:?]
        at com.rylinaux.plugman.PlugManCommandHandler.onCommand(PlugManCommandHandler.java:104) [PlugMan.jar:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_76]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_76]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-fdc1440-53fac9f]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_76]
    [15:35:21]
     
    Last edited: Nov 10, 2015
  27. Offline

    Scimiguy

    Well that error is basically saying that one of your strings that you're getting from your file is "Quest"
    You either need to make sure you're only getting the UUID keys somehow, or use a separate file to store the UUID stuff in
     
  28. Offline

    wooethan

    So what should I do?
    I deleted the file and reload again no errors but after the second reload. The error comes out again.

    my config.yml:
    Code:
    Progress:
      1:
        89618756-fbdc-3fbe-a1a6-a54a85b031de: 'no'
    
    my Code onEnable():
    Code:
    for(String s:getConfig().getKeys(false)) {
                    String p = getConfig().getString(s);
                    Redo.put(UUID.fromString(s), p);
            }
     
  29. Offline

    Scimiguy

    Well if you'd told us you weren't storing the UUIDs as a parent element, we would have been ok from the start...

    Show us the code you're using to store the UUIDs now
     
Thread Status:
Not open for further replies.

Share This Page