Hi,
I have a GUI which opens upon rightclicking a custom item. Basically you can add and remove players to/from the item, and in order to save the players I'm using the ItemStack's NBTTagCompound via player.getCurrentEquippedItem().getTagCompound().
Whenever the NBT data gets changed and the GUI is closed and quickly reopened, I get a serverside crash:
This is the method from the item's class where the GUI is opened from:
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
if(!par2World.isRemote)
{
if(!par1ItemStack.hasTagCompound())
{
par1ItemStack.stackTagCompound = new NBTTagCompound();
ClientUtils.syncItemNBT(par1ItemStack);
}
if(aBoolean)
par3EntityPlayer.openGui(Main.instance, guiToOpen, par2World, (int) par3EntityPlayer.posX, (int) par3EntityPlayer.posY, (int) par3EntityPlayer.posZ);
}
return par1ItemStack;
}
This is ClientUtils.syncItemNBT():
@SideOnly(Side.CLIENT)
public static void syncItemNBT(ItemStack item)
{
Main.network.sendToServer(new PacketSUpdateNBTTag(item));
}
The Packet:
public class PacketSUpdateNBTTag implements IMessage
{
private NBTTagCompound stack;
private String itemName;
public PacketSUpdateNBTTag(ItemStack par1ItemStack)
{
if(par1ItemStack != null && par1ItemStack.hasTagCompound())
{
this.stack = par1ItemStack.stackTagCompound;
this.itemName = par1ItemStack.getUnlocalizedName();
}
}
public void fromBytes(ByteBuf buf)
{
this.stack = ByteBufUtils.readTag(buf);
this.itemName = ByteBufUtils.readUTF8String(buf);
}
public void toBytes(ByteBuf buf)
{
ByteBufUtils.writeTag(buf, this.stack);
ByteBufUtils.writeUTF8String(buf, this.itemName);
}
public static class Handler extends PacketHelper implements IMessageHandler<PacketSUpdateNBTTag, IMessage>
{
public IMessage onMessage(PacketSUpdateNBTTag message, MessageContext context)
{
if(context.getServerHandler().playerEntity.getCurrentEquippedItem() != null && context.getServerHandler().playerEntity.getCurrentEquippedItem().getItem().getUnlocalizedName().matches(message.itemName))
context.getServerHandler().playerEntity.getCurrentEquippedItem().stackTagCompound = message.stack;
return null;
}
}
}
What causes this crash and how can I fix it?
Thanks in advance for any help!