Jump to content

[Solved] Checking if the player has nothing in their hand


Recommended Posts

I am working on adding a feature in my mod that will let the player craft grass blocks. Basically the player will right click on a grass block with a shovel and the grass block will be turned into dirt, then the player will receive a grass seed. The grass seed can then be used in a recipe with dirt to craft a grass block. However, I am having an issue when checking if the player has a shovel in their hand, because I am using an itemstack of the item in the players hand, if the player right clicks with an empty hand it crashes. So I tried writing a statement that would check if a player has nothing in their hand and if they do kick them out of the if statement series, however it's not working. So my question is how do I properly perform this check? Below is my code. TIA


public void onPlayerInteract(PlayerInteractEvent event) {

	ItemStack itemstack = event.entityPlayer.inventory.getCurrentItem();

	//Check to see if the player right clicked
	if(event.action.equals(event.action.RIGHT_CLICK_BLOCK)) {

		//Check if player has nothing in their hand
		if(!(event.entityPlayer.inventory.currentItem == 0)) {

			if(itemstack.itemID == 269 || itemstack.itemID == 273 || itemstack.itemID == 256 || itemstack.itemID == 284 || itemstack.itemID == 277) {

				//Change Grass to dirt
				if(event.entityPlayer.worldObj.getBlockId(event.x, event.y, event.z) == 2) {
					event.entityPlayer.worldObj.setBlock(event.x, event.y, event.z, 3);

					//Drop "Grass Seeds" in front of player
					event.entityPlayer.dropPlayerItem(new ItemStack(myMod.grassSeed, 1, 0));

Creator of the Recipe Expansion Pack mod.


Updated to 1.7.2!

Link to comment
Share on other sites

This topic is now closed to further replies.


  • Create New...

Important Information

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