Error with hashmap type player NotSerializableException

Discussion in 'Plugin Development' started by xlilcasper, Jun 5, 2011.

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

    xlilcasper

    I am very new to java and am having an issue. I was trying to save a hashmap<player,long> to a file and I keep getting an error. Here is the error.
    Code:
    2011-06-05 15:44:51 [SEVERE] java.io.NotSerializableException: org.bukkit.craftbukkit.entity.CraftPlayer
    2011-06-05 15:44:51 [SEVERE]     at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at java.io.ObjectOutputStream.writeObject(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at java.util.HashMap.writeObject(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2011-06-05 15:44:51 [SEVERE]     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at java.lang.reflect.Method.invoke(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at java.io.ObjectOutputStream.writeObject(Unknown Source)
    2011-06-05 15:44:51 [SEVERE]     at com.oberonserver.timerank.timerank.savePlaytime(timerank.java:129)
    2011-06-05 15:44:51 [SEVERE]     at com.oberonserver.timerank.TimeRankPlayerListener.onPlayerQuit(TimeRankPlayerListener.java:30)
    2011-06-05 15:44:51 [SEVERE]     at org.bukkit.plugin.java.JavaPluginLoader$2.execute(JavaPluginLoader.java:250)
    2011-06-05 15:44:51 [SEVERE]     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
    2011-06-05 15:44:51 [SEVERE]     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:310)
    2011-06-05 15:44:51 [SEVERE]     at net.minecraft.server.ServerConfigurationManager.disconnect(ServerConfigurationManager.java:145)
    2011-06-05 15:44:51 [SEVERE]     at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:611)
    2011-06-05 15:44:51 [SEVERE]     at net.minecraft.server.NetworkManager.b(NetworkManager.java:222)
    2011-06-05 15:44:51 [SEVERE]     at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:75)
    2011-06-05 15:44:51 [SEVERE]     at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
    2011-06-05 15:44:51 [SEVERE]     at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:401)
    2011-06-05 15:44:51 [SEVERE]     at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:311)
    2011-06-05 15:44:51 [SEVERE]     at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    
    I was looking at the "Huge Plugin Tutorial" and trying to save the hashmap that way. Here is the save code for the hashmap.
    Code:
    public static void savePlaytime()
        {
         try{
          ObjectOutputStream hashfile = new ObjectOutputStream(new FileOutputStream(times.getPath()));
          hashfile.writeObject(PlayTime);
          hashfile.flush();
          hashfile.close();
          //Something went wrong
          }catch(Exception e){
           e.printStackTrace();
          }
        }
    
    Is there some other way I should be doing this?
     
  2. Offline

    Raphfrk

    The problem is that the Player class is not tagged as Serializable. There isn't really anything you can do about it as it would require a change to the Bukkit class.

    You could submit a pull request with "implements Serializable" added to the Player class.

    The idea is that it lets authors decide if their objects should be serializable.
     
  3. Offline

    Shamebot

    Would be pretty hard to implement, since player probably has a lot of references to objects which aren't serializable too, wouldn't it? Maybe make them transient and get new ones from Bukkit.getServer().
     
  4. Offline

    marinating

    I ran into this problem and just made the hashmap into a <string,long>, then use getServer().getPlayer(string); to grab the player when I need it.
     
  5. Offline

    Dreadreaver

    Is a good approach. But I think this would be better: Use the HashMap as <Player, long> but save it as <player.getName(), Long> and load it as <getServer.getPlayer(name), Long>. That way you dont have to change anything of your plugin except for the saving to file.
     
  6. Offline

    marinating

    That works too.
     
Thread Status:
Not open for further replies.

Share This Page