How do I make a block clone what's in my right hand?

Started by HariBanGa on Fri, 12/29/2017 - 16:04

Topic category: Advanced modding

Last seen on 12:06, 8. Jan 2018
Joined Dec 2017
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
How do I make a block clone what's in my right hand?
Fri, 12/29/2017 - 16:04

Basically, what I want to do is have a block where you simply right-click on the block, and the block gives you the item that you already have in your right hand (for example, if you held a diamond and you right-clicked the Cloning Block, the Block would drop a diamond, so now you would have two diamonds in your hand).  However, I don't know what to write in the code section, as it is not a default option.

Here is the code:

if (entity.inventory.getCurrentItem() != null && entity.inventory.getCurrentItem().getItem() == whatdoIwritehere) {
                if (entity instanceof EntityPlayer)
                    ((EntityPlayer) entity).inventory.addItemStackToInventory(new ItemStack(whatdoIwritehere, 1));

I've tried using itemInPlayersHand[] but it doesn't work. Please help.

Last seen on 00:21, 11. Aug 2019
Joined Aug 2013
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Just remove the 2nd check…
Fri, 12/29/2017 - 18:04

Just remove the 2nd check and as a parameter in addItemStackToInventory use the ItemStack from getCurrentItem()  (and it would be a good idea to call ItemStack#copy()), so you will end up with something like this:

 

if (entity instanceof EntityPlayer)
{
                if (entity.inventory.getCurrentItem() != null)
				{
                    ((EntityPlayer) entity).inventory.addItemStackToInventory(entity.inventory.getCurrentItem().copy());
				}
}

 

Last seen on 12:06, 8. Jan 2018
Joined Dec 2017
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Thanks very much! It's…
Sat, 12/30/2017 - 16:46

Thanks very much! It's working great.