1
好的,所以我使用Bukkit的ItemStack存儲在一個文件中並從中重新加載,但是我發現org.bukkit.craftbukkit.v1_4_6.inventory.CraftMetaItem在ItemStack創建的哈希映射中使用。 serialize()沒有實現Serializable。有什麼方法可以解決這個問題嗎?謝謝!有沒有辦法繞過一個沒有實現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)
它看起來像你想從一個沒有實現Serializable接口的第三方庫的類中序列化一個對象。如果您在該類中使用此'org.bukkit.craftbukkit.v1_4_6.inventory.CraftMetaItem',則需要序列化,然後將其標記爲'transient',以避免對象序列化。如果您需要序列化任何術語,請嘗試製作此類的子項,並將子類標記爲Serializable,並在您的對象中使用子類。 –
嘗試製作兒童課程...... –
您可以發佈實際代碼的[SSCCE](http://sscce.org)以獲得更好的指導嗎? –