简体   繁体   中英

Is there any way to get around a class not implementing Serializable?

Ok, so I use Bukkit's ItemStack in a HashMap that is stored in a file and reloaded from it, but I found out that org.bukkit.craftbukkit.v1_4_6.inventory.CraftMetaItem which is used in the hash map created by ItemStack.serialize() is not implementing Serializable. Is there some way to get around this? Thanks!

EDIT:

Here is a snippit of code from my plugin. It is the part that raises the error.


public void onEnable(
{
public Map<NewItem, List<ItemStack>> items = new HashMap<NewItem, List<ItemStack>>();
//Add content to items...
}
public void onDisable()
{
try
{
Map<List<Map<String, Object>>, NewItem> smap = new HashMap<List<Map<String, Object>>, NewItem>();
for(NewItem item : items.keySet())
{
List<ItemStack> stacks = items.get(item);
List<Map<String, Object>> stacks2 = new ArrayList<Map<String, Object>>();
for(ItemStack stack : stacks)
{
stacks2.add(stack.serialize());
}
smap.put(stacks2, item);
}
SaveLoadAPI.save(smap, this.getDataFolder().getPath()+"\\Items.data");
}
catch (Exception e)
{
getLogger().severe("Was unable to save the items file to the default path of: "+this.getDataFolder().getPath()+"\\Items.data");
e.printStackTrace();
}
}

And here, last but certainly least, is the error!


16:08:52 [SEVERE] java.io.NotSerializableException: org.bukkit.craftbukkit.v1_4_
6.inventory.CraftMetaItem
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:08:52 [SEVERE] at java.util.HashMap.writeObject(Unknown Source)
16:08:52 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M
ethod)
16:08:52 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown S
ource)
16:08:52 [SEVERE] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
wn Source)
16:08:52 [SEVERE] at java.lang.reflect.Method.invoke(Unknown Source)
16:08:52 [SEVERE] at java.io.ObjectStreamClass.invokeWriteObject(Unknown S
ource)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeSerialData(Unknown So
urce)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeOrdinaryObject(Unknow
n Source)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:08:52 [SEVERE] at java.util.ArrayList.writeObject(Unknown Source)
16:08:52 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M
ethod)
16:08:52 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown S
ource)
16:08:52 [SEVERE] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
wn Source)
16:08:52 [SEVERE] at java.lang.reflect.Method.invoke(Unknown Source)
16:08:52 [SEVERE] at java.io.ObjectStreamClass.invokeWriteObject(Unknown S
ource)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeSerialData(Unknown So
urce)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeOrdinaryObject(Unknow
n Source)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:08:52 [SEVERE] at java.util.HashMap.writeObject(Unknown Source)
16:08:52 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M
ethod)
16:08:52 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown S
ource)
16:08:52 [SEVERE] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
wn Source)
16:08:52 [SEVERE] at java.lang.reflect.Method.invoke(Unknown Source)
16:08:52 [SEVERE] at java.io.ObjectStreamClass.invokeWriteObject(Unknown S
ource)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeSerialData(Unknown So
urce)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeOrdinaryObject(Unknow
n Source)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:08:52 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:08:52 [SEVERE] at com.tommy3244.plugins.MakeYourOwnBlocks.SaveLoadAPI.s
ave(SaveLoadAPI.java:16)
16:08:52 [SEVERE] at com.tommy3244.plugins.MakeYourOwnBlocks.MakeYourOwnBl
ocks.onDisable(MakeYourOwnBlocks.java:86)
16:08:52 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
in.java:219)
16:08:52 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin
(JavaPluginLoader.java:481)
16:08:52 [SEVERE] at org.bukkit.plugin.SimplePluginManager.disablePlugin(S
implePluginManager.java:400)
16:08:52 [SEVERE] at org.bukkit.plugin.SimplePluginManager.disablePlugins(
SimplePluginManager.java:393)
16:08:52 [SEVERE] at org.bukkit.plugin.SimplePluginManager.clearPlugins(Si
mplePluginManager.java:434)
16:08:52 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.reload(Craf
tServer.java:563)
16:08:52 [SEVERE] at org.bukkit.Bukkit.reload(Bukkit.java:184)
16:08:52 [SEVERE] at org.bukkit.command.defaults.ReloadCommand.execute(Rel
oadCommand.java:23)
16:08:52 [SEVERE] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
mmandMap.java:186)
16:08:52 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCom
mand(CraftServer.java:514)
16:08:52 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchSer
verCommand(CraftServer.java:506)
16:08:52 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedServer.al(Dedica
tedServer.java:260)
16:08:52 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedServer.r(Dedicat
edServer.java:225)
16:08:52 [SEVERE] at net.minecraft.server.v1_4_6.MinecraftServer.q(Minecra
ftServer.java:494)
16:08:52 [SEVERE] at net.minecraft.server.v1_4_6.MinecraftServer.run(Minec
raftServer.java:427)
16:08:52 [SEVERE] at net.minecraft.server.v1_4_6.ThreadServerApplication.r
un(SourceFile:849)

check out this post

If you try to extends the class that is not serializable, all the fields you inherit (and the fields that are private) will not be serialized. So, unfortunately, extending a class with a lot of fields is not an option.

if the object org.bukkit.craftbukkit.v1_4_6.inventory.CraftMetaItem is a field you could marki it as transient to avoid serialization, and if that doesn't solve the problem you could try and implement the write() and read() methods. Check out the API for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM