Saving/Loading a HashMap

Discussion in 'Plugin Development' started by thehutch, Feb 12, 2012.

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

    thehutch

  2. Offline

    edocsyl

  3. Offline

    Roadkill909

    You know... I think I may be one of the few developers that use the serializable interface. It has its cons, like backwards compatibility issues, but you don't have to go through String parsing hell to save your data.

    Hashmaps, and most Java API, is serializable. It takes about 10 lines to code it into your plugin.
     
  4. Offline

    hatstand

    Code:
    if(config.getConfigurationSection("titles") != null)
    {
        for(String s : config.getConfigurationSection("titles").getKeys(false))
        {
            Titles.put(s, config.getString("titles." + s));
        }
    }
    
    Code:
    for(Entry<String, String> entry : Titles.entrySet())
    {
        config.set("titles." + entry.getKey(), entry.getValue());
    }
    
     
  5. Offline

    Roadkill909

    I didn't read your second pastie... oops. Apologies for the rant. I've seen too many users will try to save vast clumps of data into a single text file.

    Edit: I must be missing something. Never heard of the SyncInventory class. If your own code, maybe knowing what's in it may help us solve the problem
     
  6. Offline

    vYN

    Haha i guess i can help you with this one xD
    User PrintWriter.

    Something like this: (i guess there is better ways to do this..)
    Code:
    static File sFile = new File(datafolder + "spawn.txt");
     
            try {
                pw = new PrintWriter(sFile);
                for (Entry<String, Location> spawn : Spawn.Spawn.entrySet()) {
                    pw.println(spawn.getKey() + "," + spawn.getValue() something something
                }
                pw.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
     
  7. Offline

    thehutch

    edocsyl
    Ok so I looked at your code but I don't see how I can change your method using String, Boolean to String SyncInventory

    Roadkill909
    SyncInventory is an object from a library made by Adamki11s because it allows you to serialize an onventory making it easy to store it in an HashMap

    vYN
    I don't quite understand what you're trying to write out in your code hmm i'll see what I can do :D
    also any method on loading it back into a HashMap?
     
  8. Offline

    Don Redhorse

    hmm you only save ONE object?

    oh, and if you already use SYNC why not use it to save the stuff?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  9. Offline

    thehutch

    Please teach me master :D

    Adamki11s
    Ok so I am using the SyncObjectIO to save a hashMap but I get this error

    Code:
    15:45:53 [SEVERE] java.io.EOFException
    15:45:53 [SEVERE]       at java.io.ObjectInputStream$PeekInputStream.readFully(U
    nknown Source)
    15:45:53 [SEVERE]       at java.io.ObjectInputStream$BlockDataInputStream.readSh
    ort(Unknown Source)
    15:45:53 [SEVERE]       at java.io.ObjectInputStream.readStreamHeader(Unknown So
    urce)
    15:45:53 [SEVERE]       at java.io.ObjectInputStream.<init>(Unknown Source)
    15:45:53 [SEVERE]       at couk.Adamki11s.IO.Objects.ObjectIOStream.read(ObjectI
    OStream.java:31)
    15:45:53 [SEVERE]       at couk.Adamki11s.IO.Objects.SyncObjectIO.read(SyncObjec
    tIO.java:81)
    15:45:53 [SEVERE]       at me.thehutch.creativelimit.CreativeLimitLoader.load(Cr
    eativeLimitLoader.java:21)
    15:45:53 [SEVERE]       at me.thehutch.creativelimit.CreativeLimit.setupHashMap(
    CreativeLimit.java:33)
    15:45:53 [SEVERE]       at me.thehutch.creativelimit.CreativeLimit.onEnable(Crea
    tiveLimit.java:19)
    15:45:53 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
    in.java:230)
    15:45:53 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
    JavaPluginLoader.java:1057)
    15:45:53 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
    mplePluginManager.java:380)
    15:45:53 [SEVERE]       at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftSe
    rver.java:199)
    15:45:53 [SEVERE]       at org.bukkit.craftbukkit.CraftServer.enablePlugins(Craf
    tServer.java:182)
    15:45:53 [SEVERE]       at net.minecraft.server.MinecraftServer.t(MinecraftServe
    r.java:357)
    15:45:53 [SEVERE]       at net.minecraft.server.MinecraftServer.a(MinecraftServe
    r.java:344)
    15:45:53 [SEVERE]       at net.minecraft.server.MinecraftServer.init(MinecraftSe
    rver.java:175)
    15:45:53 [SEVERE]       at net.minecraft.server.MinecraftServer.run(MinecraftSer
    ver.java:408)
    15:45:53 [SEVERE]       at net.minecraft.server.ThreadServerApplication.run(Sour
    ceFile:465)
    15:45:53 [SEVERE] Error occurred while enabling CreativeLimit v0.1 (Is it up to
    date?): null
    java.lang.NullPointerException
            at couk.Adamki11s.IO.Objects.ObjectIOStream.read(ObjectIOStream.java:40)
    
            at couk.Adamki11s.IO.Objects.SyncObjectIO.read(SyncObjectIO.java:81)
            at me.thehutch.creativelimit.CreativeLimitLoader.load(CreativeLimitLoade
    r.java:21)
            at me.thehutch.creativelimit.CreativeLimit.setupHashMap(CreativeLimit.ja
    va:33)
            at me.thehutch.creativelimit.CreativeLimit.onEnable(CreativeLimit.java:1
    9)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:230)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:1057)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:380)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:199)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:182
    )
            at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:357)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:344)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:175)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    
    And this is my Code for save/load:

    Code:java
    1.  
    2. public static void save(Map<String, SyncInventory> inv, File file) {
    3. SyncObjectIO stream = new SyncObjectIO(file);
    4. stream.add("playerInv", inv);
    5. stream.write();
    6. }
    7.  
    8. @SuppressWarnings("unchecked")
    9. public static HashMap<String, SyncInventory> load(File file) {
    10. SyncObjectIO stream = new SyncObjectIO(file);
    11. stream.read();
    12. HashMap<String, SyncInventory> inv;
    13. if (stream.doesObjectExist("playerInv")) {
    14. inv = (HashMap<String, SyncInventory>)stream.getObject("playerInv");
    15. return inv;
    16. }
    17. return null;
    18. }
    19.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  10. thehutch
    Hmm strange you are getting that. I never got that whilst testing.

    The EOF exception indicates that the file has reached the end of the stream unexpectedly. Perhaps you should initialise the hashmap first and then cast to it.

    If that does not work you could try adding the HashMap into a class and then serializing the whole class. If neither of these work let me know and I will do some more testing myself.
     
  11. Offline

    Firefly

    As I've stated before, the best way to save anything from a HashMap is to iterate all of the elements in the HashMap, and for each index store it in a YAML config file (presuming that the elements you are trying to save are Serializable). This is the easiest way I've found to save all of the ItemStacks in someone's inventory (what I needed for my plugin).
     
  12. Offline

    Don Redhorse

    lol.. I can make recommendations.. not more... you must learn yourself...

    leaving that aside... you asked the right guy after me :)
     
Thread Status:
Not open for further replies.

Share This Page