Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

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

  • Author

Thanks for the reply. Good to know I didn't cause this, not so good to know this is unavoidable, but it is what it is :)

 

Marking as solved

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.