好的,所以我在存储在文件中的HashMap中使用Bukkit的ItemStack并从文件中重新加载,但是我发现org.bukkit.craftbukkit.v1_4_6.inventory.CraftMetaItem用于由ItemStack.serialize( )未实现Serializable。有什么办法可以解决这个问题?谢谢!

编辑:

这是我插件的代码片段。这是引发错误的部分。


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();
}
}


这是错误,最后但也是最不重要的一点!


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)

最佳答案

看看这个post

如果您尝试扩展无法序列化的类,则您继承的所有字段(以及私有字段)都不会被序列化。因此,不幸的是,扩展具有很多字段的类不是一种选择。

如果对象org.bukkit.craftbukkit.v1_4_6.inventory.CraftMetaItem是一个字段,则可以将其标记为瞬态以避免序列化,如果不能解决问题,则可以尝试实现write()和read()方法。查看API以获得更多信息。

关于java - 有什么办法可以解决不实现Serializable的类?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14448156/

10-10 02:04