Jump to content

[Solved] CapabilityProvider's serializeNBT() fired too often for Item inventory


logalu

Recommended Posts

Hi there,

 

I've implemented an Item with a 1-slot inventory using capabilities. The inventory is then accessible through a GUI on item right click. Everything seems to work fine, but I've observed that the `serializeNBT()` method is fired too often (every tick apparently) when the item is in the players' inventory, even though I do not fire at all such method manually (afaik...).

 

Is this the expected behaviour of the capabilities system, or am I missing or wrongdoing something? 

 

Thanks in advance

 

 

public class MyItem extends Item {
		
	public MyItem(){
		super();
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand){
		ItemStack stack = player.getHeldItem(hand);

		if(!world.isRemote)
			player.openGui(MyMod.instance, 0, world, (int)player.posX, (int)player.posY, (int)player.posZ);

		return ActionResult.newResult(EnumActionResult.PASS, stack);
	}
	
	@Override
	public ICapabilityProvider initCapabilities(ItemStack item, NBTTagCompound nbt)
	{
		if(item.getItem() instanceof MyItem)
		{
			return new MyCapabilityProvider();
		}
		return null;
	}
	
	public class MyCapabilityProvider implements ICapabilityProvider, ICapabilitySerializable<NBTTagCompound> {

		private final IItemHandler inventory;
		
		public MyCapabilityProvider(){
			inventory = new ItemStackHandler(1);
		}
		
		@Override
		public NBTTagCompound serializeNBT() 
		{
			return ((ItemStackHandler)inventory).serializeNBT();
		}

		@Override
		public void deserializeNBT(NBTTagCompound nbt) 
		{
			((ItemStackHandler)inventory).deserializeNBT(nbt);
		}

		@Override
		public boolean hasCapability(Capability<?> capability, EnumFacing facing) 
		{
			if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
				return true;
			
			return false;
		}

		@Override
		public <T> T getCapability(Capability<T> capability, EnumFacing facing) 
		{
			if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
			{
				return (T)inventory;
			}
			return null;
		}
	}
}
Edited by logalu
Marked as solved
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.