0

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)
Tom
  • 846
  • 5
  • 18
  • 30
  • 1
    It looks like you're trying to serialize an object from a class from a third party library that doesn't implement the `Serializable` interface. If you're using this `org.bukkit.craftbukkit.v1_4_6.inventory.CraftMetaItem` in the class you need to serialize then mark it as `transient` in order to avoid the serialization of the object. If by any term you need to serialize it, then try making a child of this class and mark the child class as Serializable and use the child class in your object. – Luiggi Mendoza Jan 21 '13 at 22:17
  • Tried making a child class.... – Tom Jan 21 '13 at 22:22
  • Could you post a [SSCCE](http://sscce.org) of your actual code in order to get better guidance? – Luiggi Mendoza Jan 21 '13 at 22:23

1 Answers1

0

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.

Community
  • 1
  • 1
fazhool
  • 121
  • 2