Problem writing/reading hashmap file

Discussion in 'Plugin Development' started by peterarenot911, May 1, 2012.

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

    peterarenot911

    I am basically trying to save/load a hashmap counting the number of times a player has died. I have tried using the SLAPI api from the wiki and also the one I am currently using. The hashmap works as it should but just wont save or load.

    Here is the source if anyone can check what I am doing wrong!

    Many thanks, Peter :confused:
     
  2. Offline

    AmoebaMan

    Maybe I'm just blind, but I don't see a HashMap anywhere in that source...

    Anyways, I think I know what your problem is. If you're using that map as a HashMap<Player, Integer>, then you can't save it. The reason why is because the S/L-API can only save objects that are fully serializable - that is, they implement the Serializable interface. Players (and indeed, most objects/interfaces in the Bukkit API) do not do this, and so you can't save them to a File.

    The basic solution to this is to instead us a HashMap<String, Integer>, and simply store the player's name rather than the object itself.
     
  3. Offline

    peterarenot911

    The hashmap already is a <String, String> one because I knew about that problem, the saving class is in a separate package, not the main one which is where the hashmap is. I should have mad that clear, sorry.

    Hashmap saving/loading class
     
  4. so does it not save or does it not load? If the first: try reader.flush() before reader.close()
     
  5. Offline

    peterarenot911

    flush() is not a method for java.io.BufferedReader

    And it does not do either. I cant get it to read from the file if I add data first or write to it.
    Is there a better method?
     
  6. Sorry, my fault. out.flush() before out.close() :)
     
  7. Offline

    AmoebaMan

    You don't need to store the death count as a String. Integer is a Serializable wrapper class for the int rawtype (because rawtypes can't be used in maps or lists), and it functions in exactly the same way.
     
  8. Offline

    Kierrow

  9. Offline

    peterarenot911

    I know, I tried string because integer did not work and I am covering my bases. However that is not the problem. It still wont save or load the file. Any more ideas?
     
  10. Offline

    AmoebaMan

    Alright, let's see here:
    1. Use a HashMap<String, Integer>. As I mentioned, Integers are serializable, and it's a pointless waste of time to convert between Strings and Integers.
    2. In your ListStore constructor - NEVER use something like storageFile.exists() == false. I don't know why, but it tends to cause errors. Instead, just use !storageFile.exists().
    3. If it's not necessary to read the death list in the file itself, you should just use an ObjectOutputStream and ObjectInputStream to save and load. They are much faster and easier to use, but the file text won't be legible.
    Also, I don't think you ever pasted the actual error you got. Showing me that would help immensely.
     
  11. Offline

    ItsHarry

    Explain more detailed what you mean with "wont save or load."
     
  12. NEVER? You can use it, it's the same. If it gives you a different output then you probably did something else wrong because they're the same. Still, using '!' instead of '== false' is better coding practice.
     
  13. Offline

    AmoebaMan

    I agree, it should be the same. I'm just saying that once or twice when I was beginning coding that turned out to be the source of a bug. I have no idea how, it just was.
     
Thread Status:
Not open for further replies.

Share This Page