Jump to content

Unable to set item in main hand when item breaks


Geenium

Recommended Posts

I'm rather new to forge and I'm currently doing some basic tests.

I have an item that has durability and I reduce the durability when the item is used to break blocks or is right clicked whilst being held.

When the item breaks I am putting a different item into the player's mainhand.

The problem is I can't put a new item into the player's hand when the item breaks from right clicking but works fine if the item breaks from breaking a block with it.

 

These methods are in my new item class.

@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving) {
	damageTrinketInHand(stack, entityLiving);
	return true;
}

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand handIn) {
	ItemStack stack = player.getHeldItemMainhand();
	damageTrinketInHand(stack, player);
	return new ActionResult<>(EnumActionResult.SUCCESS, stack);
}

private void damageTrinketInHand(ItemStack stack, EntityLivingBase entityLiving) {
	stack.damageItem(1, entityLiving);
	if (entityLiving.getHeldItemMainhand().isEmpty()) {
		System.out.println("Trinket broken");
		entityLiving.setHeldItem(EnumHand.MAIN_HAND, new ItemStack(ModItems.BROKEN_TRINKET));
	}
}

 

I should point out that I can place items into another slot i.e. offhand and this will work for both ways the item can break, but currently it specifically won't work when putting a new item into the slot that the intial item was in before the item broke by right clicking with it.

Link to comment
Share on other sites

8 hours ago, Geenium said:

I should point out that I can place items into another slot i.e. offhand and this will work for both ways the item can break, but currently it specifically won't work when putting a new item into the slot that the intial item was in before the item broke by right clicking with it.

Try setting stack to the new stack instead of setting it via EntityLivingBase#setHeldItem.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

1 minute ago, Geenium said:

I'm sorry, but how would I do that.

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand handIn) {
	ItemStack stack = player.getHeldItemMainhand();
	damageTrinketInHand(stack, player);
  	if (stack.isEmpty()) {
		stack = new ItemStack(ModItems.BROKEN_TRINKET);
	}
	return new ActionResult<>(EnumActionResult.SUCCESS, stack);
}

private void damageTrinketInHand(ItemStack stack, EntityLivingBase entityLiving) {
	stack.damageItem(1, entityLiving);
}
  • Thanks 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.



×
×
  • Create New...

Important Information

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