好的,对于我的Bukkit插件,我需要以下java类(NewItem.java)可序列化,但事实并非如此。当我的插件试图保存它时,它会给我错误:
16:33:23 [SEVERE] java.io.NotSerializableException: org.bukkit.inventory.ItemSta
ck
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:33:23 [SEVERE] at java.util.ArrayList.writeObject(Unknown Source)
16:33:23 [SEVERE] at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown
Source)
16:33:23 [SEVERE] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
wn Source)
16:33:23 [SEVERE] at java.lang.reflect.Method.invoke(Unknown Source)
16:33:23 [SEVERE] at java.io.ObjectStreamClass.invokeWriteObject(Unknown S
ource)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeSerialData(Unknown So
urce)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeOrdinaryObject(Unknow
n Source)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:33:23 [SEVERE] at java.util.HashMap.writeObject(Unknown Source)
16:33:23 [SEVERE] at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown
Source)
16:33:23 [SEVERE] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
wn Source)
16:33:23 [SEVERE] at java.lang.reflect.Method.invoke(Unknown Source)
16:33:23 [SEVERE] at java.io.ObjectStreamClass.invokeWriteObject(Unknown S
ource)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeSerialData(Unknown So
urce)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeOrdinaryObject(Unknow
n Source)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:33:23 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:33:23 [SEVERE] at com.tommy3244.plugins.MakeYourOwnBlocks.SaveLoadAPI.s
ave(SaveLoadAPI.java:16)
16:33:23 [SEVERE] at com.tommy3244.plugins.MakeYourOwnBlocks.MakeYourOwnBl
ocks.onDisable(MakeYourOwnBlocks.java:60)
16:33:23 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
in.java:219)
16:33:23 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin
(JavaPluginLoader.java:481)
16:33:23 [SEVERE] at org.bukkit.plugin.SimplePluginManager.disablePlugin(S
implePluginManager.java:400)
16:33:23 [SEVERE] at org.bukkit.plugin.SimplePluginManager.disablePlugins(
SimplePluginManager.java:393)
16:33:23 [SEVERE] at org.bukkit.plugin.SimplePluginManager.clearPlugins(Si
mplePluginManager.java:434)
16:33:23 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.reload(Craf
tServer.java:563)
16:33:23 [SEVERE] at org.bukkit.Bukkit.reload(Bukkit.java:184)
16:33:23 [SEVERE] at org.bukkit.command.defaults.ReloadCommand.execute(Rel
oadCommand.java:23)
16:33:23 [SEVERE] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
mmandMap.java:186)
16:33:23 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCom
mand(CraftServer.java:514)
16:33:23 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchSer
verCommand(CraftServer.java:506)
16:33:23 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedServer.al(Dedica
tedServer.java:260)
16:33:23 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedServer.r(Dedicat
edServer.java:225)
16:33:23 [SEVERE] at net.minecraft.server.v1_4_6.MinecraftServer.q(Minecra
ftServer.java:494)
16:33:23 [SEVERE] at net.minecraft.server.v1_4_6.MinecraftServer.run(Minec
raftServer.java:427)
16:33:23 [SEVERE] at net.minecraft.server.v1_4_6.ThreadServerApplication.r
un(SourceFile:849)
NewItem.java的源代码在这里:
package com.tommy3244.plugins.MakeYourOwnBlocks;
import java.util.*;
import java.util.logging.Logger;
import java.io.Serializable;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
public class NewItem implements Serializable
{
public int id;
public byte data;
public int damage;
public String name;
public List<String> lore;
public List<List<String>> commands;
public NewItem(List<String> lore, String name, int itemid, byte itemdata, List<List<String>> lines, int damage, boolean hasRecipe, ConfigurationSection section)
{
id = itemid;
data = itemdata;
this.name = name;
this.damage = damage;
this.lore = lore;
commands = lines;
ItemStack stack;
if(data != 0)
{
stack = new ItemStack(id, data);
}
else
{
stack = new ItemStack(id);
}
if(hasRecipe)
{
try
{
ShapedRecipe recipe = new ShapedRecipe(stack);
String r1 = section.getString("items."+name+".r1");
String r2 = section.getString("items."+name+".r2");
String r3 = section.getString("items."+name+".r3");
List<Integer> defines = section.getIntegerList("items."+name+".defines");
Character[] chars = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
List<Character> charlist = Arrays.asList(chars);
for(int defined : defines)
{
Material md = Material.getMaterial(defined);
recipe.setIngredient(charlist.get(0), md);
}
String[] rows = {r1, r2, r3};
recipe.shape(rows);
Bukkit.addRecipe(recipe);
}
catch(Exception error)
{
}
}
}
public NewItem(List<String> lore, String name, int itemid, List<List<String>> lines, int damage, boolean hasRecipe, ConfigurationSection section)
{
this(lore, name, itemid, (byte)0, lines, damage, hasRecipe, section);
}
public Logger getLogger()
{
return Bukkit.getServer().getPluginManager().getPlugin("MakeYourOwnBlocks").getLogger();
}
public int getItem()
{
return id;
}
public String getName()
{
return name;
}
public boolean onRightClick(Player p, boolean right, boolean block)
{
int isright;
int isblock;
if(right)
{
isright = 1;
}
else
{
isright = 0;
}
if(block)
{
isblock = 1;
}
else
{
isblock = 0;
}
try
{
List<String> cmds = commands.get(0);
for(String line : cmds)
{
line = line.replaceAll("\\<block\\>", String.valueOf(isblock));
line = line.replaceAll("\\<right\\>", String.valueOf(isright));
String[] segments = line.split(" ");
String cmd = segments[0];
List<String> rest = Arrays.asList(segments);
getLogger().info(String.valueOf(rest));
rest.remove(0);
StringBuilder sb = new StringBuilder();
for(String stst : rest)
{
sb.append(stst);
}
String reststr = sb.toString();
if(cmd.equalsIgnoreCase("cmd"))
{
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), reststr);
}
if(cmd.equalsIgnoreCase("setblock"))
{
Block b = p.getWorld().getBlockAt(Integer.parseInt(rest.get(0)), Integer.parseInt(rest.get(1)), Integer.parseInt(rest.get(2)));
b.setTypeId(Integer.parseInt(rest.get(3)));
if(rest.size() > 4)
{
b.setData((byte)Integer.parseInt(rest.get(5)));
}
}
if(cmd.equalsIgnoreCase("say"))
{
p.sendMessage(reststr);
}
if(cmd.equalsIgnoreCase("kill"))
{
p.setHealth(0);
}
if(cmd.equalsIgnoreCase("hurt"))
{
p.setHealth(p.getHealth()-Integer.parseInt(rest.get(0)));
}
}
}
catch(Exception error)
{
error.printStackTrace();
return false;
}
return true;
}
public boolean onLeftClick(Player p, boolean right, boolean block)
{
int isright;
int isblock;
if(right)
{
isright = 1;
}
else
{
isright = 0;
}
if(block)
{
isblock = 1;
}
else
{
isblock = 0;
}
try
{
List<String> cmds = commands.get(1);
for(String line : cmds)
{
line.replaceAll("\\<block\\>", String.valueOf(isblock));
line.replaceAll("\\<right\\>", String.valueOf(isright));
String[] segments = line.split(" ");
String cmd = segments[0];
List<String> rest = Arrays.asList(segments);
rest.remove(rest.get(0));
StringBuilder sb = new StringBuilder();
for(String stst : rest)
{
sb.append(stst);
}
String reststr = sb.toString();
if(cmd.equalsIgnoreCase("cmd"))
{
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), reststr);
}
if(cmd.equalsIgnoreCase("setblock"))
{
Block b = p.getWorld().getBlockAt(Integer.parseInt(rest.get(0)), Integer.parseInt(rest.get(1)), Integer.parseInt(rest.get(2)));
b.setTypeId(Integer.parseInt(rest.get(3)));
if(rest.size() > 4)
{
b.setData((byte)Integer.parseInt(rest.get(5)));
}
}
if(cmd.equalsIgnoreCase("say"))
{
p.sendMessage(reststr);
}
if(cmd.equalsIgnoreCase("kill"))
{
p.setHealth(0);
}
if(cmd.equalsIgnoreCase("hurt"))
{
p.setHealth(p.getHealth()-Integer.parseInt(rest.get(0)));
}
}
}
catch(Exception error)
{
return false;
}
return true;
}
}
答案 0 :(得分:5)
您需要实施java.io.Serializable
:
public class NewItem implements Serializable
来自JavaDoc:
类的可序列化由实现该类的类启用 java.io.Serializable接口。没有实现此功能的类 接口不会将其任何状态序列化或反序列化。 可序列化类的所有子类型本身都是可序列化的。该 序列化接口没有方法或字段,仅用于 确定可序列化的语义。
答案 1 :(得分:3)
通过实施Serializable
接口
答案 2 :(得分:2)
您使用了一些List
。 List
未实施Serializable
。
[并非所有List
- 实施都可能是Serializable
]
我会尝试使用实现ArrayList
的{{1}}。
原因Serializable
还必须实施NewItem
。